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 accepts messages from Redis to be dispatched to an idle processor.

Constant Summary

Constants included from Util

Util::EXPIRY

Instance Method Summary collapse

Methods included from Util

#constantize, #logger, #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
32
33
34
# File 'lib/sidekiq/manager.rb', line 21

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

  @in_progress = {}
  @done = false
  @busy = []
  @fetcher = Fetcher.new(current_actor, options[:queues])
  @ready = @count.times.map { Processor.new_link(current_actor) }
  procline
end

Instance Method Details

#assign(msg, queue) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sidekiq/manager.rb', line 100

def assign(msg, queue)
  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.
      Sidekiq.redis do |conn|
        conn.lpush("queue:#{queue}", msg)
      end
    else
      processor = @ready.pop
      @in_progress[processor.object_id] = [msg, queue]
      @busy << processor
      processor.process!(Sidekiq.load_json(msg), queue)
    end
  end
end

#processor_died(processor, reason) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sidekiq/manager.rb', line 86

def processor_died(processor, reason)
  watchdog("Manager#processor_died died") do
    @in_progress.delete(processor.object_id)
    @busy.delete(processor)

    unless stopped?
      @ready << Processor.new_link(current_actor)
      dispatch
    else
      signal(:shutdown) if @busy.empty?
    end
  end
end

#processor_done(processor) ⇒ Object



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

def processor_done(processor)
  watchdog('Manager#processor_done died') do
    @done_callback.call(processor) if @done_callback
    @in_progress.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

#startObject



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

def start
  @ready.each { dispatch }
end

#stop(options = {}) ⇒ Object



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 36

def stop(options={})
  watchdog('Manager#stop died') do
    shutdown = options[:shutdown]
    timeout = options[:timeout]

    @done = true
    Sidekiq::Fetcher.done!
    @fetcher.terminate! if @fetcher.alive?

    logger.info { "Shutting down #{@ready.size} quiet workers" }
    @ready.each { |x| x.terminate if x.alive? }
    @ready.clear

    logger.debug { "Clearing workers in redis" }
    Sidekiq.redis do |conn|
      workers = conn.smembers('workers')
      workers.each do |name|
        conn.srem('workers', name) if name =~ /:#{process_id}-/
      end
    end

    return after(0) { signal(:shutdown) } if @busy.empty?
    logger.info { "Pausing up to #{timeout} seconds to allow workers to finish..." }
    hard_shutdown_in timeout if 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