Skip to content

Commit

Permalink
Merge pull request #760 from cluesque/per_job_pull_request
Browse files Browse the repository at this point in the history
Allow delay_jobs configuration to be a proc
  • Loading branch information
albus522 committed Apr 2, 2015
2 parents 443a020 + ada2f25 commit 7ff0987
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ By default all jobs will be queued without a named queue. A default named queue

It is possible to disable delayed jobs for testing purposes. Set `Delayed::Worker.delay_jobs = false` to execute all jobs realtime.

Or `Delayed::Worker.delay_jobs` can be a Proc that decides whether to execute jobs inline on a per-job basis:

```ruby
Delayed::Worker.delay_jobs = ->(job) {
job.queue != 'inline'
}
```

You may need to raise exceptions on SIGTERM signals, `Delayed::Worker.raise_signal_exceptions = :term` will cause the worker to raise a `SignalException` causing the running job to abort and be unlocked, which makes the job available to other workers. The default for this option is false.

Here is an example of changing job parameters in Rails:
Expand Down
10 changes: 9 additions & 1 deletion lib/delayed/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def enqueue(*args) # rubocop:disable CyclomaticComplexity
new(options).tap do |job|
Delayed::Worker.lifecycle.run_callbacks(:enqueue, job) do
job.hook(:enqueue)
Delayed::Worker.delay_jobs ? job.save : job.invoke_job
job.should_really_delay? ? job.save : job.invoke_job
end
end
end
Expand Down Expand Up @@ -69,6 +69,14 @@ def error=(error)
self.last_error = "#{error.message}\n#{error.backtrace.join("\n")}" if self.respond_to?(:last_error=)
end

def should_really_delay?
if Delayed::Worker.delay_jobs.is_a?(Proc)
Delayed::Worker.delay_jobs.call(self)
else
Delayed::Worker.delay_jobs
end
end

def failed?
!!failed_at
end
Expand Down
20 changes: 20 additions & 0 deletions spec/message_sending_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,25 @@ def tell
end.not_to change(fairy_tail, :happy_ending)
end.to change { Delayed::Job.count }.by(1)
end

it 'does delay when delay_jobs is a proc returning true' do
Delayed::Worker.delay_jobs = ->(_job) { true }
fairy_tail = FairyTail.new
expect do
expect do
fairy_tail.delay.tell
end.not_to change(fairy_tail, :happy_ending)
end.to change { Delayed::Job.count }.by(1)
end

it 'does not delay the job when delay_jobs is a proc returning false' do
Delayed::Worker.delay_jobs = ->(_job) { false }
fairy_tail = FairyTail.new
expect do
expect do
fairy_tail.delay.tell
end.to change(fairy_tail, :happy_ending).from(nil).to(true)
end.not_to change { Delayed::Job.count }
end
end
end

0 comments on commit 7ff0987

Please sign in to comment.