Class: Sidekiq::Manager

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Util
Defined in:
lib/sidekiq/manager.rb

Overview

The main router in the system. This manages the processor state and fetches messages from Redis to be dispatched to an idle processor.

Instance Method Summary collapse

Methods included from Util

#constantize, logger, #logger, logger=, process_id, #process_id, #redis, #watchdog

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq/manager.rb', line 21

def initialize(options={})
  logger.info "Booting sidekiq #{Sidekiq::VERSION} with Redis at #{redis.client.location}"
  logger.debug { options.inspect }
  @count = options[:concurrency] || 25
  @queues = options[:queues]
  @done_callback = nil

  @done = false
  @busy = []
  @ready = @count.times.map { Processor.new_link(current_actor) }
end

Instance Method Details

#processor_died(processor, reason) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/sidekiq/manager.rb', line 85

def processor_died(processor, reason)
  @busy.delete(processor)

  unless stopped?
    @ready << Processor.new_link(current_actor)
    dispatch
  end
end

#processor_done(processor) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sidekiq/manager.rb', line 71

def processor_done(processor)
  watchdog('sidekiq processor_done crashed!') do
    @done_callback.call(processor) if @done_callback
    @busy.delete(processor)
    processor.msg = processor.queue = nil
    if stopped?
      processor.terminate if processor.alive?
    else
      @ready << processor
    end
    dispatch
  end
end

#startObject



63
64
65
# File 'lib/sidekiq/manager.rb', line 63

def start
  dispatch(true)
end

#stopObject



33
34
35
36
37
38
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/sidekiq/manager.rb', line 33

def stop
  @done = true
  @ready.each(&:terminate)
  @ready.clear

  redis.with_connection do |conn|
    workers = conn.smembers('workers')
    workers.each do |name|
      conn.srem('workers', name) if name =~ /:#{process_id}-/
    end
  end

  if @busy.empty?
    return signal(:shutdown)
  end

  logger.info("Pausing 5 seconds to allow workers to finish...")
  after(5) do
    @busy.each(&:terminate)
    redis.with_connection do |conn|
      conn.multi do
        @busy.each do |busy|
          conn.lpush("queue:#{busy.queue}", busy.msg)
        end
      end
    end
    signal(:shutdown)
  end
end

#when_done(&blk) ⇒ Object



67
68
69
# File 'lib/sidekiq/manager.rb', line 67

def when_done(&blk)
  @done_callback = blk
end