Skip to content

Commit

Permalink
Update and add documentation for job specific max_run_time
Browse files Browse the repository at this point in the history
  • Loading branch information
albus522 committed Sep 24, 2014
1 parent 692786b commit ae0ca0a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ end

Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.pluck(:email))
```

To set a per-job max attempts that overrides the Delayed::Worker.max_attempts you can define a max_attempts method on the job

```ruby
NewsletterJob = Struct.new(:text, :emails) do
def perform
Expand All @@ -256,7 +258,25 @@ NewsletterJob = Struct.new(:text, :emails) do
end
end
```

To set a per-job max run time that overrides the Delayed::Worker.max_run_time you can define a max_run_time method on the job

NOTE: this can ONLY be used to set a max_run_time that is lower than Delayed::Worker.max_run_time. Otherwise the lock on the job would expire and another worker would start the working on the in progress job.

```ruby
NewsletterJob = Struct.new(:text, :emails) do
def perform
emails.each { |e| NewsletterMailer.deliver_text_to_email(text, e) }
end

def max_run_time
120 # seconds
end
end
```

To set a default queue name for a custom job that overrides Delayed::Worker.default_queue_name, you can define a queue_name method on the job

```ruby
NewsletterJob = Struct.new(:text, :emails) do
def perform
Expand Down
9 changes: 8 additions & 1 deletion lib/delayed/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ def max_attempts
end

def max_run_time
payload_object.max_run_time if payload_object.respond_to?(:max_run_time)
return unless payload_object.respond_to?(:max_run_time)
if (run_time = payload_object.max_run_time)
if run_time > Delayed::Worker.max_run_time
Delayed::Worker.max_run_time
else
run_time
end
end
end

def fail!
Expand Down
5 changes: 5 additions & 0 deletions lib/delayed/backend/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def create_job(opts = {})
expect(@job.payload_object).to receive(:max_run_time).and_return(45.minutes)
expect(worker.max_run_time(@job)).to eq(45.minutes)
end

it 'job set max_run_time can not exceed default max run time' do
expect(@job.payload_object).to receive(:max_run_time).and_return(Delayed::Worker::DEFAULT_MAX_RUN_TIME + 60)
expect(worker.max_run_time(@job)).to eq(Delayed::Worker::DEFAULT_MAX_RUN_TIME)
end
end

describe 'yaml serialization' do
Expand Down

0 comments on commit ae0ca0a

Please sign in to comment.