Class: Sidekiq::Launcher
- Inherits:
-
Object
- Object
- Sidekiq::Launcher
- Includes:
- Util
- Defined in:
- lib/sidekiq/launcher.rb
Overview
The Launcher starts the Manager and Poller threads 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::WORKER_STATE.size} of #{data["concurrency"]} busy]" }, proc { |me, data| "stopping" if me.stopping? } ]
- MEMORY_GRABBER =
case RUBY_PLATFORM when /linux/ ->(pid) { IO.readlines("/proc/#{$$}/status").each do |line| next unless line.start_with?("VmRSS:") break line.split[1].to_i end } when /darwin|bsd/ ->(pid) { `ps -o pid,rss -p #{pid}`.lines.last.split.last.to_i } else ->(pid) { 0 } end
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.
Class Method Summary collapse
Instance Method Summary collapse
- #clear_heartbeat ⇒ Object
- #heartbeat ⇒ Object
-
#initialize(options) ⇒ Launcher
constructor
A new instance of Launcher.
- #memory_usage(pid) ⇒ Object
-
#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, #tid, #watchdog
Methods included from ExceptionHandler
Constructor Details
Instance Attribute Details
#fetcher ⇒ Object
Returns the value of attribute fetcher.
22 23 24 |
# File 'lib/sidekiq/launcher.rb', line 22 def fetcher @fetcher end |
#manager ⇒ Object
Returns the value of attribute manager.
22 23 24 |
# File 'lib/sidekiq/launcher.rb', line 22 def manager @manager end |
#poller ⇒ Object
Returns the value of attribute poller.
22 23 24 |
# File 'lib/sidekiq/launcher.rb', line 22 def poller @poller end |
Class Method Details
.flush_stats ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sidekiq/launcher.rb', line 100 def self.flush_stats fails = Processor::FAILURE.reset procd = Processor::PROCESSED.reset return if fails + procd == 0 nowdate = Time.now.utc.strftime("%Y-%m-%d") begin Sidekiq.redis do |conn| conn.pipelined do conn.incrby("stat:processed", procd) conn.incrby("stat:processed:#{nowdate}", procd) conn.expire("stat:processed:#{nowdate}", STATS_TTL) conn.incrby("stat:failed", fails) conn.incrby("stat:failed:#{nowdate}", fails) conn.expire("stat:failed:#{nowdate}", STATS_TTL) end end rescue => ex # we're exiting the process, things might be shut down so don't # try to handle the exception Sidekiq.logger.warn("Unable to flush stats: #{ex}") end end |
Instance Method Details
#clear_heartbeat ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sidekiq/launcher.rb', line 80 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.unlink("#{identity}:workers") end end rescue # best effort, ignore network errors end |
#heartbeat ⇒ Object
94 95 96 97 98 |
# File 'lib/sidekiq/launcher.rb', line 94 def heartbeat $0 = PROCTITLES.map { |proc| proc.call(self, to_data) }.compact.join(" ") ❤ end |
#memory_usage(pid) ⇒ Object
204 205 206 |
# File 'lib/sidekiq/launcher.rb', line 204 def memory_usage(pid) MEMORY_GRABBER.call(pid) end |
#quiet ⇒ Object
Stops this instance from processing any more jobs,
40 41 42 43 44 |
# File 'lib/sidekiq/launcher.rb', line 40 def quiet @done = true @manager.quiet @poller.terminate end |
#run ⇒ Object
32 33 34 35 36 |
# File 'lib/sidekiq/launcher.rb', line 32 def run @thread = safe_thread("heartbeat", &method(:start_heartbeat)) @poller.start @manager.start end |
#start_heartbeat ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/sidekiq/launcher.rb', line 72 def start_heartbeat loop do 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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sidekiq/launcher.rb', line 49 def stop deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + @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] strategy.bulk_requeue([], @options) clear_heartbeat end |
#stopping? ⇒ Boolean
66 67 68 |
# File 'lib/sidekiq/launcher.rb', line 66 def stopping? @done end |
#to_data ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/sidekiq/launcher.rb', line 208 def to_data @data ||= begin { "hostname" => hostname, "started_at" => Time.now.to_f, "pid" => ::Process.pid, "tag" => @options[:tag] || "", "concurrency" => @options[:concurrency], "queues" => @options[:queues].uniq, "labels" => @options[:labels], "identity" => identity } end end |
#to_json ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/sidekiq/launcher.rb', line 223 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/sidekiq/launcher.rb', line 126 def ❤ key = identity fails = procd = 0 begin fails = Processor::FAILURE.reset procd = Processor::PROCESSED.reset curstate = Processor::WORKER_STATE.dup workers_key = "#{key}:workers" nowdate = Time.now.utc.strftime("%Y-%m-%d") Sidekiq.redis do |conn| conn.multi do conn.incrby("stat:processed", procd) conn.incrby("stat:processed:#{nowdate}", procd) conn.expire("stat:processed:#{nowdate}", STATS_TTL) conn.incrby("stat:failed", fails) conn.incrby("stat:failed:#{nowdate}", fails) conn.expire("stat:failed:#{nowdate}", STATS_TTL) conn.unlink(workers_key) curstate.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 kb = memory_usage(::Process.pid) _, exists, _, _, msg = Sidekiq.redis { |conn| conn.multi { conn.sadd("processes", key) conn.exists?(key) conn.hmset(key, "info", to_json, "busy", curstate.size, "beat", Time.now.to_f, "quiet", @done, "rss", kb) conn.expire(key, 60) conn.rpop("#{key}-signals") } } # first heartbeat or recovering from an outage and need to reestablish our heartbeat fire_event(:heartbeat) unless exists return unless msg ::Process.kill(msg, ::Process.pid) rescue => e # ignore all redis/network issues logger.error("heartbeat: #{e}") # don't lose the counts if there was a network issue Processor::PROCESSED.incr(procd) Processor::FAILURE.incr(fails) end end |