Class: Sidekiq::Manager
Overview
The Manager is the central coordination point in Sidekiq, controlling the lifecycle of the Processors and feeding them jobs as necessary.
Tasks:
-
start: Spin up Processors.
-
processor_died: Handle job failure, throw away Processor, create new one.
-
quiet: shutdown idle Processors.
-
stop: hard stop the Processors by deadline.
Note that only the last task requires its own Thread since it has to monitor the shutdown process. The other tasks are performed by other threads.
Constant Summary collapse
- PAUSE_TIME =
hack for quicker development / testing environment #2774
STDOUT.tty? ? 0.1 : 0.5
Constants included from Util
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Manager
constructor
A new instance of Manager.
- #processor_died(processor, reason) ⇒ Object
- #processor_stopped(processor) ⇒ Object
- #quiet ⇒ Object
- #start ⇒ Object
- #stop(deadline) ⇒ Object
- #stopped? ⇒ Boolean
Methods included from Util
#fire_event, #hostname, #identity, #logger, #process_nonce, #redis, #safe_thread, #watchdog
Methods included from ExceptionHandler
Constructor Details
#initialize(options = {}) ⇒ Manager
Returns a new instance of Manager.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sidekiq/manager.rb', line 31 def initialize(={}) logger.debug { .inspect } @options = @count = [:concurrency] || 25 raise ArgumentError, "Concurrency of #{@count} is not supported" if @count < 1 @done = false @workers = Set.new @count.times do @workers << Processor.new(self) end @plock = Mutex.new end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/sidekiq/manager.rb', line 29 def @options end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
28 29 30 |
# File 'lib/sidekiq/manager.rb', line 28 def workers @workers end |
Instance Method Details
#processor_died(processor, reason) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sidekiq/manager.rb', line 91 def processor_died(processor, reason) @plock.synchronize do @workers.delete(processor) unless @done p = Processor.new(self) @workers << p p.start end end end |
#processor_stopped(processor) ⇒ Object
85 86 87 88 89 |
# File 'lib/sidekiq/manager.rb', line 85 def processor_stopped(processor) @plock.synchronize do @workers.delete(processor) end end |
#quiet ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/sidekiq/manager.rb', line 51 def quiet return if @done @done = true logger.info { "Terminating quiet workers" } @workers.each { |x| x.terminate } fire_event(:quiet, true) end |
#start ⇒ Object
45 46 47 48 49 |
# File 'lib/sidekiq/manager.rb', line 45 def start @workers.each do |x| x.start end end |
#stop(deadline) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sidekiq/manager.rb', line 63 def stop(deadline) quiet fire_event(:shutdown, true) # some of the shutdown events can be async, # we don't have any way to know when they're done but # give them a little time to take effect sleep PAUSE_TIME return if @workers.empty? logger.info { "Pausing to allow workers to finish..." } remaining = deadline - Time.now while remaining > PAUSE_TIME return if @workers.empty? sleep PAUSE_TIME remaining = deadline - Time.now end return if @workers.empty? hard_shutdown end |
#stopped? ⇒ Boolean
102 103 104 |
# File 'lib/sidekiq/manager.rb', line 102 def stopped? @done end |