Class: Sidekiq::Launcher
- Inherits:
-
Object
- Object
- Sidekiq::Launcher
- Includes:
- Util
- Defined in:
- lib/sidekiq/launcher.rb
Overview
The Launcher is a very simple Actor whose job is to start, monitor and stop the core Actors in Sidekiq. If any of these actors die, the Sidekiq process exits immediately.
Constant Summary
Constants included from Util
Instance Attribute Summary collapse
-
#fetcher ⇒ Object
Returns the value of attribute fetcher.
-
#manager ⇒ Object
Returns the value of attribute manager.
-
#poller ⇒ Object
Returns the value of attribute poller.
Instance Method Summary collapse
- #clear_heartbeat ⇒ Object
- #heartbeat ⇒ Object
-
#initialize(options) ⇒ Launcher
constructor
A new instance of Launcher.
-
#quiet ⇒ Object
Stops this instance from processing any more jobs,.
- #run ⇒ Object
- #start_heartbeat ⇒ Object
-
#stop ⇒ Object
Shuts down the process.
- #stopping? ⇒ Boolean
- #to_data ⇒ Object
- #to_json ⇒ Object
- #❤ ⇒ Object
Methods included from Util
#fire_event, #hostname, #identity, #logger, #process_nonce, #redis, #safe_thread, #watchdog
Methods included from ExceptionHandler
Constructor Details
Instance Attribute Details
#fetcher ⇒ Object
Returns the value of attribute fetcher.
15 16 17 |
# File 'lib/sidekiq/launcher.rb', line 15 def fetcher @fetcher end |
#manager ⇒ Object
Returns the value of attribute manager.
15 16 17 |
# File 'lib/sidekiq/launcher.rb', line 15 def manager @manager end |
#poller ⇒ Object
Returns the value of attribute poller.
15 16 17 |
# File 'lib/sidekiq/launcher.rb', line 15 def poller @poller end |
Instance Method Details
#clear_heartbeat ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sidekiq/launcher.rb', line 152 def clear_heartbeat # Remove record from Redis since we are shutting down. # Note we don't stop the heartbeat thread; if the process # doesn't actually exit, it'll reappear in the Web UI. Sidekiq.redis do |conn| conn.pipelined do conn.srem('processes', identity) conn.del("#{identity}:workers") end end rescue # best effort, ignore network errors end |
#heartbeat ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/sidekiq/launcher.rb', line 64 def heartbeat results = Sidekiq::CLI::PROCTITLES.map {|x| x.(self, to_data) } results.compact! $0 = results.join(' ') ❤ end |
#quiet ⇒ Object
Stops this instance from processing any more jobs,
32 33 34 35 36 |
# File 'lib/sidekiq/launcher.rb', line 32 def quiet @done = true @manager.quiet @poller.terminate end |
#run ⇒ Object
24 25 26 27 28 |
# File 'lib/sidekiq/launcher.rb', line 24 def run @thread = safe_thread("heartbeat", &method(:start_heartbeat)) @poller.start @manager.start end |
#start_heartbeat ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/sidekiq/launcher.rb', line 121 def start_heartbeat while true heartbeat sleep 5 end Sidekiq.logger.info("Heartbeat stopping...") end |
#stop ⇒ Object
Shuts down the process. This method does not return until all work is complete and cleaned up. It can take up to the timeout to complete.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sidekiq/launcher.rb', line 41 def stop deadline = Time.now + @options[:timeout] @done = true @manager.quiet @poller.terminate @manager.stop(deadline) # Requeue everything in case there was a worker who grabbed work while stopped # This call is a no-op in Sidekiq but necessary for Sidekiq Pro. strategy = (@options[:fetch] || Sidekiq::BasicFetch) strategy.bulk_requeue([], @options) clear_heartbeat end |
#stopping? ⇒ Boolean
58 59 60 |
# File 'lib/sidekiq/launcher.rb', line 58 def stopping? @done end |
#to_data ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sidekiq/launcher.rb', line 129 def to_data @data ||= begin { 'hostname' => hostname, 'started_at' => Time.now.to_f, 'pid' => $$, 'tag' => @options[:tag] || '', 'concurrency' => @options[:concurrency], 'queues' => @options[:queues].uniq, 'labels' => @options[:labels], 'identity' => identity, } end end |
#to_json ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/sidekiq/launcher.rb', line 144 def to_json @json ||= begin # this data changes infrequently so dump it to a string # now so we don't need to dump it every heartbeat. Sidekiq.dump_json(to_data) end end |
#❤ ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sidekiq/launcher.rb', line 72 def ❤ key = identity fails = procd = 0 begin Processor::FAILURE.update {|curr| fails = curr; 0 } Processor::PROCESSED.update {|curr| procd = curr; 0 } workers_key = "#{key}:workers".freeze nowdate = Time.now.utc.strftime("%Y-%m-%d".freeze) Sidekiq.redis do |conn| conn.multi do conn.incrby("stat:processed".freeze, procd) conn.incrby("stat:processed:#{nowdate}", procd) conn.incrby("stat:failed".freeze, fails) conn.incrby("stat:failed:#{nowdate}", fails) conn.del(workers_key) Processor::WORKER_STATE.each_pair do |tid, hash| conn.hset(workers_key, tid, Sidekiq.dump_json(hash)) end conn.expire(workers_key, 60) end end fails = procd = 0 _, exists, _, _, msg = Sidekiq.redis do |conn| conn.multi do conn.sadd('processes', key) conn.exists(key) conn.hmset(key, 'info', to_json, 'busy', Processor::WORKER_STATE.size, 'beat', Time.now.to_f, 'quiet', @done) conn.expire(key, 60) conn.rpop("#{key}-signals") end end # first heartbeat or recovering from an outage and need to reestablish our heartbeat fire_event(:heartbeat) if !exists return unless msg ::Process.kill(msg, $$) rescue => e # ignore all redis/network issues logger.error("heartbeat: #{e.}") # don't lose the counts if there was a network issue Processor::PROCESSED.increment(procd) Processor::FAILURE.increment(fails) end end |