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

#hostname, #logger, #process_id, #redis, #watchdog

Methods included from ExceptionHandler

#handle_exception

Constructor Details

#initialize(options = {}) ⇒ Manager

Returns a new instance of Manager.


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

def initialize(options={})
  logger.debug { options.inspect }
  @count = options[:concurrency] || 25
  @done_callback = nil

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

Instance Method Details

#assign(work) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sidekiq/manager.rb', line 88

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


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

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


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sidekiq/manager.rb', line 59

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

#procline(tag) ⇒ Object


105
106
107
# File 'lib/sidekiq/manager.rb', line 105

def procline(tag)
  "sidekiq #{Sidekiq::VERSION} #{tag}[#{@busy.size} of #{@count} busy]#{stopped? ? ' stopping' : ''}"
end

#startObject


51
52
53
# File 'lib/sidekiq/manager.rb', line 51

def start
  @ready.each { dispatch }
end

#stop(options = {}) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sidekiq/manager.rb', line 32

def stop(options={})
  watchdog('Manager#stop died') do
    shutdown = options[:shutdown]
    timeout = options[: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

    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


55
56
57
# File 'lib/sidekiq/manager.rb', line 55

def when_done(&blk)
  @done_callback = blk
end