Skip to content

Commit

Permalink
Make scheduled jobs unique until they are scheduled for.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Aug 19, 2012
1 parent 3ca8d77 commit f89097e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def call(worker_class, item, queue)
if conn.get(payload_hash)
conn.unwatch
else
expires_at = HASH_KEY_EXPIRATION
expires_at = ((Time.at(item['at']) - Time.now.utc) * 24 * 60 * 60).to_i if item['at']

unique = conn.multi do
conn.setex(payload_hash, HASH_KEY_EXPIRATION, 1)
conn.setex(payload_hash, expires_at, 1)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq-unique-jobs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SidekiqUniqueJobs
VERSION = "2.2.0"
VERSION = "2.2.1"
end
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'simplecov'
SimpleCov.start
end

require 'pry'
require 'minitest/unit'
require 'minitest/pride'
require 'minitest/autorun'
Expand Down
22 changes: 22 additions & 0 deletions test/lib/sidekiq/test_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'helper'
require 'sidekiq/worker'
require "sidekiq-unique-jobs"
require 'sidekiq/scheduled'

class TestClient < MiniTest::Unit::TestCase
describe 'with real redis' do
Expand All @@ -12,6 +13,8 @@ class TestClient < MiniTest::Unit::TestCase
class QueueWorker
include Sidekiq::Worker
sidekiq_options :queue => 'customqueue'
def perform(x)
end
end

it 'does not push duplicate messages when configured for unique only' do
Expand All @@ -25,5 +28,24 @@ class QueueWorker
10.times { Sidekiq::Client.push('class' => QueueWorker, 'args' => [1, 2]) }
assert_equal 10, Sidekiq.redis {|c| c.llen("queue:customqueue") }
end

# TODO: If anyone know of a better way to check that the expiration for scheduled
# jobs are set around the same time as the scheduled job itself feel free to improve.
it 'expires the payload_hash when a scheduled job is scheduled at' do
require 'active_support/all'
QueueWorker.sidekiq_options :unique => true

at = 15.minutes.from_now
expected_expires_at = (Time.at(at) - Time.now.utc).to_f

QueueWorker.perform_in(at, 'mike')
payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(['mike']))

# deconstruct this into a time format we can use to get a decent delta for
actual_expires_at = Sidekiq.redis {|c| c.ttl(payload_hash).to_f / 24 / 60 / 60 }

assert_in_delta expected_expires_at, actual_expires_at, 0.05

end
end
end

0 comments on commit f89097e

Please # to comment.