From 571e287f1bb6c03fe7fa50e13647ca7dba2ca9a4 Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Tue, 17 Jul 2012 16:48:54 +0200 Subject: [PATCH 1/6] Added another Resque compatibility helper #enqueue_to --- lib/sidekiq/client.rb | 12 ++++++++++-- lib/sidekiq/worker.rb | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/sidekiq/client.rb b/lib/sidekiq/client.rb index 65ba3f69..8bc9aff7 100644 --- a/lib/sidekiq/client.rb +++ b/lib/sidekiq/client.rb @@ -61,14 +61,22 @@ module Sidekiq !! pushed end - # Redis compatibility helper. Example usage: + # Resque compatibility helpers. # - # Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar') + # Example usage: + # Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar') # # Messages are enqueued to the 'default' queue. # def self.enqueue(klass, *args) klass.perform_async(*args) end + + # Example usage: + # Sidekiq::Client.enqueue_to(:queue_name, MyWorker, 'foo', 1, :bat => 'bar') + # + def self.enqueue_to(queue, klass, *args) + klass.perform_in_queue(queue, *args) + end end end diff --git a/lib/sidekiq/worker.rb b/lib/sidekiq/worker.rb index 6a2ada51..50bb021d 100644 --- a/lib/sidekiq/worker.rb +++ b/lib/sidekiq/worker.rb @@ -35,6 +35,11 @@ module Sidekiq client_push('class' => self, 'args' => args) end + def perform_in_queue(queue, *args) + client_push('queue' => queue, 'class' => self, 'args' => args) + end + alias_method :perform_at_queue, :perform_in_queue + def perform_in(interval, *args) int = interval.to_f ts = (int < 1_000_000_000 ? Time.now.to_f + int : int) From 24933489568ab8412319a3463a5883c23daa1b92 Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Fri, 20 Jul 2012 10:27:33 +0200 Subject: [PATCH 2/6] Overrule the queue name when loading the job for processing --- lib/sidekiq/processor.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/sidekiq/processor.rb b/lib/sidekiq/processor.rb index d72db448..ed182287 100644 --- a/lib/sidekiq/processor.rb +++ b/lib/sidekiq/processor.rb @@ -39,6 +39,7 @@ module Sidekiq msg = Sidekiq.load_json(msgstr) klass = constantize(msg['class']) worker = klass.new + worker.sidekiq_options(:queue => queue) stats(worker, msg, queue) do Sidekiq.server_middleware.invoke(worker, msg, queue) do From 501fbc7b43d3568fc2e3e1283924ebe53ca7c97b Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Fri, 20 Jul 2012 10:43:14 +0200 Subject: [PATCH 3/6] Fixed bug in queue setting --- lib/sidekiq/processor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sidekiq/processor.rb b/lib/sidekiq/processor.rb index ed182287..2bda07c0 100644 --- a/lib/sidekiq/processor.rb +++ b/lib/sidekiq/processor.rb @@ -39,7 +39,7 @@ module Sidekiq msg = Sidekiq.load_json(msgstr) klass = constantize(msg['class']) worker = klass.new - worker.sidekiq_options(:queue => queue) + worker.class.sidekiq_options(:queue => queue) stats(worker, msg, queue) do Sidekiq.server_middleware.invoke(worker, msg, queue) do From 9c938db2c6d9efedf9660bc0e68881432a4be669 Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Thu, 9 Aug 2012 13:09:58 +0200 Subject: [PATCH 4/6] Added method for testing with perform_in_queue --- lib/sidekiq/testing/inline.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/sidekiq/testing/inline.rb b/lib/sidekiq/testing/inline.rb index a838f601..444f244a 100644 --- a/lib/sidekiq/testing/inline.rb +++ b/lib/sidekiq/testing/inline.rb @@ -32,6 +32,11 @@ module Sidekiq new.perform(*Sidekiq.load_json(Sidekiq.dump_json(args))) true end + alias_method :perform_in_queue_old, :perform_in_queue + def perform_in_queue(queue, *args) + new.perform(*Sidekiq.load_json(Sidekiq.dump_json(args))) + true + end end end end From 6e2e34f01396de4e7685bebdc7896837f1307dcc Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Tue, 21 Aug 2012 11:02:07 +0200 Subject: [PATCH 5/6] Added tests for perform_in_queue method on worker and enqueue_at on client --- lib/sidekiq/testing/inline.rb | 5 ----- lib/sidekiq/worker.rb | 3 +-- test/test_client.rb | 21 +++++++++++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/sidekiq/testing/inline.rb b/lib/sidekiq/testing/inline.rb index 444f244a..a838f601 100644 --- a/lib/sidekiq/testing/inline.rb +++ b/lib/sidekiq/testing/inline.rb @@ -32,11 +32,6 @@ module Sidekiq new.perform(*Sidekiq.load_json(Sidekiq.dump_json(args))) true end - alias_method :perform_in_queue_old, :perform_in_queue - def perform_in_queue(queue, *args) - new.perform(*Sidekiq.load_json(Sidekiq.dump_json(args))) - true - end end end end diff --git a/lib/sidekiq/worker.rb b/lib/sidekiq/worker.rb index f673cdf1..c798f076 100644 --- a/lib/sidekiq/worker.rb +++ b/lib/sidekiq/worker.rb @@ -38,8 +38,7 @@ module Sidekiq def perform_in_queue(queue, *args) client_push('queue' => queue, 'class' => self, 'args' => args) end - alias_method :perform_at_queue, :perform_in_queue - + def perform_in(interval, *args) int = interval.to_f ts = (int < 1_000_000_000 ? Time.now.to_f + int : int) diff --git a/test/test_client.rb b/test/test_client.rb index 52fce1d9..ce90478c 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -69,6 +69,27 @@ class TestClient < MiniTest::Unit::TestCase @redis.verify end + it 'handles perform_in_queue' do + @redis.expect :rpush, 1, ['queue:custom_queue', String] + pushed = MyWorker.perform_in_queue(:custom_queue, 1, 2) + assert pushed + @redis.verify + end + + it 'handles perform_in_queue on failure' do + @redis.expect :rpush, nil, ['queue:custom_queue', String] + pushed = MyWorker.perform_in_queue(:custom_queue, 1, 2) + refute pushed + @redis.verify + end + + it 'enqueues messages to redis' do + @redis.expect :rpush, 1, ['queue:custom_queue', String] + pushed = Sidekiq::Client.enqueue_to(:custom_queue, MyWorker, 1, 2) + assert pushed + @redis.verify + end + class QueuedWorker include Sidekiq::Worker sidekiq_options :queue => :flimflam, :timeout => 1 From fb53a27c89146efd0b7bb955371a9958273ae039 Mon Sep 17 00:00:00 2001 From: Niels Kristian Date: Tue, 21 Aug 2012 11:06:48 +0200 Subject: [PATCH 6/6] Removed queue setting from processor --- lib/sidekiq/processor.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/sidekiq/processor.rb b/lib/sidekiq/processor.rb index 61c6490f..193bd8e1 100644 --- a/lib/sidekiq/processor.rb +++ b/lib/sidekiq/processor.rb @@ -37,7 +37,6 @@ module Sidekiq msg = Sidekiq.load_json(msgstr) klass = constantize(msg['class']) worker = klass.new - worker.class.sidekiq_options(:queue => queue) stats(worker, msg, queue) do Sidekiq.server_middleware.invoke(worker, msg, queue) do