Skip to content

Commit d2b619f

Browse files
authored
Introduce max_run_time handling in WrappingJob and DelayedWorker (#3880)
This commit adds support for specifying the delayed_job max_run_time parameter, addressing the previous lack of explicit control which resulted in jobs defaulting to a 4h timeout. In `WrappingJob`, `max_run_time` is now delegated to the handler if available, ensuring delayed_job does not use the 4h default. For `DelayedWorker`, a global `max_run_time` setting is derived from application configuration, correcting the unintended 4h default timeout. Unit tests confirm the correct application of `max_run_time`.
1 parent b69dcdf commit d2b619f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

app/jobs/wrapping_job.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def max_attempts
3939
handler.respond_to?(:max_attempts) ? handler.max_attempts : 1
4040
end
4141

42+
def max_run_time
43+
handler.max_run_time if handler.respond_to?(:max_run_time)
44+
end
45+
4246
def reschedule_at(time, attempts)
4347
handler.reschedule_at(time, attempts) if handler.respond_to?(:reschedule_at)
4448
end

lib/tasks/jobs.rake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ namespace :jobs do
7474
setup_app_log_emitter(config, logger)
7575
Delayed::Worker.destroy_failed_jobs = false
7676
Delayed::Worker.max_attempts = 3
77+
Delayed::Worker.max_run_time = config.get(:jobs, :global, :timeout_in_seconds) + 1
7778
Delayed::Worker.logger = logger
7879
worker = Delayed::Worker.new(@queue_options)
7980
worker.name = @queue_options[:worker_name]

spec/unit/jobs/wrapping_job_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,25 @@ module Jobs
103103
end
104104
end
105105

106+
describe '#max_run_time' do
107+
let(:handler_with_method) { double('Job', max_run_time: 12) }
108+
let(:handler_without_method) { Object.new }
109+
110+
context 'when the handler implements max_run_time' do
111+
it 'returns the max_run_time from the handler' do
112+
job = WrappingJob.new(handler_with_method)
113+
expect(job.max_run_time).to eq(12)
114+
end
115+
end
116+
117+
context 'when the handler does not implement max_run_time' do
118+
it 'returns nil' do
119+
job = WrappingJob.new(handler_without_method)
120+
expect(job.max_run_time).to be_nil
121+
end
122+
end
123+
end
124+
106125
describe '#display_name' do
107126
subject(:wrapping_job) { WrappingJob.new(handler) }
108127

0 commit comments

Comments
 (0)