Class: RR::TaskSweeper
- Inherits:
-
Object
- Object
- RR::TaskSweeper
- Defined in:
- lib/rubyrep/task_sweeper.rb
Overview
Monitors and cancels stalled tasks
Instance Attribute Summary collapse
-
#timeout_period ⇒ Object
Time in seconds after which a task is considered stalled.
Class Method Summary collapse
-
.timeout(timeout_period) ⇒ Object
Executes the give block in a separate Thread.
Instance Method Summary collapse
-
#initialize(timeout_period) ⇒ TaskSweeper
constructor
Creates a new TaskSweeper *
timeout_period
: timeout value in seconds. -
#join ⇒ Object
Waits without timeout till the task executing thread is finished.
-
#ping ⇒ Object
Must be called by the executed task to announce it is still alive.
-
#terminated? ⇒ Boolean
Returns
true
if the task was timed out.
Constructor Details
#initialize(timeout_period) ⇒ TaskSweeper
Creates a new TaskSweeper
-
timeout_period
: timeout value in seconds
40 41 42 43 44 |
# 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 |
Instance Attribute Details
#timeout_period ⇒ Object
Time in seconds after which a task is considered stalled. Timer is reset by calling #ping.
20 21 22 |
# File 'lib/rubyrep/task_sweeper.rb', line 20 def timeout_period @timeout_period end |
Class Method Details
.timeout(timeout_period) ⇒ Object
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).
12 13 14 15 16 |
# 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 |
Instance Method Details
#join ⇒ Object
Waits without timeout till the task executing thread is finished
34 35 36 |
# File 'lib/rubyrep/task_sweeper.rb', line 34 def join thread && thread.join end |
#ping ⇒ Object
Must be called by the executed task to announce it is still alive
23 24 25 |
# File 'lib/rubyrep/task_sweeper.rb', line 23 def ping self.last_ping = Time.now end |
#terminated? ⇒ Boolean
Returns true
if the task was timed out. The terminated task is expected to free all resources and exit.
29 30 31 |
# File 'lib/rubyrep/task_sweeper.rb', line 29 def terminated? terminated end |