Setting a timeout for a system call in Rails

From Rails, I make a wget system call:

 system("wget", ...) 

I want to set a timeout for this call, so if it takes too much time (which probably means too many uploaded files or a large file uploaded), I want to stop it and return an error to the user so that my server is not overloaded . How can i do this?

+4
source share
2 answers

Need to make a call in a subarea? If not, use timeout and callbacks:

 require 'timeout' Timeout.timeout(3) do puts `tree /` # raises an exception, which you can rescue and handle end 

If you need to run it from the outside, I would go with something like Subexec

+2
source

In general, try wrapping the call in SystemTimer. https://rubygems.org/gems/SystemTimer

In your specific case, try system("wget -T #{timeout_in_seconds}")

+1
source

Source: https://habr.com/ru/post/1479250/


All Articles