Add unique job ID for every pushed job

This commit is contained in:
Mike Perham 2012-08-11 11:47:25 -07:00
parent 5b99b4208f
commit ae2837bd86
4 changed files with 14 additions and 3 deletions

View File

@ -1,3 +1,10 @@
HEAD
-----------
- Sidekiq::Client now generates and returns a random, 128-bit Job ID 'jid' which
can be used to track the processing of a Job, e.g. for calling back to a webhook
when a job is finished.
2.1.1
-----------

View File

@ -28,6 +28,8 @@ module Sidekiq
# All options must be strings, not symbols. NB: because we are serializing to JSON, all
# symbols in 'args' will be converted to strings.
#
# Returns nil if not pushed to Redis or a unique Job ID if pushed.
#
# Example:
# Sidekiq::Client.push('queue' => 'my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
#
@ -42,6 +44,7 @@ module Sidekiq
item = worker_class.get_sidekiq_options.merge(item)
item['retry'] = !!item['retry']
queue = item['queue']
item['jid'] = SecureRandom.base64
pushed = false
Sidekiq.client_middleware.invoke(worker_class, item, queue) do
@ -57,7 +60,7 @@ module Sidekiq
end
end
end
!! pushed
pushed ? item['jid'] : nil
end
# Redis compatibility helper. Example usage:

View File

@ -36,6 +36,7 @@ class TestClient < MiniTest::Unit::TestCase
@redis.expect :rpush, 1, ['queue:foo', String]
pushed = Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => [1, 2])
assert pushed
assert_equal 24, pushed.size
@redis.verify
end

View File

@ -19,13 +19,13 @@ class TestScheduling < MiniTest::Unit::TestCase
it 'schedules a job via interval' do
@redis.expect :zadd, true, ['schedule', String, String]
assert_equal true, ScheduledWorker.perform_in(600, 'mike')
assert ScheduledWorker.perform_in(600, 'mike')
@redis.verify
end
it 'schedules a job via timestamp' do
@redis.expect :zadd, true, ['schedule', String, String]
assert_equal true, ScheduledWorker.perform_in(5.days.from_now, 'mike')
assert ScheduledWorker.perform_in(5.days.from_now, 'mike')
@redis.verify
end
end