forked from mikeys/resque-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- New module, ResqueScheduler::Plugin, for running hooks. - Refactored before_schedule- and after_schedule-hooks. - Added before_delayed_enqueue-hook. - Simplified tests for hooks. - Updated README.
- Loading branch information
andreas
committed
Mar 14, 2012
1 parent
6d37cdd
commit b10a06a
Showing
6 changed files
with
60 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module ResqueScheduler | ||
module Plugin | ||
extend self | ||
def hooks(job, pattern) | ||
job.methods.grep(/^#{pattern}/).sort | ||
end | ||
|
||
def run_hooks(job, pattern, *args) | ||
results = hooks(job, pattern).collect do |hook| | ||
job.send(hook, *args) | ||
end | ||
|
||
results.all? { |result| result != false } | ||
end | ||
|
||
def method_missing(method_name, *args, &block) | ||
if method_name =~ /^run_(.*)_hooks$/ | ||
job = args.shift | ||
run_hooks job, $1, *args | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,23 @@ | ||
require File.dirname(__FILE__) + '/test_helper' | ||
|
||
context "scheduling jobs with hooks" do | ||
class JobThatCannotBeScheduledWithoutArguments < Resque::Job | ||
@queue = :job_that_cannot_be_scheduled_without_arguments | ||
def self.perform(*x);end | ||
def self.before_schedule_return_nil_if_arguments_not_supplied(*args) | ||
counters[:before_schedule] += 1 | ||
return false if args.empty? | ||
end | ||
|
||
def self.after_schedule_do_something(*args) | ||
counters[:after_schedule] += 1 | ||
end | ||
|
||
class << self | ||
def counters | ||
@counters ||= Hash.new{|h,k| h[k]=0} | ||
end | ||
def clean | ||
counters.clear | ||
self | ||
end | ||
end | ||
end | ||
|
||
setup do | ||
Resque::Scheduler.dynamic = false | ||
Resque.redis.del(:schedules) | ||
Resque.redis.del(:schedules_changed) | ||
Resque::Scheduler.mute = true | ||
Resque::Scheduler.clear_schedule! | ||
Resque::Scheduler.send(:class_variable_set, :@@scheduled_jobs, {}) | ||
Resque.redis.flushall | ||
end | ||
|
||
test "before_schedule hook that does not return false should not block" do | ||
enqueue_time = Time.now + 12 | ||
Resque.enqueue_at(enqueue_time, JobThatCannotBeScheduledWithoutArguments.clean, :foo) | ||
assert_equal(1, Resque.delayed_timestamp_size(enqueue_time.to_i), "delayed queue should have one entry now") | ||
assert_equal(1, JobThatCannotBeScheduledWithoutArguments.counters[:before_schedule], 'before_schedule was not run') | ||
assert_equal(1, JobThatCannotBeScheduledWithoutArguments.counters[:after_schedule], 'after_schedule was not run') | ||
test "before_schedule hook that does not return false should be enqueued" do | ||
enqueue_time = Time.now | ||
SomeRealClass.expects(:before_schedule_example).with(:foo) | ||
SomeRealClass.expects(:after_schedule_example).with(:foo) | ||
Resque.enqueue_at(enqueue_time.to_i, SomeRealClass, :foo) | ||
assert_equal(1, Resque.delayed_timestamp_size(enqueue_time.to_i), "job should be enqueued") | ||
end | ||
|
||
test "before_schedule hook that returns false should block" do | ||
enqueue_time = Time.now + 60 | ||
assert_equal(0, JobThatCannotBeScheduledWithoutArguments.clean.counters[:before_schedule], 'before_schedule should be zero') | ||
Resque.enqueue_at(enqueue_time, JobThatCannotBeScheduledWithoutArguments.clean) | ||
assert_equal(0, Resque.delayed_timestamp_size(enqueue_time.to_i), "job should not have been put in queue") | ||
assert_equal(1, JobThatCannotBeScheduledWithoutArguments.counters[:before_schedule], 'before_schedule was not run') | ||
assert_equal(0, JobThatCannotBeScheduledWithoutArguments.counters[:after_schedule], 'after_schedule was run') | ||
test "before_schedule hook that returns false should not be enqueued" do | ||
enqueue_time = Time.now | ||
SomeRealClass.expects(:before_schedule_example).with(:foo).returns(false) | ||
SomeRealClass.expects(:after_schedule_example).never | ||
Resque.enqueue_at(enqueue_time.to_i, SomeRealClass, :foo) | ||
assert_equal(0, Resque.delayed_timestamp_size(enqueue_time.to_i), "job should not be enqueued") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters