Class: PerfectQueue::Worker
- Inherits:
-
Object
- Object
- PerfectQueue::Worker
- Defined in:
- lib/perfectqueue/worker.rb
Class Method Summary collapse
Instance Method Summary collapse
- #detach ⇒ Object
-
#initialize(runner, config = nil, &block) ⇒ Worker
constructor
A new instance of Worker.
- #logrotated ⇒ Object
- #restart(immediate) ⇒ Object
- #run ⇒ Object
- #stop(immediate) ⇒ Object
Constructor Details
#initialize(runner, config = nil, &block) ⇒ Worker
Returns a new instance of Worker.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/perfectqueue/worker.rb', line 26 def initialize(runner, config=nil, &block) block = Proc.new { config } if config config = block.call @config = config @runner = runner @detach_wait = config[:detach_wait] || config['detach_wait'] || 10.0 @sv = Supervisor.new(runner, &block) @detach = false @finish_flag = BlockingFlag.new end |
Class Method Details
.run(runner, config = nil, &block) ⇒ Object
22 23 24 |
# File 'lib/perfectqueue/worker.rb', line 22 def self.run(runner, config=nil, &block) new(runner, config, &block).run end |
Instance Method Details
#detach ⇒ Object
87 88 89 90 91 |
# File 'lib/perfectqueue/worker.rb', line 87 def detach send_signal(:INT) @detach = true @finish_flag.set! end |
#logrotated ⇒ Object
83 84 85 |
# File 'lib/perfectqueue/worker.rb', line 83 def logrotated send_signal(:USR2) end |
#restart(immediate) ⇒ Object
79 80 81 |
# File 'lib/perfectqueue/worker.rb', line 79 def restart(immediate) send_signal(immediate ? :HUP : :USR1) end |
#run ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/perfectqueue/worker.rb', line 40 def run @pid = fork do $0 = "perfectqueue-supervisor:#{@runner}" @sv.run exit! 0 end install_signal_handlers begin until @finish_flag.set? pid, status = Process.waitpid2(@pid, Process::WNOHANG) break if pid @finish_flag.wait(1) end return if pid if @detach wait_time = Time.now + @detach_wait while (w = wait_time - Time.now) > 0 sleep [0.5, w].min pid, status = Process.waitpid2(@pid, Process::WNOHANG) break if pid end else # child process finished unexpectedly end rescue Errno::ECHILD end end |
#stop(immediate) ⇒ Object
75 76 77 |
# File 'lib/perfectqueue/worker.rb', line 75 def stop(immediate) send_signal(immediate ? :QUIT : :TERM) end |