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) ⇒ TaskMonitor

Returns a new instance of TaskMonitor.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/perfectqueue/task_monitor.rb', line 22

def initialize(config, child_heartbeat=nil)
  @config = config
  @log = config[:logger]
  @child_heartbeat = child_heartbeat || 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



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

def join
  @thread.join
end

#kill_task(reason) ⇒ Object



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

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



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

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_time = next_child_heartbeat
      end

      next_wait = [1, next_time - now].max
      @cond.wait(next_wait) if next_wait > 0  # TODO timeout doesn't work?

      now = Time.now.to_i
      if @task && next_task_heartbeat && now <= next_task_heartbeat
        task_heartbeat
        @last_task_heartbeat = now
      end

      if now <= next_child_heartbeat
        @child_heartbeat.call  # will recursive lock
        @last_child_heartbeat = now
      end
    end
  }
rescue
  @log.error "Unknown error #{$!.class}: #{$!}. Exiting worker pid=#{Process.pid}"
  $!.backtrace.each {|bt| @log.warn "\t#{bt}" }
end

#set_heartbeat_message(task, message) ⇒ Object

callback



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

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

#set_task(task, runner) ⇒ Object



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

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

#startObject



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

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

#stopObject



43
44
45
46
47
48
# File 'lib/perfectqueue/task_monitor.rb', line 43

def stop
  @finished = true
  @mutex.synchronize {
    @cond.broadcast
  }
end

#stop_task(immediate) ⇒ Object



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

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



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

def task_finished(task, &block)
  @mutex.synchronize {
    ret = block.call if block
    if task == @task
      @task = nil
    end
    ret
  }
end