Skip to content

Commit

Permalink
Merge branch 'time_override' of https://github.com/rcarver/resque-sch…
Browse files Browse the repository at this point in the history
…eduler into rcarver-time_override
  • Loading branch information
bvandenbos committed Nov 9, 2010
2 parents 3c7fbe8 + c7ff729 commit 0845095
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/resque/scheduler.rb
Expand Up @@ -86,10 +86,11 @@ def rails_env_matches?(config)
end

# Handles queueing delayed items
def handle_delayed_items
item = nil
# at_time - Time to start scheduling items (default: now).
def handle_delayed_items(at_time=nil)
timestamp = nil
begin
if timestamp = Resque.next_delayed_timestamp
if timestamp = Resque.next_delayed_timestamp(at_time)
enqueue_delayed_items_for_timestamp(timestamp)
end
# continue processing until there are no more ready timestamps
Expand Down
4 changes: 2 additions & 2 deletions lib/resque_scheduler.rb
Expand Up @@ -91,8 +91,8 @@ def delayed_timestamp_peek(timestamp, start, count)

# Returns the next delayed queue timestamp
# (don't call directly)
def next_delayed_timestamp
items = redis.zrangebyscore :delayed_queue_schedule, '-inf', Time.now.to_i, :limit => [0, 1]
def next_delayed_timestamp(at_time=nil)
items = redis.zrangebyscore :delayed_queue_schedule, '-inf', (at_time || Time.now).to_i, :limit => [0, 1]
timestamp = items.nil? ? nil : Array(items).first
timestamp.to_i unless timestamp.nil?
end
Expand Down
21 changes: 21 additions & 0 deletions test/delayed_queue_test.rb
Expand Up @@ -50,6 +50,16 @@ def test_something_in_the_future_doesnt_come_out
assert_nil(read_timestamp, "No timestamps should be ready for queueing")
end

def test_something_in_the_future_comes_out_if_you_want_it_to
timestamp = Time.now + 600 # 10 minutes from now

Resque.enqueue_at(timestamp, SomeIvarJob, "path")

read_timestamp = Resque.next_delayed_timestamp(timestamp)

assert_equal(timestamp.to_i, read_timestamp, "The timestamp we pull out of redis should match the one we put in")
end

def test_enqueue_at_and_enqueue_in_are_equivelent
timestamp = Time.now + 60

Expand Down Expand Up @@ -120,6 +130,17 @@ def test_handle_delayed_items_with_items
Resque.expects(:queue_from_class).never # Should NOT need to load the class
Resque::Scheduler.handle_delayed_items
end

def test_handle_delayed_items_with_items_in_the_future
t = Time.now + 60 # in the future
Resque.enqueue_at(t, SomeIvarJob)
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::Scheduler.handle_delayed_items(t)
end

def test_enqueue_delayed_items_for_timestamp
t = Time.now + 60
Expand Down

0 comments on commit 0845095

Please sign in to comment.