Class: Sidekiq::Manager
- Inherits:
-
Object
- Object
- Sidekiq::Manager
- Includes:
- Component
- Defined in:
- lib/sidekiq/manager.rb
Overview
The Manager is the central coordination point in Sidekiq, controlling the lifecycle of the Processors.
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.
Instance Attribute Summary collapse
-
#capsule ⇒ Object
readonly
Returns the value of attribute capsule.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Attributes included from Component
Instance Method Summary collapse
-
#initialize(capsule) ⇒ Manager
constructor
A new instance of Manager.
- #processor_result(processor, reason = nil) ⇒ Object
- #quiet ⇒ Object
- #start ⇒ Object
- #stop(deadline) ⇒ Object
- #stopped? ⇒ Boolean
Methods included from Component
#default_tag, #fire_event, #handle_exception, #hostname, #identity, #inspect, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog
Constructor Details
#initialize(capsule) ⇒ Manager
Returns a new instance of Manager.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sidekiq/manager.rb', line 26 def initialize(capsule) @config = @capsule = capsule @count = capsule.concurrency raise ArgumentError, "Concurrency of #{@count} is not supported" if @count < 1 @done = false @workers = Set.new @plock = Mutex.new @count.times do @workers << Processor.new(@config, &method(:processor_result)) end end |
Instance Attribute Details
#capsule ⇒ Object (readonly)
Returns the value of attribute capsule.
24 25 26 |
# File 'lib/sidekiq/manager.rb', line 24 def capsule @capsule end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
23 24 25 |
# File 'lib/sidekiq/manager.rb', line 23 def workers @workers end |
Instance Method Details
#processor_result(processor, reason = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sidekiq/manager.rb', line 69 def processor_result(processor, reason = nil) @plock.synchronize do @workers.delete(processor) unless @done p = Processor.new(@config, &method(:processor_result)) @workers << p p.start end end end |
#quiet ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/sidekiq/manager.rb', line 43 def quiet return if @done @done = true logger.info { "Terminating quiet threads for #{capsule.name} capsule" } @workers.each(&:terminate) end |
#start ⇒ Object
39 40 41 |
# File 'lib/sidekiq/manager.rb', line 39 def start @workers.each(&:start) end |
#stop(deadline) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sidekiq/manager.rb', line 51 def stop(deadline) quiet # 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 jobs to finish..." } wait_for(deadline) { @workers.empty? } return if @workers.empty? hard_shutdown ensure capsule.stop end |
#stopped? ⇒ Boolean
80 81 82 |
# File 'lib/sidekiq/manager.rb', line 80 def stopped? @done end |