Class: Sidekiq::Manager
- Inherits:
-
Object
- Object
- Sidekiq::Manager
- Defined in:
- lib/sidekiq/manager.rb
Overview
The main router in the system. This manages the processor state and accepts messages from Redis to be dispatched to an idle processor.
Constant Summary
Constants included from Util
Instance Attribute Summary collapse
-
#busy ⇒ Object
readonly
Returns the value of attribute busy.
-
#fetcher ⇒ Object
Returns the value of attribute fetcher.
-
#ready ⇒ Object
readonly
Returns the value of attribute ready.
Instance Method Summary collapse
- #assign(work) ⇒ Object
-
#initialize(options = {}) ⇒ Manager
constructor
A new instance of Manager.
- #processor_died(processor, reason) ⇒ Object
- #processor_done(processor) ⇒ Object
- #procline(tag) ⇒ Object
-
#real_thread(proxy_id, thr) ⇒ Object
A hack worthy of Rube Goldberg.
- #start ⇒ Object
- #stop(options = {}) ⇒ Object
- #when_done(&blk) ⇒ Object
Methods included from Actor
Methods included from Util
#hostname, #logger, #process_id, #redis, #watchdog
Methods included from ExceptionHandler
Constructor Details
#initialize(options = {}) ⇒ Manager
Returns a new instance of Manager.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sidekiq/manager.rb', line 22 def initialize(={}) logger.debug { .inspect } @count = [:concurrency] || 25 @done_callback = nil @in_progress = {} @threads = {} @done = false @busy = [] @fetcher = Fetcher.new(current_actor, ) @ready = @count.times.map do p = Processor.new_link(current_actor) p.proxy_id = p.object_id p end end |
Instance Attribute Details
#busy ⇒ Object (readonly)
Returns the value of attribute busy.
19 20 21 |
# File 'lib/sidekiq/manager.rb', line 19 def busy @busy end |
#fetcher ⇒ Object
Returns the value of attribute fetcher.
20 21 22 |
# File 'lib/sidekiq/manager.rb', line 20 def fetcher @fetcher end |
#ready ⇒ Object (readonly)
Returns the value of attribute ready.
18 19 20 |
# File 'lib/sidekiq/manager.rb', line 18 def ready @ready end |
Instance Method Details
#assign(work) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sidekiq/manager.rb', line 100 def assign(work) watchdog("Manager#assign died") do if stopped? # Race condition between Manager#stop if Fetcher # is blocked on redis and gets a message after # all the ready Processors have been stopped. # Push the message back to redis. work.requeue else processor = @ready.pop @in_progress[processor.object_id] = work @busy << processor processor.async.process(work) end end end |
#processor_died(processor, reason) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sidekiq/manager.rb', line 83 def processor_died(processor, reason) watchdog("Manager#processor_died died") do @in_progress.delete(processor.object_id) @threads.delete(processor.object_id) @busy.delete(processor) unless stopped? p = Processor.new_link(current_actor) p.proxy_id = p.object_id @ready << p dispatch else signal(:shutdown) if @busy.empty? end end end |
#processor_done(processor) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sidekiq/manager.rb', line 67 def processor_done(processor) watchdog('Manager#processor_done died') do @done_callback.call(processor) if @done_callback @in_progress.delete(processor.object_id) @threads.delete(processor.object_id) @busy.delete(processor) if stopped? processor.terminate if processor.alive? signal(:shutdown) if @busy.empty? else @ready << processor if processor.alive? end dispatch end end |
#procline(tag) ⇒ Object
125 126 127 |
# File 'lib/sidekiq/manager.rb', line 125 def procline(tag) "sidekiq #{Sidekiq::VERSION} #{tag}[#{@busy.size} of #{@count} busy]#{stopped? ? ' stopping' : ''}" end |
#real_thread(proxy_id, thr) ⇒ Object
A hack worthy of Rube Goldberg. We need to be able to hard stop a working thread. But there’s no way for us to get handle to the underlying thread performing work for a processor so we have it call us and tell us.
121 122 123 |
# File 'lib/sidekiq/manager.rb', line 121 def real_thread(proxy_id, thr) @threads[proxy_id] = thr end |
#start ⇒ Object
59 60 61 |
# File 'lib/sidekiq/manager.rb', line 59 def start @ready.each { dispatch } end |
#stop(options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/sidekiq/manager.rb', line 39 def stop(={}) watchdog('Manager#stop died') do shutdown = [:shutdown] timeout = [:timeout] @done = true Sidekiq::Fetcher.done! @fetcher.async.terminate if @fetcher.alive? logger.info { "Shutting down #{@ready.size} quiet workers" } @ready.each { |x| x.terminate if x.alive? } @ready.clear clear_worker_set return after(0) { signal(:shutdown) } if @busy.empty? hard_shutdown_in timeout if shutdown end end |
#when_done(&blk) ⇒ Object
63 64 65 |
# File 'lib/sidekiq/manager.rb', line 63 def when_done(&blk) @done_callback = blk end |