Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:defunkt/resque

  • Loading branch information...
commit 4d7dcc5b761ae8eea1c64251cc568ea3e1315c65 2 parents 3d9814d + 61d9cb5
@defunkt defunkt authored
View
11 docs/HOOKS.md
@@ -66,6 +66,9 @@ An unnamed hook (`before_perform`) will be executed first.
The available hooks are:
+* `after_enqueue`: Called with the job args after a job is placed on the queue.
+ Any exception raised propagates up to the code which queued the job.
+
* `before_perform`: Called with the job args before perform. If it raises
`Resque::Job::DontPerform`, the job is aborted. If other exceptions
are raised, they will be propagated up the the `Resque::Failure`
@@ -99,6 +102,13 @@ look something like this.
Modules are even better because jobs can use many of them.
+ module ScaledJob
+ def after_enqueue_scale_workers(*args)
+ Logger.info "Scaling worker count up"
+ Scaler.up! if Redis.info[:pending].to_i > 25
+ end
+ end
+
module LoggedJob
def before_perform_log_job(*args)
Logger.info "About to perform #{self} with #{args.inspect}"
@@ -115,6 +125,7 @@ Modules are even better because jobs can use many of them.
class MyJob
extend LoggedJob
extend RetriedJob
+ extend ScaledJob
def self.perform(*args)
...
end
View
6 lib/resque/job.rb
@@ -48,7 +48,11 @@ def self.create(queue, klass, *args)
raise NoClassError.new("Jobs must be given a class.")
end
- Resque.push(queue, :class => klass.to_s, :args => args)
+ ret = Resque.push(queue, :class => klass.to_s, :args => args)
+ Plugin.after_enqueue_hooks(klass).each do |hook|
+ klass.send(hook, *args)
+ end
+ ret
end
# Removes a job from a queue. Expects a string queue name, a
View
5 lib/resque/plugin.rb
@@ -42,5 +42,10 @@ def after_hooks(job)
def failure_hooks(job)
job.methods.grep(/^on_failure/).sort
end
+
+ # Given an object, returns a list `after_enqueue` hook names.
+ def after_enqueue_hooks(job)
+ job.methods.grep(/^after_enqueue/).sort
+ end
end
end
View
21 test/job_hooks_test.rb
@@ -228,6 +228,27 @@ def self.on_failure_record_failure(exception, history)
end
end
+context "Resque::Job after_enqueue" do
+ include PerformJob
+
+ class ::AfterEnqueueJob
+ def self.after_enqueue_record_history(history)
+ history << :after_enqueue
+ end
+
+ def self.perform(history)
+ end
+ end
+
+ test "the after enqueue hook should run" do
+ history = []
+ @worker = Resque::Worker.new(:jobs)
+ Resque::Job.create(:jobs, AfterEnqueueJob, history)
+ @worker.work(0)
+ assert_equal history, [:after_enqueue], "after_enqueue was not run"
+ end
+end
+
context "Resque::Job all hooks" do
include PerformJob
Please sign in to comment.
Something went wrong with that request. Please try again.