diff --git a/lib/active_job/queue_adapters/delayed_job_adapter.rb b/lib/active_job/queue_adapters/delayed_job_adapter.rb index d23d7ef4..ecfd7091 100644 --- a/lib/active_job/queue_adapters/delayed_job_adapter.rb +++ b/lib/active_job/queue_adapters/delayed_job_adapter.rb @@ -20,6 +20,10 @@ def enqueue_at(job, timestamp) delayed_job end + def stopping? + Delayed::Worker.stop? + end + class JobWrapper attr_accessor :job_data diff --git a/lib/delayed/worker.rb b/lib/delayed/worker.rb index 7b983a20..f7786eb9 100644 --- a/lib/delayed/worker.rb +++ b/lib/delayed/worker.rb @@ -53,7 +53,7 @@ def self.reset # (perhaps to inspect the reason for the failure), set this to false. self.destroy_failed_jobs = true - # By default, Signals INT and TERM set @exit, and the worker exits upon completion of the current job. + # By default, Signals INT and TERM set @@exit, and the worker exits upon completion of the current job. # If you would prefer to raise a SignalException and exit immediately you can use this. # Be aware daemons uses TERM to stop and restart # false - No exceptions will be raised @@ -196,14 +196,22 @@ def start # rubocop:disable CyclomaticComplexity, PerceivedComplexity end end - def stop + def self.stop @exit = true end - def stop? + def stop + self.class.stop + end + + def self.stop? !!@exit end + def stop? + self.class.stop? + end + # Do num jobs and return stats on success/failure. # Exit early if interrupted. def work_off(num = 100) diff --git a/spec/active_job_adapter_spec.rb b/spec/active_job_adapter_spec.rb index ba1ca5ac..32d62206 100644 --- a/spec/active_job_adapter_spec.rb +++ b/spec/active_job_adapter_spec.rb @@ -107,4 +107,12 @@ def perform(message) expect(JobBuffer.values).to eq(%w[2 1]) end + + it 'adapter stopping? reflects worker stop?' do + adapter = ActiveJob::QueueAdapters::DelayedJobAdapter.new + + expect(adapter.stopping?).to eq(false) + worker.stop + expect(adapter.stopping?).to eq(true) + end end