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, #redis, #watchdog

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.



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

def initialize(options={})
  logger.info "Booting sidekiq #{Sidekiq::VERSION} with Redis at #{redis {|x| x.client.location}}"
  logger.info "Running in #{RUBY_DESCRIPTION}"
  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



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

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

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

#processor_done(processor) ⇒ Object



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

def processor_done(processor)
  watchdog('sidekiq processor_done crashed!') do
    @done_callback.call(processor) if @done_callback
    @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



65
66
67
# File 'lib/sidekiq/manager.rb', line 65

def start
  dispatch(true)
end

#stop(options = {}) ⇒ Object



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
62
63
# File 'lib/sidekiq/manager.rb', line 35

def stop(options={})
  shutdown = options[:shutdown]
  timeout = options[:timeout]

  @done = true
  @ready.each { |x| x.terminate if x.alive? }
  @ready.clear

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

  if shutdown
    if @busy.empty?
      # after(0) needed to avoid deadlock in Celluoid after USR1 + TERM
      return after(0) { signal(:shutdown) }
    else
      logger.info { "Pausing #{timeout} seconds to allow workers to finish..." }
    end

    after(timeout) do
      @busy.each { |x| x.terminate if x.alive? }
      signal(:shutdown)
    end
  end
end

#when_done(&blk) ⇒ Object



69
70
71
# File 'lib/sidekiq/manager.rb', line 69

def when_done(&blk)
  @done_callback = blk
end