diff --git a/lib/good_job.rb b/lib/good_job.rb index 367f03cb4..1995888c3 100644 --- a/lib/good_job.rb +++ b/lib/good_job.rb @@ -131,7 +131,7 @@ def self.shutdown? # When forking processes you should shut down these background threads before forking, and restart them after forking. # For example, you should use +shutdown+ and +restart+ when using async execution mode with Puma. # See the {file:README.md#executing-jobs-async--in-process} for more explanation and examples. - # @param timeout [Numeric, nil] Seconds to wait for active threads to finish. + # @param timeout [Numeric] Seconds to wait for active threads to finish. # @return [void] def self.restart(timeout: -1) _shutdown_all(Capsule.instances, :restart, timeout: timeout) diff --git a/lib/good_job/capsule.rb b/lib/good_job/capsule.rb index a74d17673..c504c96e2 100644 --- a/lib/good_job/capsule.rb +++ b/lib/good_job/capsule.rb @@ -55,9 +55,11 @@ def shutdown(timeout: :default) end # Shutdown and then start the capsule again. - # @param timeout [nil, Numeric, Symbol] Seconds to wait for active threads. + # @param timeout [Numeric, Symbol] Seconds to wait for active threads. # @return [void] def restart(timeout: :default) + raise ArgumentError, "Capsule#restart cannot be called with a timeout of nil" if timeout.nil? + shutdown(timeout: timeout) start end diff --git a/lib/good_job/scheduler.rb b/lib/good_job/scheduler.rb index cba1eb023..500f2d150 100644 --- a/lib/good_job/scheduler.rb +++ b/lib/good_job/scheduler.rb @@ -132,11 +132,13 @@ def shutdown(timeout: -1) # Restart the Scheduler. # When shutdown, start; or shutdown and start. - # @param timeout [nil, Numeric] Seconds to wait for actively executing jobs to finish; shares same values as {#shutdown}. + # @param timeout [Numeric] Seconds to wait for actively executing jobs to finish; shares same values as {#shutdown}. # @return [void] def restart(timeout: -1) + raise ArgumentError, "Scheduler#restart cannot be called with a timeout of nil" if timeout.nil? + instrument("scheduler_restart_pools") do - shutdown(timeout: timeout) if running? + shutdown(timeout: timeout) create_executor warm_cache end diff --git a/spec/lib/good_job/scheduler_spec.rb b/spec/lib/good_job/scheduler_spec.rb index 6bb0d3fac..8bdec7b7c 100644 --- a/spec/lib/good_job/scheduler_spec.rb +++ b/spec/lib/good_job/scheduler_spec.rb @@ -114,6 +114,17 @@ expect { scheduler.restart } .to change(scheduler, :running?).from(false).to(true) end + + it 'can be called multiple times' do + scheduler = described_class.new(performer) + scheduler.shutdown + expect do + scheduler.restart + scheduler.restart + scheduler.restart + end.not_to raise_error + scheduler.shutdown + end end describe '#create_thread' do @@ -144,13 +155,11 @@ it 'uses state[:count] to create multiple threads' do job_performer = instance_double(GoodJob::JobPerformer, next: nil, next?: true, name: '', next_at: [], cleanup: nil) scheduler = described_class.new(job_performer, max_threads: 1) + allow(scheduler).to receive(:create_task) result = scheduler.create_thread({ count: 10 }) - if result - expect(job_performer).to have_received(:next).exactly(10).times - else - expect(job_performer).to have_received(:next).at_most(9).times - end + expect(result).to be true + expect(scheduler).to have_received(:create_task).exactly(10).times end end diff --git a/spec/lib/good_job_spec.rb b/spec/lib/good_job_spec.rb index ab18d81d8..82623f856 100644 --- a/spec/lib/good_job_spec.rb +++ b/spec/lib/good_job_spec.rb @@ -13,7 +13,7 @@ end describe '.shutdown?' do - it 'shuts down all scheduler and notifier instances' do + it 'returns whether any capsules are running' do expect do capsule = GoodJob::Capsule.new(configuration: configuration) capsule.start