Skip to content

Commit

Permalink
Ensure CLI can shutdown cleanly with multiple queues and timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Aug 4, 2021
1 parent 4152008 commit cd89ef8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def self.restart(timeout: -1)
_shutdown_all(_executables, :restart, timeout: timeout)
end

# Sends +#shutdown+ or +#restart+ to executable objects ({GoodJob::Notifier}, {GoodJob::Poller}, {GoodJob::Scheduler})
# @param executables [Array<Notifier, Poller, Scheduler, MultiScheduler>] Objects to shut down.
# Sends +#shutdown+ or +#restart+ to executable objects ({GoodJob::Notifier}, {GoodJob::Poller}, {GoodJob::Scheduler}, {GoodJob::MultiScheduler}, {GoodJob::CronManager})
# @param executables [Array<Notifier, Poller, Scheduler, MultiScheduler, CronManager>] Objects to shut down.
# @param method_name [:symbol] Method to call, e.g. +:shutdown+ or +:restart+.
# @param timeout [nil,Numeric]
# @return [void]
def self._shutdown_all(executables, method_name = :shutdown, timeout: -1)
if timeout.positive?
if timeout.is_a?(Numeric) && timeout.positive?
executables.each { |executable| executable.send(method_name, timeout: nil) }

stop_at = Time.current + timeout
Expand Down
20 changes: 18 additions & 2 deletions spec/lib/good_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
require 'rails_helper'

describe GoodJob do
let(:configuration) { GoodJob::Configuration.new({ queues: 'mice:1', poll_interval: -1 }) }
let!(:scheduler) { GoodJob::Scheduler.from_configuration(configuration) }
let(:configuration) { GoodJob::Configuration.new({ queues: 'mice:1;elephants:1', poll_interval: -1 }) }
let!(:multi_scheduler) { GoodJob::Scheduler.from_configuration(configuration) }
let(:scheduler) { multi_scheduler.schedulers.first }
let!(:notifier) { GoodJob::Notifier.new([scheduler, :create_thread]) }

describe '.shutdown' do
Expand All @@ -13,6 +14,13 @@
expect(scheduler.shutdown?).to eq true
expect(notifier.shutdown?).to eq true
end

it 'shuts down when there is a timeout set' do
described_class.shutdown(timeout: 5)

expect(scheduler.shutdown?).to eq true
expect(notifier.shutdown?).to eq true
end
end

describe '.shutdown?' do
Expand All @@ -33,6 +41,14 @@
end
end

describe '._shutdown_all' do
it 'delegates shutdown methods to multiple instances' do
expect do
described_class._shutdown_all([multi_scheduler, notifier], timeout: 5)
end.to change(described_class, :shutdown?).from(false).to(true)
end
end

describe '.reperform_jobs_on_standard_error' do
around do |example|
original_retry_on_unhandled_error = described_class.retry_on_unhandled_error
Expand Down

0 comments on commit cd89ef8

Please sign in to comment.