Class: Sidekiq::WorkerKiller

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/sidekiq/worker_killer.rb,
lib/sidekiq/worker_killer/version.rb

Overview

Sidekiq server middleware. Kill worker when the RSS memory exceeds limit after a given grace time.

Constant Summary collapse

VERSION =
"0.4.0".freeze
MUTEX =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WorkerKiller

Returns a new instance of WorkerKiller.



12
13
14
15
16
17
18
# File 'lib/sidekiq/worker_killer.rb', line 12

def initialize(options = {})
  @max_rss         = options.fetch(:max_rss, 0)
  @grace_time      = options.fetch(:grace_time, 15 * 60)
  @shutdown_wait   = options.fetch(:shutdown_wait, 30)
  @kill_signal     = options.fetch(:kill_signal, "SIGKILL")
  @gc              = options.fetch(:gc, true)
end

Instance Method Details

#call(_worker, _job, _queue) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq/worker_killer.rb', line 20

def call(_worker, _job, _queue)
  yield
  # Skip if the max RSS is not exceeded
  return unless @max_rss > 0
  return unless current_rss > @max_rss
  GC.start(full_mark: true, immediate_sweep: true) if @gc
  return unless current_rss > @max_rss
  # Launch the shutdown process
  warn "current RSS #{current_rss} of #{identity} exceeds " \
       "maximum RSS #{@max_rss}"
  request_shutdown
end