Skip to content

Commit

Permalink
remove_delayed_job_from_timestamp should clear out the timestamp if i…
Browse files Browse the repository at this point in the history
…t's empty after the remove
  • Loading branch information
bvandenbos committed Nov 3, 2011
1 parent 5b12d4b commit c9a761f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/resque_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,14 @@ def remove_delayed(klass, *args)
# O(N) where N is the number of jobs scheduled to fire at the given
# timestamp
def remove_delayed_job_from_timestamp(timestamp, klass, *args)
redis.lrem "delayed:#{timestamp.to_i}", 0, encode(job_to_hash(klass, args))
key = "delayed:#{timestamp.to_i}"
count = redis.lrem key, 0, encode(job_to_hash(klass, args))
clean_up_timestamp(key, timestamp)
count
end

def count_all_scheduled_jobs
total_jobs = 0
total_jobs = 0
Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |timestamp|
total_jobs += redis.llen("delayed:#{timestamp}").to_i
end
Expand Down
8 changes: 8 additions & 0 deletions test/delayed_queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,17 @@
end

test "remove_delayed_job_from_timestamp returns the number of items removed" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob, "foo")
assert_equal 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, "foo")
end

test "remove_delayed_job_from_timestamp should cleanup the delayed timestamp list if not jobs are left" do
t = Time.now + 120
Resque.enqueue_at(t, SomeIvarJob, "foo")
assert_equal 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, "foo")
assert !Resque.redis.exists("delayed:#{t.to_i}")
assert Resque.delayed_queue_peek(0, 100).empty?
end

test "invalid job class" do
Expand Down

0 comments on commit c9a761f

Please sign in to comment.