Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/574'
Browse files Browse the repository at this point in the history
Conflicts:
	lib/delayed/backend/shared_spec.rb
  • Loading branch information
albus522 committed Sep 24, 2014
2 parents cacda3c + fbb704e commit 2cfe2f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/delayed/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def max_attempts
payload_object.max_attempts if payload_object.respond_to?(:max_attempts)
end

def max_run_time
payload_object.max_run_time if payload_object.respond_to?(:max_run_time)
end

def fail!
update_attributes(:failed_at => self.class.db_time_now)
end
Expand Down
24 changes: 23 additions & 1 deletion lib/delayed/backend/shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,34 @@ def create_job(opts = {})
expect(@job.max_attempts).to be_nil
end

it 'uses the max_retries value on the payload when defined' do
it 'uses the max_attempts value on the payload when defined' do
expect(@job.payload_object).to receive(:max_attempts).and_return(99)
expect(@job.max_attempts).to eq(99)
end
end

describe '#max_run_time' do
before(:each) { @job = described_class.enqueue SimpleJob.new }

it 'is not defined' do
expect(@job.max_run_time).to be_nil
end

it 'results in a default run time when not defined' do
expect(worker.max_run_time(@job)).to eq(Delayed::Worker::DEFAULT_MAX_RUN_TIME)
end

it 'uses the max_run_time value on the payload when defined' do
@job.payload_object.stub(:max_run_time).and_return(30.minutes)
expect(@job.max_run_time).to eq(30.minutes)
end

it 'results in an overridden run time when defined' do
@job.payload_object.stub(:max_run_time).and_return(45.minutes)
expect(worker.max_run_time(@job)).to eq(45.minutes)
end
end

describe 'yaml serialization' do
context 'when serializing jobs' do
it 'raises error ArgumentError for new records' do
Expand Down
6 changes: 5 additions & 1 deletion lib/delayed/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def work_off(num = 100)
def run(job)
job_say job, 'RUNNING'
runtime = Benchmark.realtime do
Timeout.timeout(self.class.max_run_time.to_i, WorkerTimeout) { job.invoke_job }
Timeout.timeout(max_run_time(job).to_i, WorkerTimeout) { job.invoke_job }
job.destroy
end
job_say job, format('COMPLETED after %.4f', runtime)
Expand Down Expand Up @@ -262,6 +262,10 @@ def max_attempts(job)
job.max_attempts || self.class.max_attempts
end

def max_run_time(job)
job.max_run_time || self.class.max_run_time
end

protected

def handle_failed_job(job, error)
Expand Down

0 comments on commit 2cfe2f5

Please sign in to comment.