Maxim Veksler
Maxim Veksler

Reputation: 30182

How to implement timeout on a running task?

I need to write a ruby block that will be polling remote network service. Since, it’s network love I need to enforce several conditions:

Is there an execution library or equivalent in ruby which I can use? I need to basically setup a thread to run in the background and block on it’s execution, then expect the status / return value of the thread (block) and act upon the result.

Being somewhat new to ruby //

Upvotes: 0

Views: 528

Answers (2)

shivam
shivam

Reputation: 16506

Try something like this:

Timeout.timeout(60) do #time is sec
  #your code    
end

rescue Timeout::Error
  #rescue code
end

Upvotes: 2

Yanhao
Yanhao

Reputation: 5294

Please check Timeout in Ruby's Std-lib.

Upvotes: 0

Related Questions