Monitors and cancels stalled tasks
Time in seconds after which a task is considered stalled. Timer is reset by calling ping.
Creates a new TaskSweeper
timeout_period: timeout value in seconds
# File lib/rubyrep/task_sweeper.rb, line 40 def initialize(timeout_period) self.timeout_period = timeout_period self.terminated = false self.last_ping = Time.now end
Executes the give block in a separate Thread. Returns if block is finished or stalled. The block must call regular ping to announce it is not stalled.
timeout_period: Maximum time (in seonds) without ping, after which a task is considered stalled.
Returns the created sweeper (allows checking if task was terminated).
# File lib/rubyrep/task_sweeper.rb, line 12 def self.timeout(timeout_period) sweeper = TaskSweeper.new(timeout_period) sweeper.send(:timeout) {yield sweeper} sweeper end
Waits without timeout till the task executing thread is finished
# File lib/rubyrep/task_sweeper.rb, line 34 def join thread && thread.join end
Executes the given block and times it out if stalled.
# File lib/rubyrep/task_sweeper.rb, line 58 def timeout exception = nil self.thread = Thread.new do begin yield rescue Exception => e # save exception so it can be rethrown outside of the thread exception = e end end while self.thread.join(self.timeout_period) == nil do if self.last_ping < Time.now - self.timeout_period self.terminated = true break end end
Generated with the Darkfish Rdoc Generator 2.