Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support hooks with resque >= 1.15.0
  • Loading branch information
Jon Larkowski and Les Hill authored and leshill committed Mar 30, 2011
1 parent 7dcf9bc commit 5029aa2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
34 changes: 13 additions & 21 deletions Gemfile.lock
@@ -1,52 +1,44 @@
PATH
remote: .
specs:
resque-scheduler (1.9.7)
resque-scheduler (2.0.0.c)
redis (>= 2.0.1)
resque (>= 1.8.0)
resque (>= 1.15.0)
rufus-scheduler

GEM
remote: http://rubygems.org/
specs:
git (1.2.5)
jeweler (1.5.1)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
json (1.4.6)
mocha (0.9.9)
rake
rack (1.2.1)
rack-test (0.5.6)
rack (>= 1.0)
rake (0.8.7)
redis (2.1.1)
redis-namespace (0.8.0)
redis (2.2.0)
redis-namespace (0.10.0)
redis (< 3.0.0)
resque (1.10.0)
resque (1.15.0)
json (~> 1.4.6)
redis-namespace (~> 0.8.0)
redis-namespace (>= 0.10.0)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
rufus-scheduler (2.0.7)
tzinfo
sinatra (1.1.0)
rufus-scheduler (2.0.8)
tzinfo (>= 0.3.23)
sinatra (1.2.1)
rack (~> 1.1)
tilt (~> 1.1)
tilt (1.1)
tzinfo (0.3.23)
tilt (< 2.0, >= 1.2.2)
tilt (1.2.2)
tzinfo (0.3.25)
vegas (0.1.8)
rack (>= 1.0.0)

PLATFORMS
ruby

DEPENDENCIES
jeweler
bundler (>= 1.0.0)
mocha
rack-test
redis (>= 2.0.1)
resque (>= 1.8.0)
resque-scheduler!
rufus-scheduler
14 changes: 12 additions & 2 deletions lib/resque/scheduler.rb
Expand Up @@ -152,9 +152,12 @@ def handle_shutdown
# Enqueues a job based on a config hash
def enqueue_from_config(job_config)
args = job_config['args'] || job_config[:args]

klass_name = job_config['class'] || job_config[:class]
klass = constantize(klass_name) rescue klass_name

params = args.is_a?(Hash) ? [args] : Array(args)
queue = job_config['queue'] || job_config[:queue] || Resque.queue_from_class(constantize(klass_name))
queue = job_config['queue'] || job_config[:queue] || Resque.queue_from_class(klass)
# Support custom job classes like those that inherit from Resque::JobWithStatus (resque-status)
if (job_klass = job_config['custom_job_class']) && (job_klass != 'Resque::Job')
# The custom job class API must offer a static "scheduled" method. If the custom
Expand All @@ -167,7 +170,14 @@ def enqueue_from_config(job_config)
Resque::Job.create(queue, job_klass, *params)
end
else
Resque::Job.create(queue, klass_name, *params)
# hack to avoid havoc for people shoving stuff into queues
# for non-existent classes (for example: running scheduler in
# one app that schedules for another
if Class === klass
Resque.enqueue(klass, *params)
else
Resque::Job.create(queue, klass, *params)
end
end
rescue
log! "Failed to enqueue #{klass_name}:\n #{$!}"
Expand Down
2 changes: 1 addition & 1 deletion resque-scheduler.gemspec
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.require_path = 'lib'

s.add_runtime_dependency(%q<redis>, [">= 2.0.1"])
s.add_runtime_dependency(%q<resque>, [">= 1.8.0"])
s.add_runtime_dependency(%q<resque>, [">= 1.15.0"])
s.add_runtime_dependency(%q<rufus-scheduler>, [">= 0"])
s.add_development_dependency(%q<mocha>, [">= 0"])
s.add_development_dependency(%q<rack-test>, [">= 0"])
Expand Down
11 changes: 4 additions & 7 deletions test/delayed_queue_test.rb
Expand Up @@ -126,8 +126,7 @@ def test_handle_delayed_items_with_items
Resque.enqueue_at(t, SomeIvarJob)

# 2 SomeIvarJob jobs should be created in the "ivar" queue
Resque::Job.expects(:create).twice.with('ivar', 'SomeIvarJob', nil)
Resque.expects(:queue_from_class).never # Should NOT need to load the class
Resque::Job.expects(:create).twice.with(:ivar, SomeIvarJob, nil)
Resque::Scheduler.handle_delayed_items
end

Expand All @@ -137,8 +136,7 @@ def test_handle_delayed_items_with_items_in_the_future
Resque.enqueue_at(t, SomeIvarJob)

# 2 SomeIvarJob jobs should be created in the "ivar" queue
Resque::Job.expects(:create).twice.with('ivar', 'SomeIvarJob', nil)
Resque.expects(:queue_from_class).never # Should NOT need to load the class
Resque::Job.expects(:create).twice.with(:ivar, SomeIvarJob, nil)
Resque::Scheduler.handle_delayed_items(t)
end

Expand All @@ -149,8 +147,7 @@ def test_enqueue_delayed_items_for_timestamp
Resque.enqueue_at(t, SomeIvarJob)

# 2 SomeIvarJob jobs should be created in the "ivar" queue
Resque::Job.expects(:create).twice.with('ivar', 'SomeIvarJob', nil)
Resque.expects(:queue_from_class).never # Should NOT need to load the class
Resque::Job.expects(:create).twice.with(:ivar, SomeIvarJob, nil)

Resque::Scheduler.enqueue_delayed_items_for_timestamp(t)

Expand All @@ -165,7 +162,7 @@ def test_works_with_out_specifying_queue__upgrade_case
# Since we didn't specify :queue when calling delayed_push, it will be forced
# to load the class to figure out the queue. This is the upgrade case from 1.0.4
# to 1.0.5.
Resque::Job.expects(:create).once.with(:ivar, 'SomeIvarJob', nil)
Resque::Job.expects(:create).once.with(:ivar, SomeIvarJob, nil)

Resque::Scheduler.handle_delayed_items
end
Expand Down
21 changes: 20 additions & 1 deletion test/scheduler_test.rb
Expand Up @@ -31,7 +31,7 @@ def test_enqueue_from_config_doesnt_crash_on_exception_when_enqueueing
end

def test_enqueue_from_config_puts_stuff_in_the_resque_queue
Resque::Job.stubs(:create).once.returns(true).with(:ivar, 'SomeIvarJob', '/tmp')
Resque::Job.stubs(:create).once.returns(true).with(:ivar, SomeIvarJob, '/tmp')
Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
end

Expand Down Expand Up @@ -78,6 +78,25 @@ def test_enqueue_from_config_when_rails_env_arg_is_not_set
assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
end

def test_enqueue_constantizes
# The job should be loaded, since a missing rails_env means ALL envs.
ENV['RAILS_ENV'] = 'production'
config = {'cron' => "* * * * *", 'class' => 'SomeRealClass', 'args' => "/tmp"}
Resque::Job.expects(:create).with(SomeRealClass.queue, SomeRealClass, '/tmp')
Resque::Scheduler.enqueue_from_config(config)
end

def test_enqueue_runs_hooks
# The job should be loaded, since a missing rails_env means ALL envs.
ENV['RAILS_ENV'] = 'production'
config = {'cron' => "* * * * *", 'class' => 'SomeRealClass', 'args' => "/tmp"}

Resque::Job.expects(:create).with(SomeRealClass.queue, SomeRealClass, '/tmp')
SomeRealClass.expects(:after_enqueue_example)

Resque::Scheduler.enqueue_from_config(config)
end

def test_config_makes_it_into_the_rufus_scheduler
assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)

Expand Down
6 changes: 6 additions & 0 deletions test/test_helper.rb
Expand Up @@ -76,3 +76,9 @@ def self.perform(repo_id, path)
class SomeIvarJob < SomeJob
@queue = :ivar
end

class SomeRealClass
def self.queue
:some_real_queue
end
end

0 comments on commit 5029aa2

Please sign in to comment.