Class: RR::TaskSweeper

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/task_sweeper.rb

Overview

Monitors and cancels stalled tasks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_periodObject

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

#joinObject

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

#pingObject

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.

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubyrep/task_sweeper.rb', line 29

def terminated?
  terminated
end