Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for Resque::JobWithStatus being queued via #enqueue_at and friends #127

Closed
wants to merge 1 commit into from

5 participants

@jmoses

Queuing Resque::JobWithStatus jobs fail, as they fall back to the normal Resque::Job.create behavior when they are scheduled. This is because the job_hash doesn't include the :custom_job_class parameter that the config file has support for.

This change allows the jobs to set an instance variable (which isn't the best way but works) that indicates they require the custom_job_class parameter.

@jmoses jmoses Support enqueued jobs that hate Resque::Job.create
Queuing Resque::JobWithStatus jobs fail, as they fall back to the normal Resque::Job.create behavior when they are scheduled.  This is because the job_hash doesn't include the :custom_job_class parameter that the config file has support for.

This change allows the jobs to set an instance variable (which isn't the best way but works) that indicates they require the custom_job_class parameter.
fc7aebf
@unclebilly

I would like to see this pulled as well! Thanks, @jmoses.

@bvandenbos
Owner

Any way we can avoid the instance var? Seems awkward.

Also, tap isn't in ruby 1.8.7, so this would break those unfortunate souls still using it.

@jmoses
@jonhyman

I think that this pull request satisfies this: #182. Since custom jobs need to respond to #scheduled, it just checks for that.

@meatballhat
Owner

@jmoses If there's a chance you're still interested in this, does the patch from #182 do the job? Thanks!

@jmoses

It looks like it does, yeah, assuming (I didn't check) the resque status jobs do "the right thing" for .scheduled, which they must, since that was the specifically mentioned usecase in the other pull.

@jmoses jmoses closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 12, 2011
  1. @jmoses

    Support enqueued jobs that hate Resque::Job.create

    jmoses authored
    Queuing Resque::JobWithStatus jobs fail, as they fall back to the normal Resque::Job.create behavior when they are scheduled.  This is because the job_hash doesn't include the :custom_job_class parameter that the config file has support for.
    
    This change allows the jobs to set an instance variable (which isn't the best way but works) that indicates they require the custom_job_class parameter.
This page is out of date. Refresh to see the latest.
View
12 lib/resque_scheduler.rb
@@ -239,11 +239,19 @@ def count_all_scheduled_jobs
private
def job_to_hash(klass, args)
- {:class => klass.to_s, :args => args, :queue => queue_from_class(klass)}
+ base_job_hash(klass).merge(:args => args, :queue => queue_from_class(klass))
end
def job_to_hash_with_queue(queue, klass, args)
- {:class => klass.to_s, :args => args, :queue => queue}
+ base_job_hash(klass).merge(:args => args, :queue => queue)
+ end
+
+ def base_job_hash(klass)
+ {:class => klass.to_s}.tap do |hash|
+ if klass.instance_variable_get(:@require_custom_class)
+ hash.merge! :custom_job_class => klass.to_s
+ end
+ end
end
def clean_up_timestamp(key, timestamp)
View
53 test/delayed_queue_test.rb
@@ -33,6 +33,59 @@
assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
end
+ test "enqueue_at with standard job class writes out custom class to queue" do
+ timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)
+
+ assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
+
+ Resque.enqueue_at_with_queue(:queue, timestamp, SomeJob, "path")
+
+ # Confirm the correct keys were added
+ assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should have one entry now")
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "The delayed_queue_schedule should have 1 entry now")
+
+ read_timestamp = Resque.next_delayed_timestamp
+
+ # Confirm the timestamp came out correctly
+ assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
+ item = Resque.next_item_for_timestamp(read_timestamp)
+
+ # Confirm the item came out correctly
+ assert_equal('SomeJob', item['class'], "Should be the same class that we queued")
+ assert_nil(item['custom_job_class'], "Should not be present")
+ assert_equal(["path"], item['args'], "Should have the same arguments that we queued")
+
+ # And now confirm the keys are gone
+ assert(!Resque.redis.exists("delayed:#{timestamp.to_i}"))
+ assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
+ end
+ test "enqueue_at with custom job class writes out custom class to queue" do
+ timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)
+
+ assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should be empty to start")
+
+ Resque.enqueue_at_with_queue(:queue, timestamp, FakeCustomJobClass, "path")
+
+ # Confirm the correct keys were added
+ assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i, "delayed queue should have one entry now")
+ assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule), "The delayed_queue_schedule should have 1 entry now")
+
+ read_timestamp = Resque.next_delayed_timestamp
+
+ # Confirm the timestamp came out correctly
+ assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
+ item = Resque.next_item_for_timestamp(read_timestamp)
+
+ # Confirm the item came out correctly
+ assert_equal('FakeCustomJobClass', item['class'], "Should be the same class that we queued")
+ assert_equal('FakeCustomJobClass', item['custom_job_class'], "Should be the same class that we queued")
+ assert_equal(["path"], item['args'], "Should have the same arguments that we queued")
+
+ # And now confirm the keys are gone
+ assert(!Resque.redis.exists("delayed:#{timestamp.to_i}"))
+ assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule), "delayed queue should be empty")
+ end
+
test "enqueue_at with queue adds correct list and zset and queue" do
timestamp = Time.now - 1 # 1 second ago (in the past, should come out right away)
View
2  test/test_helper.rb
@@ -69,6 +69,8 @@ def self.teardown(&block) define_method(:teardown, &block) end
end
class FakeCustomJobClass
+ @require_custom_class = true
+
def self.scheduled(queue, klass, *args); end
end
Something went wrong with that request. Please try again.