Module: Core::Worker

Included in:
Core::Workers::DumpStats
Defined in:
lib/svcbase/worker.rb

Overview

common start/stop

Instance Method Summary collapse

Instance Method Details

#run_at_startupObject



21
22
23
# File 'lib/svcbase/worker.rb', line 21

def run_at_startup
  false
end

#worker_startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/svcbase/worker.rb', line 25

def worker_start
  worker_stop

  queue = Queue.new
  @worker_thread = Thread.new do
    log.debug "Worker thread started for #{self.class}"
    worker_loop queue
    log.debug "Worker thread exited for #{self.class}"
  end
  @worker_thread[:queue] = queue

  queue.push :do_work if run_at_startup

  @worker_thread[:timer] = Thread.new do
    loop do
      sleep interval_seconds
      queue.push :do_work
    end
  end
end

#worker_stopObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/svcbase/worker.rb', line 46

def worker_stop
  return unless (w = @worker_thread)
  @worker_thread = nil
  w[:timer].kill
  w[:stopping] = true
  w[:queue].push :do_stop

  # wait a little bit to see if it shuts down then log
  log.debug "Worker thread draining for #{self.class} #{w.object_id.to_s(32)}" unless w.join(0.2)

  # wait 15 seconds for worker to finish -- if not, kill it
  return if w.join(Config.get_f!(:WORKER_SHUTDOWN_GRACE_PERIOD, 15))
  w.kill
  log.warn "Worker thread killed for #{self.class} #{w.object_id.to_s(32)}"
end

#worker_stopping?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/svcbase/worker.rb', line 62

def worker_stopping?
  Thread.current[:stopping]
end