Class: PerfectQueue::Worker

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

Class Method Summary collapse

Instance Method Summary collapse

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
# 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)
  @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

#detachObject



75
76
77
78
# File 'lib/perfectqueue/worker.rb', line 75

def detach
  send_signal(:INT)
  @finish_flag.set!
end

#logrotatedObject



71
72
73
# File 'lib/perfectqueue/worker.rb', line 71

def logrotated
  send_signal(:USR2)
end

#restart(immediate) ⇒ Object



67
68
69
# File 'lib/perfectqueue/worker.rb', line 67

def restart(immediate)
  send_signal(immediate ? :HUP : :USR1)
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/perfectqueue/worker.rb', line 39

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)
      @finish_flag.wait(1)
    end

    unless pid
      # child process is alive but detached
      sleep @detach_wait
    end

  rescue Errno::ECHILD
  end
end

#stop(immediate) ⇒ Object



63
64
65
# File 'lib/perfectqueue/worker.rb', line 63

def stop(immediate)
  send_signal(immediate ? :TERM : :QUIT)
end