Skip to content

Commit

Permalink
This feature adds skip_shutdown_if option to be defined
Browse files Browse the repository at this point in the history
  • Loading branch information
msxavi committed Oct 23, 2019
1 parent 056ff81 commit 882a72e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/sidekiq/worker_killer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ def initialize(options = {})
@shutdown_wait = options.fetch(:shutdown_wait, 30)
@kill_signal = options.fetch(:kill_signal, "SIGKILL")
@gc = options.fetch(:gc, true)
@skip_shutdown = options.fetch(:skip_shutdown_if, nil)
end

def call(_worker, _job, _queue)
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
if @skip_shutdown && @skip_shutdown.call(worker, job, queue)
warn "#{worker.class} exceeds maximum RSS #{@max_rss}, however shutdown will be ignored"
return
end

warn "current RSS #{current_rss} of #{identity} exceeds " \
"maximum RSS #{@max_rss}"
request_shutdown
Expand Down
29 changes: 29 additions & 0 deletions spec/sidekiq/worker_killer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@
expect(GC).to receive(:start).with(full_mark: true, immediate_sweep: true)
subject.call(worker, job, queue){}
end

context "when skip_shutdown_if is given" do
subject{ described_class.new(max_rss: 2, skip_shutdown_if: skip_shutdown_proc) }

context "and skip_shutdown_if is a proc" do
let(:skip_shutdown_proc) { proc { |worker| true } }
it "should NOT request shutdown" do
expect(subject).not_to receive(:request_shutdown)
subject.call(worker, job, queue){}
end
end

context "and skip_shutdown_if is a lambda" do
let(:skip_shutdown_proc) { ->(worker, job, queue) { true } }
it "should NOT request shutdown" do
expect(subject).not_to receive(:request_shutdown)
subject.call(worker, job, queue){}
end
end

context "and skip_shutdown_if returns false" do
let(:skip_shutdown_proc) { proc { |worker, job, queue| false } }
it "should still request shutdown" do
expect(subject).to receive(:request_shutdown)
subject.call(worker, job, queue){}
end
end
end

context "when gc is false" do
subject{ described_class.new(max_rss: 2, gc: false) }
it "should not call garbage collect" do
Expand Down

0 comments on commit 882a72e

Please sign in to comment.