Class: PerfectQueue::TaskMonitor
- Inherits:
-
Object
- Object
- PerfectQueue::TaskMonitor
- Defined in:
- lib/perfectqueue/task_monitor.rb
Instance Method Summary collapse
-
#initialize(config, child_heartbeat = nil, force_stop = nil) ⇒ TaskMonitor
constructor
A new instance of TaskMonitor.
- #join ⇒ Object
- #kill_task(reason) ⇒ Object
- #run ⇒ Object
-
#set_heartbeat_message(task, message) ⇒ Object
callback.
- #set_task(task, runner) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #stop_task(immediate) ⇒ Object
-
#task_finished(task, &block) ⇒ Object
callback.
Constructor Details
#initialize(config, child_heartbeat = nil, force_stop = nil) ⇒ TaskMonitor
Returns a new instance of TaskMonitor.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/perfectqueue/task_monitor.rb', line 22 def initialize(config, child_heartbeat=nil, force_stop=nil) @config = config @log = config[:logger] @child_heartbeat = child_heartbeat || Proc.new {} @force_stop = force_stop || Proc.new {} @child_heartbeat_interval = (@config[:child_heartbeat_interval] || 2).to_i @task_heartbeat_interval = (@config[:task_heartbeat_interval] || 2).to_i @last_child_heartbeat = Time.now.to_i @last_task_heartbeat = Time.now.to_i @task = nil @mutex = Monitor.new # support recursive lock @cond = @mutex.new_cond @finished = false end |
Instance Method Details
#join ⇒ Object
51 52 53 |
# File 'lib/perfectqueue/task_monitor.rb', line 51 def join @thread.join end |
#kill_task(reason) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/perfectqueue/task_monitor.rb', line 83 def kill_task(reason) @mutex.synchronize { if task = @task begin task.runner.kill(reason) # may recursive lock rescue @log.error "failed to kill task: #{$!.class}: #{$!}" $!.backtrace.each {|bt| @log.warn "\t#{bt}" } raise # force exit end end } end |
#run ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/perfectqueue/task_monitor.rb', line 108 def run @mutex.synchronize { now = Time.now.to_i until @finished next_child_heartbeat = @last_child_heartbeat + @child_heartbeat_interval if @task next_task_heartbeat = @last_task_heartbeat + @task_heartbeat_interval next_time = [next_child_heartbeat, next_task_heartbeat].min else next_task_heartbeat = nil next_time = next_child_heartbeat end next_wait = next_time - now @cond.wait(next_wait) if next_wait > 0 now = Time.now.to_i if @task && next_task_heartbeat && next_task_heartbeat <= now task_heartbeat @last_task_heartbeat = now end if next_child_heartbeat <= now @child_heartbeat.call # will recursive lock @last_child_heartbeat = now end end } rescue @log.error "Unknown error #{$!.class}: #{$!}" $!.backtrace.each {|bt| @log.warn "\t#{bt}" } @force_stop.call end |
#set_heartbeat_message(task, message) ⇒ Object
callback
67 68 69 70 71 72 73 |
# File 'lib/perfectqueue/task_monitor.rb', line 67 def (task, ) @mutex.synchronize { if task == @task @heartbeat_message = end } end |
#set_task(task, runner) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/perfectqueue/task_monitor.rb', line 55 def set_task(task, runner) task.extend(TaskMonitorHook) task.task_monitor = self task.runner = runner @mutex.synchronize { @task = task @last_task_heartbeat = Time.now.to_i @heartbeat_message = nil } end |
#start ⇒ Object
40 41 42 |
# File 'lib/perfectqueue/task_monitor.rb', line 40 def start @thread = Thread.new(&method(:run)) end |
#stop ⇒ Object
44 45 46 47 48 49 |
# File 'lib/perfectqueue/task_monitor.rb', line 44 def stop @finished = true @mutex.synchronize { @cond.broadcast } end |
#stop_task(immediate) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/perfectqueue/task_monitor.rb', line 75 def stop_task(immediate) if immediate kill_task ImmediateProcessStopError.new('immediate stop requested') else kill_task GracefulProcessStopError.new('graceful stop requested') end end |
#task_finished(task, &block) ⇒ Object
callback
98 99 100 101 102 103 104 105 106 |
# File 'lib/perfectqueue/task_monitor.rb', line 98 def task_finished(task, &block) @mutex.synchronize { ret = block.call if block if task == @task @task = nil end ret } end |