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? }
]
BEAT_PAUSE =
10
RTT_READINGS =

We run the heartbeat every five seconds. Capture five samples of RTT, log a warning if each sample is above our warning threshold.

RingBuffer.new(5)
RTT_WARNING_LEVEL =
50_000
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

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#fire_event, #handle_exception, #hostname, #identity, #logger, #process_nonce, #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

#beatObject



96
97
98
99
# File 'lib/sidekiq/launcher.rb', line 96

def beat
  $0 = PROCTITLES.map { |proc| proc.call(self, to_data) }.compact.join(" ") unless @embedded
  
end

#check_rttObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/sidekiq/launcher.rb', line 202

def check_rtt
  a = b = 0
  redis do |x|
    a = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :microsecond)
    x.ping
    b = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC, :microsecond)
  end
  rtt = b - a
  RTT_READINGS << rtt
  # Ideal RTT for Redis is < 1000µs
  # Workable is < 10,000µs
  # Log a warning if it's a disaster.
  if RTT_READINGS.all? { |x| x > RTT_WARNING_LEVEL }
    logger.warn <<~EOM
      Your Redis network connection is performing extremely poorly.
      Last RTT readings were #{RTT_READINGS.buffer.inspect}, ideally these should be < 1000.
      Ensure Redis is running in the same AZ or datacenter as Sidekiq.
      If these values are close to 100,000, that means your Sidekiq process may be
      CPU-saturated; reduce your concurrency and/or see https://github.com/sidekiq/sidekiq/discussions/5039
    EOM
    RTT_READINGS.reset
  end
  rtt
end

#clear_heartbeatObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sidekiq/launcher.rb', line 101

def clear_heartbeat
  flush_stats

  # 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.
  redis do |conn|
    conn.pipelined do |pipeline|
      pipeline.srem("processes", [identity])
      pipeline.unlink("#{identity}:work")
    end
  end
rescue
  # best effort, ignore network errors
end

#flush_statsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sidekiq/launcher.rb', line 117

def flush_stats
  fails = Processor::FAILURE.reset
  procd = Processor::PROCESSED.reset
  return if fails + procd == 0

  nowdate = Time.now.utc.strftime("%Y-%m-%d")
  begin
    redis do |conn|
      conn.pipelined do |pipeline|
        pipeline.incrby("stat:processed", procd)
        pipeline.incrby("stat:processed:#{nowdate}", procd)
        pipeline.expire("stat:processed:#{nowdate}", STATS_TTL)

        pipeline.incrby("stat:failed", fails)
        pipeline.incrby("stat:failed:#{nowdate}", fails)
        pipeline.expire("stat:failed:#{nowdate}", STATS_TTL)
      end
    end
  rescue => ex
    logger.warn("Unable to flush stats: #{ex}")
  end
end

#heartbeatObject

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



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

def heartbeat
  
end

#memory_usage(pid) ⇒ Object



243
244
245
# File 'lib/sidekiq/launcher.rb', line 243

def memory_usage(pid)
  MEMORY_GRABBER.call(pid)
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

#start_heartbeatObject



88
89
90
91
92
93
94
# File 'lib/sidekiq/launcher.rb', line 88

def start_heartbeat
  loop do
    beat
    sleep BEAT_PAUSE
  end
  logger.info("Heartbeat stopping...")
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
# 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
end

#stopping?Boolean

Returns:

  • (Boolean)


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

def stopping?
  @done
end

#to_dataObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/sidekiq/launcher.rb', line 247

def to_data
  @data ||= {
    "hostname" => hostname,
    "started_at" => Time.now.to_f,
    "pid" => ::Process.pid,
    "tag" => @config[:tag] || "",
    "concurrency" => @config.total_concurrency,
    "queues" => @config.capsules.values.flat_map { |cap| cap.queues }.uniq,
    "weights" => to_weights,
    "labels" => @config[:labels].to_a,
    "identity" => identity,
    "version" => Sidekiq::VERSION,
    "embedded" => @embedded
  }
end

#to_jsonObject



267
268
269
270
271
# File 'lib/sidekiq/launcher.rb', line 267

def to_json
  # this data changes infrequently so dump it to a string
  # now so we don't need to dump it every heartbeat.
  @json ||= Sidekiq.dump_json(to_data)
end

#to_weightsObject



263
264
265
# File 'lib/sidekiq/launcher.rb', line 263

def to_weights
  @config.capsules.values.map(&:weights)
end

#Object



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
187
188
189
190
191
192
193
194
# File 'lib/sidekiq/launcher.rb', line 140

def 
  key = identity
  fails = procd = 0

  begin
    flush_stats

    curstate = Processor::WORK_STATE.dup
    curstate.transform_values! { |val| Sidekiq.dump_json(val) }

    redis do |conn|
      # work is the current set of executing jobs
      work_key = "#{key}:work"
      conn.multi do |transaction|
        transaction.unlink(work_key)
        if curstate.size > 0
          transaction.hset(work_key, curstate)
          transaction.expire(work_key, 60)
        end
      end
    end

    rtt = check_rtt

    fails = procd = 0
    kb = memory_usage(::Process.pid)

    _, exists, _, _, signal = redis { |conn|
      conn.multi { |transaction|
        transaction.sadd("processes", [key])
        transaction.exists(key)
        transaction.hset(key, "info", to_json,
          "busy", curstate.size,
          "beat", Time.now.to_f,
          "rtt_us", rtt,
          "quiet", @done.to_s,
          "rss", kb)
        transaction.expire(key, 60)
        transaction.rpop("#{key}-signals")
      }
    }

    # first heartbeat or recovering from an outage and need to reestablish our heartbeat
    fire_event(:heartbeat) unless exists > 0
    fire_event(:beat, oneshot: false)

    ::Process.kill(signal, ::Process.pid) if signal && !@embedded
  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