Skip to content

Commit

Permalink
Added ability for enqueue hook to modify the job object before being …
Browse files Browse the repository at this point in the history
…saved.
  • Loading branch information
Cameron Hurst committed May 24, 2011
1 parent 3fc5713 commit dafba70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/delayed/backend/base.rb
Expand Up @@ -25,8 +25,9 @@ def enqueue(*args)
end

if Delayed::Worker.delay_jobs
self.create(options).tap do |job|
self.new(options).tap do |job|
job.hook(:enqueue)
job.save
end
else
Delayed::Job.new(:payload_object => options[:payload_object]).tap do |job|
Expand Down
8 changes: 8 additions & 0 deletions spec/active_record_job_spec.rb
Expand Up @@ -33,4 +33,12 @@
Delayed::Backend::ActiveRecord::Job.after_fork
end
end

describe "enqueue" do
it "should allow enqueue hook to modify job at DB level" do
later = described_class.db_time_now + 20.minutes
job = described_class.enqueue :payload_object => EnqueueJobMod.new
Delayed::Backend::ActiveRecord::Job.find(job.id).run_at.should be_within(1).of(later)
end
end
end
6 changes: 6 additions & 0 deletions spec/sample_jobs.rb
Expand Up @@ -67,3 +67,9 @@ def failure(job)
self.class.messages << 'failure'
end
end

class EnqueueJobMod < SimpleJob
def enqueue(job)
job.run_at = 20.minutes.from_now
end
end

0 comments on commit dafba70

Please sign in to comment.