Class: PerfectQueue::TaskMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/perfectqueue/task_monitor.rb

Instance Method Summary collapse

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

#joinObject



51
52
53
# File 'lib/perfectqueue/task_monitor.rb', line 51

def join
  @thread.join
end

#kill_task(reason) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/perfectqueue/task_monitor.rb', line 84

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

#runObject



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
143
# File 'lib/perfectqueue/task_monitor.rb', line 109

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



68
69
70
71
72
73
74
# File 'lib/perfectqueue/task_monitor.rb', line 68

def set_heartbeat_message(task, message)
  @mutex.synchronize {
    if task == @task
      @heartbeat_message = message
    end
  }
end

#set_task(task, runner) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/perfectqueue/task_monitor.rb', line 55

def set_task(task, runner)
  task.extend(TaskMonitorHook)
  task.log = @log
  task.task_monitor = self
  task.runner = runner
  @mutex.synchronize {
    @task = task
    @last_task_heartbeat = Time.now.to_i
    @heartbeat_message = nil
  }
end

#startObject



40
41
42
# File 'lib/perfectqueue/task_monitor.rb', line 40

def start
  @thread = Thread.new(&method(:run))
end

#stopObject



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



76
77
78
79
80
81
82
# File 'lib/perfectqueue/task_monitor.rb', line 76

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



99
100
101
102
103
104
105
106
107
# File 'lib/perfectqueue/task_monitor.rb', line 99

def task_finished(task, &block)
  @mutex.synchronize {
    ret = block.call if block  # TODO is this ought to be synchronized?
    if task == @task
      @task = nil
    end
    ret
  }
end