Class: Sidekiq::Killer::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/killer/memory.rb

Constant Summary collapse

MUTEX =

Create a mutex used to ensure there will be only one thread waiting to shut Sidekiq down

Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Memory

Returns a new instance of Memory.



6
7
8
9
10
11
12
13
# File 'lib/sidekiq/killer/memory.rb', line 6

def initialize(options = {})
  options.stringify_keys!

  @max_rss         = (options["max_rss"]         || 0).to_s.to_i
  @grace_time      = (options["grace_time"]      || 15 * 60).to_s.to_i
  @shutdown_wait   = (options["shutdown_wait"]   || 30).to_s.to_i
  @shutdown_signal = (options["shutdown_signal"] || "SIGKILL").to_s
end

Instance Method Details

#call(worker, job, queue) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sidekiq/killer/memory.rb', line 19

def call(worker, job, queue)
  yield
  current_rss = get_rss

  return unless @max_rss > 0 && current_rss > @max_rss

  Thread.new do
    # Return if another thread is already waiting to shut Sidekiq down
    return unless MUTEX.try_lock

    Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\
      "#{@max_rss}"
    Sidekiq.logger.warn "this thread will shut down PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"\
      "in #{@grace_time} seconds"
    sleep(@grace_time)

    Sidekiq.logger.warn "sending SIGTERM to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
    ::Process.kill("SIGTERM", ::Process.pid)

    Sidekiq.logger.warn "waiting #{@shutdown_wait} seconds before sending "\
      "#{@shutdown_signal} to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
    sleep(@shutdown_wait)

    Sidekiq.logger.warn "sending #{@shutdown_signal} to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
    ::Process.kill(@shutdown_signal, ::Process.pid)
  end
end