Class: PerfectQueue::Engine
- Inherits:
-
Object
- Object
- PerfectQueue::Engine
- Defined in:
- lib/perfectqueue/engine.rb
Instance Method Summary collapse
-
#initialize(runner, config) ⇒ Engine
constructor
A new instance of Engine.
- #join ⇒ Object
- #logrotated ⇒ Object
- #replace(immediate, command = [$0]+ARGV) ⇒ Object
- #restart(immediate, config) ⇒ Object
- #run ⇒ Object
- #shutdown(immediate) ⇒ Object
- #stop(immediate) ⇒ Object
Constructor Details
#initialize(runner, config) ⇒ Engine
Returns a new instance of Engine.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/perfectqueue/engine.rb', line 22 def initialize(runner, config) @runner = runner @finish_flag = BlockingFlag.new processor_type = config[:processor_type] || :process case processor_type.to_sym when :process @processor_class = Multiprocess::ForkProcessor when :thread @processor_class = Multiprocess::ThreadProcessor else raise ConfigError, "Unknown processor_type: #{config[:processor_type].inspect}" end @processors = [] restart(false, config) end |
Instance Method Details
#join ⇒ Object
88 89 90 91 |
# File 'lib/perfectqueue/engine.rb', line 88 def join @processors.each {|c| c.join } self end |
#logrotated ⇒ Object
108 109 110 |
# File 'lib/perfectqueue/engine.rb', line 108 def logrotated @processors.each {|c| c.logrotated } end |
#replace(immediate, command = [$0]+ARGV) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/perfectqueue/engine.rb', line 98 def replace(immediate, command=[$0]+ARGV) return if @replaced_pid stop(immediate) @replaced_pid = Process.fork do exec(*command) exit!(127) end self end |
#restart(immediate, config) ⇒ Object
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 |
# File 'lib/perfectqueue/engine.rb', line 41 def restart(immediate, config) return nil if @finish_flag.set? # TODO connection check @log = config[:logger] || Logger.new(STDERR) num_processors = config[:processors] || 1 # scaling extra = num_processors - @processors.length if extra > 0 extra.times do @processors << @processor_class.new(@runner, config) end elsif extra < 0 -extra.times do c = @processors.shift c.stop(immediate) c.join end extra = 0 end @processors[0..(-extra-1)].each {|c| c.restart(immediate, config) } @child_keepalive_interval = (config[:child_keepalive_interval] || config[:child_heartbeat_interval] || 2).to_i self end |
#run ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/perfectqueue/engine.rb', line 74 def run until @finish_flag.set? @processors.each {|c| c.keepalive } @finish_flag.wait(@child_keepalive_interval) end join end |
#shutdown(immediate) ⇒ Object
93 94 95 96 |
# File 'lib/perfectqueue/engine.rb', line 93 def shutdown(immediate) stop(immediate) join end |
#stop(immediate) ⇒ Object
82 83 84 85 86 |
# File 'lib/perfectqueue/engine.rb', line 82 def stop(immediate) @processors.each {|c| c.stop(immediate) } @finish_flag.set! self end |