[Backgroundrb-devel] can Timeout::timeout(...) be used in a Worker ?

skaar skaar at waste.org
Fri Dec 15 13:17:37 EST 2006

What is it that doesn't work with Timeout::timeout?

  class TimeoutWorker < BackgrounDRb::Worker::Base
    require 'timeout'
    def do_work(args)
    def simple_timeout
      Timeout::timeout(5) { sleep 10 }
    def rescued_timeout
      rescue Timeout::Error
        "timed out"

There is a number of things to notice with this. You need to make sure
that to handle exceptions in do_work properly. 'do_work' is called
automatically as a backgrounded call, so an exception will never reach
the caller. The work_thread worker method will terminate the worker on
an unhandled exception. Above, I've commented out self.simple_timeout,
since it would always result in the worker getting terminated.

If you want to call

  worker(new_worker(:class => :timeout_worker, :job_key => :timeout)).simple_timeout

directly, you will also need to require 'timeout' on the caller side,
otherwise Timeout::Error will be an unknown symbol for DRb.

The resuced_timout method properly handle the exception in the worker.


* george . (george at initiatecommerce.com) [061214 22:12]:
> Hi ,
> I was wondering if anyone else has tried to use the Timeout::timeout  
> method in a backgroundrb Worker and had Timeout raise an exception  
> after the allotted amount of time .
> I ask because i have written a test script to make sure Timeout would  
> work .  I have tested the script by directly feeding it to ruby with :
> $ ruby test_script.rb
> and piping the script to the rails console:
> $ script/console < test_script.rb
> Both of which work as expected .
> I am using the latest release of backgroundrb: 0.2.1 (Released  
> November 28, 2006 - r162) .
> If it is not possible to use TImeout::timeout in a Worker currently,  
> is there another means to accomplish the same goal as Timeout::timeout ?
> thanks ,
> george .
> _______________________________________________
> Backgroundrb-devel mailing list
> Backgroundrb-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/backgroundrb-devel

|\|\             where in the       |          s_u_b_s_t_r_u_c_t_i_o_n 
| | >===========  W.A.S.T.E.        |                  genarratologies 
|/|/    (_)     is the wisdom       |                  skaar at waste.org

More information about the Backgroundrb-devel mailing list