Skip to content

Commit

Permalink
fixed an error with the usage of Resque::Job.create where the class n…
Browse files Browse the repository at this point in the history
…ame was passed in instead of the actual class which caused the after_enqueue hooks not to work. the Resque docs are actually incorrect in stating that they accept a classname string
  • Loading branch information
sskirby committed Oct 26, 2010
1 parent 8beb3c4 commit 9fb5cff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
10 changes: 6 additions & 4 deletions lib/resque/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ def enqueue_delayed_items_for_timestamp(timestamp)
handle_shutdown do
if item = Resque.next_item_for_timestamp(timestamp)
log "queuing #{item['class']} [delayed]"
queue = item['queue'] || Resque.queue_from_class(constantize(item['class']))
klass = constantize(item['class'])
queue = item['queue'] || Resque.queue_from_class(klass)
# Support custom job classes like job with status
if (job_klass = item['custom_job_class']) && (job_klass != 'Resque::Job')
# custom job classes not supporting the same API calls must implement the #schedule method
constantize(job_klass).scheduled(queue, item['class'], *item['args'])
else
Resque::Job.create(queue, item['class'], *item['args'])
Resque::Job.create(queue, klass, *item['args'])
end
end
end
Expand All @@ -137,14 +138,15 @@ def handle_shutdown
def enqueue_from_config(config)
args = config['args'] || config[:args]
klass_name = config['class'] || config[:class]
klass = constantize(klass_name)
params = args.nil? ? [] : Array(args)
queue = config['queue'] || config[:queue] || Resque.queue_from_class(constantize(klass_name))
queue = config['queue'] || config[:queue] || Resque.queue_from_class(klass)
# Support custom job classes like job with status
if (job_klass = config['custom_job_class']) && (job_klass != 'Resque::Job')
# custom job classes not supporting the same API calls must implement the #schedule method
constantize(job_klass).scheduled(queue, klass_name, *params)
else
Resque::Job.create(queue, klass_name, *params)
Resque::Job.create(queue, klass, *params)
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/delayed_queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
Resque.expects(:queue_from_class).never # Should NOT need to load the class
Resque::Scheduler.handle_delayed_items
end
Expand All @@ -128,7 +128,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::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
Resque.expects(:queue_from_class).never # Should NOT need to load the class

Resque::Scheduler.enqueue_delayed_items_for_timestamp(t)
Expand All @@ -144,7 +144,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
7 changes: 1 addition & 6 deletions test/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ def setup
Resque::Scheduler.send(:class_variable_set, :@@scheduled_jobs, {})
end

def test_enqueue_from_config_puts_stuff_in_the_resque_queue_without_class_loaded
Resque::Job.stubs(:create).once.returns(true).with('joes_queue', 'BigJoesJob', '/tmp')
Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'BigJoesJob', 'args' => "/tmp", 'queue' => 'joes_queue')
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

0 comments on commit 9fb5cff

Please sign in to comment.