Class: Sidekiq::Launcher

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/sidekiq/launcher.rb

Overview

The Launcher starts the Capsule Managers, the Poller thread and provides the process heartbeat.

Constant Summary collapse

STATS_TTL =

5 years

5 * 365 * 24 * 60 * 60
PROCTITLES =
[
  proc { "sidekiq" },
  proc { Sidekiq::VERSION },
  proc { |me, data| data["tag"] },
  proc { |me, data| "[#{Processor::WORK_STATE.size} of #{me.config.total_concurrency} busy]" },
  proc { |me, data| "stopping" if me.stopping? }
]

Instance Attribute Summary collapse

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#default_tag, #fire_event, #handle_exception, #hostname, #identity, #inspect, #logger, #mono_ms, #process_nonce, #real_ms, #redis, #safe_thread, #tid, #watchdog

Constructor Details

#initialize(config, embedded: false) ⇒ Launcher

Returns a new instance of Launcher.



25
26
27
28
29
30
31
32
33
# File 'lib/sidekiq/launcher.rb', line 25

def initialize(config, embedded: false)
  @config = config
  @embedded = embedded
  @managers = config.capsules.values.map do |cap|
    Sidekiq::Manager.new(cap)
  end
  @poller = Sidekiq::Scheduled::Poller.new(@config)
  @done = false
end

Instance Attribute Details

#managersObject

Returns the value of attribute managers.



23
24
25
# File 'lib/sidekiq/launcher.rb', line 23

def managers
  @managers
end

#pollerObject

Returns the value of attribute poller.



23
24
25
# File 'lib/sidekiq/launcher.rb', line 23

def poller
  @poller
end

Instance Method Details

#heartbeatObject

If embedding Sidekiq, you can have the process heartbeat call this method to regularly heartbeat rather than creating a separate thread.



81
82
83
# File 'lib/sidekiq/launcher.rb', line 81

def heartbeat
  
end

#quietObject

Stops this instance from processing any more jobs,



47
48
49
50
51
52
53
54
# File 'lib/sidekiq/launcher.rb', line 47

def quiet
  return if @done

  @done = true
  @managers.each(&:quiet)
  @poller.terminate
  fire_event(:quiet, reverse: true)
end

#run(async_beat: true) ⇒ Object

Start this Sidekiq instance. If an embedding process already has a heartbeat thread, caller can use ‘async_beat: false` and instead have thread call Launcher#heartbeat every N seconds.



38
39
40
41
42
43
44
# File 'lib/sidekiq/launcher.rb', line 38

def run(async_beat: true)
  logger.debug { @config.merge!({}) }
  Sidekiq.freeze!
  @thread = safe_thread("heartbeat", &method(:start_heartbeat)) if async_beat
  @poller.start
  @managers.each(&:start)
end

#stopObject

Shuts down this Sidekiq instance. Waits up to the deadline for all jobs to complete.



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

def stop
  deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + @config[:timeout]

  quiet
  stoppers = @managers.map do |mgr|
    Thread.new do
      mgr.stop(deadline)
    end
  end

  fire_event(:shutdown, reverse: true)
  stoppers.each(&:join)

  clear_heartbeat
  fire_event(:exit, reverse: true)
end

#stopping?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sidekiq/launcher.rb', line 74

def stopping?
  @done
end