resque-bus 0.3.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +2 -3
- data/README.mdown +50 -64
- data/Rakefile +0 -1
- data/lib/resque-bus.rb +21 -283
- data/lib/resque_bus/adapter.rb +66 -0
- data/lib/resque_bus/compatibility/deprecated.rb +38 -0
- data/lib/resque_bus/compatibility/driver.rb +10 -0
- data/lib/resque_bus/compatibility/heartbeat.rb +10 -0
- data/lib/resque_bus/compatibility/publisher.rb +13 -0
- data/lib/resque_bus/compatibility/rider.rb +32 -0
- data/lib/resque_bus/compatibility/subscriber.rb +8 -0
- data/lib/resque_bus/compatibility/task_manager.rb +8 -0
- data/lib/resque_bus/server.rb +6 -5
- data/lib/resque_bus/server/views/bus.erb +2 -2
- data/lib/resque_bus/tasks.rb +46 -46
- data/lib/resque_bus/version.rb +2 -4
- data/resque-bus.gemspec +5 -12
- data/spec/adapter/compatibility_spec.rb +97 -0
- data/spec/adapter/integration_spec.rb +111 -0
- data/spec/adapter/publish_at_spec.rb +50 -0
- data/spec/adapter/retry_spec.rb +47 -0
- data/spec/adapter/support.rb +23 -0
- data/spec/adapter_spec.rb +14 -0
- data/spec/application_spec.rb +62 -62
- data/spec/config_spec.rb +83 -0
- data/spec/dispatch_spec.rb +6 -6
- data/spec/driver_spec.rb +62 -53
- data/spec/heartbeat_spec.rb +4 -4
- data/spec/integration_spec.rb +2 -2
- data/spec/matcher_spec.rb +29 -29
- data/spec/publish_spec.rb +62 -38
- data/spec/publisher_spec.rb +7 -0
- data/spec/rider_spec.rb +14 -66
- data/spec/spec_helper.rb +25 -28
- data/spec/subscriber_spec.rb +194 -176
- data/spec/subscription_list_spec.rb +1 -1
- data/spec/subscription_spec.rb +1 -1
- data/spec/worker_spec.rb +32 -0
- metadata +75 -91
- data/lib/resque_bus/application.rb +0 -115
- data/lib/resque_bus/dispatch.rb +0 -61
- data/lib/resque_bus/driver.rb +0 -30
- data/lib/resque_bus/heartbeat.rb +0 -106
- data/lib/resque_bus/local.rb +0 -34
- data/lib/resque_bus/matcher.rb +0 -81
- data/lib/resque_bus/publisher.rb +0 -12
- data/lib/resque_bus/rider.rb +0 -54
- data/lib/resque_bus/subscriber.rb +0 -63
- data/lib/resque_bus/subscription.rb +0 -55
- data/lib/resque_bus/subscription_list.rb +0 -53
- data/lib/resque_bus/task_manager.rb +0 -52
- data/lib/resque_bus/util.rb +0 -42
- data/lib/tasks/resquebus.rake +0 -2
- data/spec/publish_at_spec.rb +0 -74
- data/spec/redis_spec.rb +0 -13
data/spec/publish_spec.rb
CHANGED
@@ -1,72 +1,96 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Publishing an event" do
|
4
|
-
|
4
|
+
|
5
5
|
before(:each) do
|
6
6
|
Timecop.freeze
|
7
|
-
|
7
|
+
QueueBus.stub(:generate_uuid).and_return("idfhlkj")
|
8
8
|
end
|
9
9
|
after(:each) do
|
10
10
|
Timecop.return
|
11
11
|
end
|
12
|
-
let(:bus_attrs) { {"
|
12
|
+
let(:bus_attrs) { {"bus_class_proxy"=>"QueueBus::Driver",
|
13
|
+
"bus_published_at" => Time.now.to_i,
|
13
14
|
"bus_id"=>"#{Time.now.to_i}-idfhlkj",
|
14
15
|
"bus_app_hostname" => `hostname 2>&1`.strip.sub(/.local/,'')} }
|
15
|
-
|
16
|
+
|
16
17
|
it "should add it to Redis" do
|
17
18
|
hash = {:one => 1, "two" => "here", "id" => 12 }
|
18
19
|
event_name = "event_name"
|
19
|
-
|
20
|
-
val =
|
20
|
+
|
21
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
21
22
|
val.should == nil
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
val =
|
23
|
+
|
24
|
+
QueueBus.publish(event_name, hash)
|
25
|
+
|
26
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
26
27
|
hash = JSON.parse(val)
|
27
|
-
hash["class"].should == "
|
28
|
-
hash["args"].should ==
|
29
|
-
|
28
|
+
hash["class"].should == "QueueBus::Worker"
|
29
|
+
hash["args"].size.should == 1
|
30
|
+
JSON.parse(hash["args"].first).should == {"bus_event_type" => event_name, "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs)
|
31
|
+
|
30
32
|
end
|
31
|
-
|
33
|
+
|
32
34
|
it "should use the id if given" do
|
33
35
|
hash = {:one => 1, "two" => "here", "bus_id" => "app-given" }
|
34
36
|
event_name = "event_name"
|
35
|
-
|
36
|
-
val =
|
37
|
+
|
38
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
37
39
|
val.should == nil
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
val =
|
40
|
+
|
41
|
+
QueueBus.publish(event_name, hash)
|
42
|
+
|
43
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
42
44
|
hash = JSON.parse(val)
|
43
|
-
hash["class"].should == "
|
44
|
-
hash["args"].should ==
|
45
|
+
hash["class"].should == "QueueBus::Worker"
|
46
|
+
hash["args"].size.should == 1
|
47
|
+
JSON.parse(hash["args"].first).should == {"bus_event_type" => event_name, "two"=>"here", "one"=>1}.merge(bus_attrs).merge("bus_id" => 'app-given')
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should add metadata via callback" do
|
51
|
+
myval = 0
|
52
|
+
QueueBus.before_publish = lambda { |att|
|
53
|
+
att["mine"] = 4
|
54
|
+
myval += 1
|
55
|
+
}
|
56
|
+
|
57
|
+
hash = {:one => 1, "two" => "here", "bus_id" => "app-given" }
|
58
|
+
event_name = "event_name"
|
59
|
+
|
60
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
61
|
+
val.should == nil
|
62
|
+
|
63
|
+
QueueBus.publish(event_name, hash)
|
64
|
+
|
65
|
+
|
66
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
67
|
+
hash = JSON.parse(val)
|
68
|
+
att = JSON.parse(hash["args"].first)
|
69
|
+
att["mine"].should == 4
|
70
|
+
myval.should == 1
|
45
71
|
end
|
46
|
-
|
47
72
|
|
48
|
-
|
49
73
|
it "should set the timezone and locale if available" do
|
50
74
|
defined?(I18n).should be_nil
|
51
|
-
Time.respond_to?(:zone).should
|
52
|
-
|
75
|
+
Time.respond_to?(:zone).should eq(false)
|
76
|
+
|
53
77
|
stub_const("I18n", Class.new)
|
54
|
-
I18n.
|
55
|
-
|
56
|
-
Time.
|
57
|
-
|
78
|
+
I18n.stub(:locale).and_return("jp")
|
79
|
+
|
80
|
+
Time.stub(:zone).and_return(double('zone', :name => "EST"))
|
81
|
+
|
58
82
|
hash = {:one => 1, "two" => "here", "bus_id" => "app-given" }
|
59
83
|
event_name = "event_name"
|
60
|
-
|
61
|
-
val =
|
84
|
+
|
85
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
62
86
|
val.should == nil
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
val =
|
87
|
+
|
88
|
+
QueueBus.publish(event_name, hash)
|
89
|
+
|
90
|
+
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
67
91
|
hash = JSON.parse(val)
|
68
|
-
hash["class"].should == "
|
69
|
-
att = hash["args"].first
|
92
|
+
hash["class"].should == "QueueBus::Worker"
|
93
|
+
att = JSON.parse(hash["args"].first)
|
70
94
|
att["bus_locale"].should == "jp"
|
71
95
|
att["bus_timezone"].should == "EST"
|
72
96
|
end
|
data/spec/rider_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
module
|
3
|
+
module QueueBus
|
4
4
|
describe Rider do
|
5
5
|
it "should call execute" do
|
6
|
-
|
6
|
+
QueueBus.should_receive(:dispatcher_execute)
|
7
7
|
Rider.perform("bus_rider_app_key" => "app", "bus_rider_sub_key" => "sub", "ok" => true, "bus_event_type" => "event_name")
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
it "should change the value" do
|
11
|
-
|
11
|
+
QueueBus.dispatch("r1") do
|
12
12
|
subscribe "event_name" do |attributes|
|
13
13
|
Runner1.run(attributes)
|
14
14
|
end
|
@@ -18,74 +18,22 @@ module ResqueBus
|
|
18
18
|
Rider.perform("bus_rider_app_key" => "other", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
|
19
19
|
Runner1.value.should == 1
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should set the timezone and locale if present" do
|
23
|
+
QueueBus.dispatch("r1") do
|
24
|
+
subscribe "event_name" do |attributes|
|
25
|
+
Runner1.run(attributes)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
23
29
|
defined?(I18n).should be_nil
|
24
|
-
Time.respond_to?(:zone).should
|
30
|
+
Time.respond_to?(:zone).should eq(false)
|
25
31
|
|
26
32
|
stub_const("I18n", Class.new)
|
27
33
|
I18n.should_receive(:locale=).with("en")
|
28
34
|
Time.should_receive(:zone=).with("PST")
|
29
|
-
|
35
|
+
|
30
36
|
Rider.perform("bus_locale" => "en", "bus_timezone" => "PST", "bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
|
31
37
|
end
|
32
|
-
|
33
|
-
context "Integration Test" do
|
34
|
-
before(:each) do
|
35
|
-
Resque.redis = "example.com/bad"
|
36
|
-
ResqueBus.original_redis = Resque.redis
|
37
|
-
ResqueBus.redis = "localhost:6379"
|
38
|
-
|
39
|
-
|
40
|
-
ResqueBus.enqueue_to("testing", "::ResqueBus::Rider", { "bus_rider_app_key" => "r2", "bus_rider_sub_key" => "event_name", "bus_event_type" => "event_name", "ok" => true, "bus_rider_queue" => "testing" })
|
41
|
-
|
42
|
-
# like the job does
|
43
|
-
Resque.redis = ResqueBus.redis
|
44
|
-
|
45
|
-
@worker = Resque::Worker.new(:testing)
|
46
|
-
@worker.register_worker
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should use the app's redis within the rider" do
|
50
|
-
host = Resque.redis.instance_variable_get("@redis").instance_variable_get("@client").host
|
51
|
-
host.should == "localhost"
|
52
|
-
|
53
|
-
ResqueBus.dispatch("r2") do
|
54
|
-
subscribe "event_name" do |attributes|
|
55
|
-
host = Resque.redis.instance_variable_get("@redis").instance_variable_get("@client").host
|
56
|
-
if host == "example.com"
|
57
|
-
Runner1.run(attributes)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
host = Resque.redis.instance_variable_get("@redis").instance_variable_get("@client").host
|
63
|
-
host.should == "localhost"
|
64
|
-
|
65
|
-
|
66
|
-
Runner1.value.should == 0
|
67
|
-
perform_next_job @worker
|
68
|
-
Runner1.value.should == 1
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should put it in the failed jobs" do
|
72
|
-
|
73
|
-
ResqueBus.dispatch("r2") do
|
74
|
-
subscribe "event_name" do |attributes|
|
75
|
-
raise "boo!"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
perform_next_job @worker
|
80
|
-
Resque.info[:processed].should == 1
|
81
|
-
Resque.info[:failed].should == 1
|
82
|
-
Resque.info[:pending].should == 1 # requeued
|
83
|
-
|
84
|
-
perform_next_job @worker
|
85
|
-
Resque.info[:processed].should == 2
|
86
|
-
Resque.info[:failed].should == 2
|
87
|
-
Resque.info[:pending].should == 0
|
88
|
-
end
|
89
|
-
end
|
90
38
|
end
|
91
|
-
end
|
39
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,73 +1,70 @@
|
|
1
1
|
require 'timecop'
|
2
|
-
require '
|
3
|
-
require '
|
2
|
+
require 'queue-bus'
|
3
|
+
require 'adapter/support'
|
4
4
|
|
5
|
-
|
5
|
+
reset_test_adapter
|
6
|
+
|
7
|
+
module QueueBus
|
6
8
|
class Runner
|
7
9
|
def self.value
|
8
10
|
@value ||= 0
|
9
11
|
end
|
10
|
-
|
12
|
+
|
11
13
|
def self.attributes
|
12
14
|
@attributes
|
13
15
|
end
|
14
|
-
|
16
|
+
|
15
17
|
def self.run(attrs)
|
16
18
|
@value ||= 0
|
17
19
|
@value += 1
|
18
20
|
@attributes = attrs
|
19
21
|
end
|
20
|
-
|
22
|
+
|
21
23
|
def self.reset
|
22
24
|
@value = nil
|
23
25
|
@attributes = nil
|
24
26
|
end
|
25
27
|
end
|
26
|
-
|
28
|
+
|
27
29
|
class Runner1 < Runner
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
class Runner2 < Runner
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
|
-
def perform_next_job(worker, &block)
|
35
|
-
return unless job = @worker.reserve
|
36
|
-
@worker.perform(job, &block)
|
37
|
-
@worker.done_working
|
38
|
-
end
|
39
|
-
|
40
36
|
def test_sub(event_name, queue="default")
|
41
37
|
matcher = {"bus_event_type" => event_name}
|
42
|
-
|
38
|
+
QueueBus::Subscription.new(queue, event_name, "::QueueBus::Rider", matcher, nil)
|
43
39
|
end
|
44
40
|
|
45
41
|
def test_list(*args)
|
46
|
-
out =
|
42
|
+
out = QueueBus::SubscriptionList.new
|
47
43
|
args.each do |sub|
|
48
44
|
out.add(sub)
|
49
45
|
end
|
50
46
|
out
|
51
47
|
end
|
52
48
|
|
53
|
-
|
54
|
-
Resque::Scheduler.mute = true
|
55
|
-
|
56
49
|
RSpec.configure do |config|
|
57
|
-
config.
|
58
|
-
|
50
|
+
config.mock_with :rspec do |c|
|
51
|
+
c.syntax = :should
|
52
|
+
end
|
53
|
+
config.expect_with :rspec do |c|
|
54
|
+
c.syntax = :should
|
55
|
+
end
|
56
|
+
|
59
57
|
config.before(:each) do
|
60
|
-
|
58
|
+
reset_test_adapter
|
59
|
+
ResqueBus.show_deprecations = true
|
61
60
|
end
|
62
61
|
config.after(:each) do
|
63
62
|
begin
|
64
|
-
|
63
|
+
QueueBus.redis { |redis| redis.flushall }
|
65
64
|
rescue
|
66
65
|
end
|
67
|
-
|
68
|
-
|
69
|
-
|
66
|
+
QueueBus.send(:reset)
|
67
|
+
QueueBus::Runner1.reset
|
68
|
+
QueueBus::Runner2.reset
|
70
69
|
end
|
71
70
|
end
|
72
|
-
|
73
|
-
ResqueBus.redis.namespace = "resquebus_test"
|
data/spec/subscriber_spec.rb
CHANGED
@@ -1,78 +1,82 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
include ResqueBus::Subscriber
|
5
|
-
@queue = "myqueue"
|
6
|
-
|
7
|
-
application :my_thing
|
8
|
-
subscribe :thing_filter, :x => "y"
|
9
|
-
subscribe :event_sub
|
10
|
-
|
11
|
-
def event_sub(attributes)
|
12
|
-
ResqueBus::Runner1.run(attributes)
|
13
|
-
end
|
14
|
-
|
15
|
-
def thing_filter(attributes)
|
16
|
-
ResqueBus::Runner2.run(attributes)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class SubscriberTest2
|
21
|
-
include ResqueBus::Subscriber
|
22
|
-
application :test2
|
23
|
-
subscribe :test2, "value" => :present
|
24
|
-
transform :make_an_int
|
25
|
-
|
26
|
-
def self.make_an_int(attributes)
|
27
|
-
attributes["value"].to_s.length
|
28
|
-
end
|
29
|
-
|
30
|
-
def test2(int)
|
31
|
-
ResqueBus::Runner1.run("transformed"=>int)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
module SubModule
|
36
|
-
class SubscriberTest3
|
37
|
-
include ResqueBus::Subscriber
|
38
|
-
|
39
|
-
subscribe_queue :sub_queue1, :test3, :bus_event_type => "the_event"
|
40
|
-
subscribe_queue :sub_queue2, :the_event
|
41
|
-
subscribe :other, :bus_event_type => "other_event"
|
42
|
-
|
43
|
-
def test3(attributes)
|
44
|
-
ResqueBus::Runner1.run(attributes)
|
45
|
-
end
|
46
|
-
|
47
|
-
def the_event(attributes)
|
48
|
-
ResqueBus::Runner2.run(attributes)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class SubscriberTest4
|
53
|
-
include ResqueBus::Subscriber
|
54
|
-
|
55
|
-
subscribe_queue :sub_queue1, :test4
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
module ResqueBus
|
60
|
-
describe Subscriber do
|
3
|
+
describe QueueBus::Subscriber do
|
61
4
|
let(:attributes) { {"x" => "y"} }
|
62
5
|
let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i} }
|
63
|
-
|
6
|
+
|
64
7
|
before(:each) do
|
65
|
-
|
8
|
+
class SubscriberTest1
|
9
|
+
include QueueBus::Subscriber
|
10
|
+
@queue = "myqueue"
|
11
|
+
|
12
|
+
application :my_thing
|
13
|
+
subscribe :thing_filter, :x => "y"
|
14
|
+
subscribe :event_sub
|
15
|
+
|
16
|
+
def event_sub(attributes)
|
17
|
+
QueueBus::Runner1.run(attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
def thing_filter(attributes)
|
21
|
+
QueueBus::Runner2.run(attributes)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class SubscriberTest2
|
26
|
+
include QueueBus::Subscriber
|
27
|
+
application :test2
|
28
|
+
subscribe :test2, "value" => :present
|
29
|
+
transform :make_an_int
|
30
|
+
|
31
|
+
def self.make_an_int(attributes)
|
32
|
+
attributes["value"].to_s.length
|
33
|
+
end
|
34
|
+
|
35
|
+
def test2(int)
|
36
|
+
QueueBus::Runner1.run("transformed"=>int)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module SubModule
|
41
|
+
class SubscriberTest3
|
42
|
+
include QueueBus::Subscriber
|
43
|
+
|
44
|
+
subscribe_queue :sub_queue1, :test3, :bus_event_type => "the_event"
|
45
|
+
subscribe_queue :sub_queue2, :the_event
|
46
|
+
subscribe :other, :bus_event_type => "other_event"
|
47
|
+
|
48
|
+
def test3(attributes)
|
49
|
+
QueueBus::Runner1.run(attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
def the_event(attributes)
|
53
|
+
QueueBus::Runner2.run(attributes)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class SubscriberTest4
|
58
|
+
include QueueBus::Subscriber
|
59
|
+
|
60
|
+
subscribe_queue :sub_queue1, :test4
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Timecop.freeze
|
65
|
+
QueueBus::TaskManager.new(false).subscribe!
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
|
+
after(:each) do
|
69
|
+
Timecop.return
|
70
|
+
end
|
71
|
+
|
68
72
|
it "should have the application" do
|
69
73
|
SubscriberTest1.app_key.should == "my_thing"
|
70
74
|
SubModule::SubscriberTest3.app_key.should == "sub_module"
|
71
75
|
SubModule::SubscriberTest4.app_key.should == "sub_module"
|
72
76
|
end
|
73
|
-
|
77
|
+
|
74
78
|
it "should be able to transform the attributes" do
|
75
|
-
dispatcher =
|
79
|
+
dispatcher = QueueBus.dispatcher_by_key("test2")
|
76
80
|
all = dispatcher.subscriptions.all
|
77
81
|
all.size.should == 1
|
78
82
|
|
@@ -82,154 +86,169 @@ module ResqueBus
|
|
82
86
|
sub.key.should == "SubscriberTest2.test2"
|
83
87
|
sub.matcher.filters.should == {"value"=>"bus_special_value_present"}
|
84
88
|
|
85
|
-
Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"nice"))
|
86
|
-
|
87
|
-
hash = JSON.parse(
|
88
|
-
hash["class"].should == "
|
89
|
-
hash["args"].should ==
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
hash
|
105
|
-
hash["
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
89
|
+
QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"nice"))
|
90
|
+
|
91
|
+
hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
|
92
|
+
hash["class"].should == "QueueBus::Worker"
|
93
|
+
hash["args"].size.should == 1
|
94
|
+
JSON.parse(hash["args"].first).should eq({"bus_class_proxy" => "SubscriberTest2", "bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
|
95
|
+
"bus_event_type" => "something2", "value"=>"nice", "x"=>"y"}.merge(bus_attrs))
|
96
|
+
|
97
|
+
QueueBus::Runner1.value.should == 0
|
98
|
+
QueueBus::Runner2.value.should == 0
|
99
|
+
QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
|
100
|
+
QueueBus::Runner1.value.should == 1
|
101
|
+
QueueBus::Runner2.value.should == 0
|
102
|
+
|
103
|
+
QueueBus::Runner1.attributes.should == {"transformed" => 4}
|
104
|
+
|
105
|
+
|
106
|
+
QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"12"))
|
107
|
+
|
108
|
+
hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
|
109
|
+
hash["class"].should == "QueueBus::Worker"
|
110
|
+
hash["args"].size.should == 1
|
111
|
+
JSON.parse(hash["args"].first).should == {"bus_class_proxy" => "SubscriberTest2", "bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
|
112
|
+
"bus_event_type" => "something2", "value"=>"12", "x"=>"y"}.merge(bus_attrs)
|
113
|
+
|
114
|
+
QueueBus::Runner1.value.should == 1
|
115
|
+
QueueBus::Runner2.value.should == 0
|
116
|
+
QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
|
117
|
+
QueueBus::Runner1.value.should == 2
|
118
|
+
QueueBus::Runner2.value.should == 0
|
119
|
+
|
120
|
+
QueueBus::Runner1.attributes.should == {"transformed" => 2}
|
115
121
|
end
|
116
|
-
|
117
|
-
|
122
|
+
|
123
|
+
|
118
124
|
it "should put in a different queue" do
|
119
|
-
dispatcher =
|
125
|
+
dispatcher = QueueBus.dispatcher_by_key("sub_module")
|
120
126
|
all = dispatcher.subscriptions.all
|
121
127
|
all.size.should == 4
|
122
|
-
|
128
|
+
|
123
129
|
sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.test3"}.first
|
124
130
|
sub.queue_name.should == "sub_queue1"
|
125
131
|
sub.class_name.should == "SubModule::SubscriberTest3"
|
126
132
|
sub.key.should == "SubModule::SubscriberTest3.test3"
|
127
133
|
sub.matcher.filters.should == {"bus_event_type"=>"the_event"}
|
128
|
-
|
134
|
+
|
129
135
|
sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.the_event"}.first
|
130
136
|
sub.queue_name.should == "sub_queue2"
|
131
137
|
sub.class_name.should == "SubModule::SubscriberTest3"
|
132
138
|
sub.key.should == "SubModule::SubscriberTest3.the_event"
|
133
139
|
sub.matcher.filters.should == {"bus_event_type"=>"the_event"}
|
134
|
-
|
140
|
+
|
135
141
|
sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.other"}.first
|
136
142
|
sub.queue_name.should == "sub_module_default"
|
137
143
|
sub.class_name.should == "SubModule::SubscriberTest3"
|
138
144
|
sub.key.should == "SubModule::SubscriberTest3.other"
|
139
145
|
sub.matcher.filters.should == {"bus_event_type"=>"other_event"}
|
140
|
-
|
146
|
+
|
141
147
|
sub = all.select{ |s| s.key == "SubModule::SubscriberTest4.test4"}.first
|
142
148
|
sub.queue_name.should == "sub_queue1"
|
143
149
|
sub.class_name.should == "SubModule::SubscriberTest4"
|
144
150
|
sub.key.should == "SubModule::SubscriberTest4.test4"
|
145
151
|
sub.matcher.filters.should == {"bus_event_type"=>"test4"}
|
146
|
-
|
147
|
-
Driver.perform(attributes.merge("bus_event_type" => "the_event"))
|
148
|
-
|
149
|
-
hash = JSON.parse(
|
150
|
-
hash["class"].should == "
|
151
|
-
hash["args"].should ==
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
hash
|
162
|
-
hash["
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
152
|
+
|
153
|
+
QueueBus::Driver.perform(attributes.merge("bus_event_type" => "the_event"))
|
154
|
+
|
155
|
+
hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue1") })
|
156
|
+
hash["class"].should == "QueueBus::Worker"
|
157
|
+
hash["args"].size.should == 1
|
158
|
+
JSON.parse(hash["args"].first).should == {"bus_class_proxy" => "SubModule::SubscriberTest3", "bus_rider_app_key"=>"sub_module", "bus_rider_sub_key"=>"SubModule::SubscriberTest3.test3", "bus_rider_queue" => "sub_queue1", "bus_rider_class_name"=>"SubModule::SubscriberTest3",
|
159
|
+
"bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs)
|
160
|
+
|
161
|
+
QueueBus::Runner1.value.should == 0
|
162
|
+
QueueBus::Runner2.value.should == 0
|
163
|
+
QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
|
164
|
+
QueueBus::Runner1.value.should == 1
|
165
|
+
QueueBus::Runner2.value.should == 0
|
166
|
+
|
167
|
+
hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue2") })
|
168
|
+
hash["class"].should == "QueueBus::Worker"
|
169
|
+
hash["args"].size.should == 1
|
170
|
+
JSON.parse(hash["args"].first).should == {"bus_class_proxy" => "SubModule::SubscriberTest3", "bus_rider_app_key"=>"sub_module", "bus_rider_sub_key"=>"SubModule::SubscriberTest3.the_event", "bus_rider_queue" => "sub_queue2", "bus_rider_class_name"=>"SubModule::SubscriberTest3",
|
171
|
+
"bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs)
|
172
|
+
|
173
|
+
QueueBus::Runner1.value.should == 1
|
174
|
+
QueueBus::Runner2.value.should == 0
|
175
|
+
QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
|
176
|
+
QueueBus::Runner1.value.should == 1
|
177
|
+
QueueBus::Runner2.value.should == 1
|
170
178
|
end
|
171
|
-
|
179
|
+
|
172
180
|
it "should subscribe to default and attributes" do
|
173
|
-
dispatcher =
|
181
|
+
dispatcher = QueueBus.dispatcher_by_key("my_thing")
|
174
182
|
all = dispatcher.subscriptions.all
|
175
|
-
|
183
|
+
|
176
184
|
sub = all.select{ |s| s.key == "SubscriberTest1.event_sub"}.first
|
177
185
|
sub.queue_name.should == "myqueue"
|
178
186
|
sub.class_name.should == "SubscriberTest1"
|
179
187
|
sub.key.should == "SubscriberTest1.event_sub"
|
180
188
|
sub.matcher.filters.should == {"bus_event_type"=>"event_sub"}
|
181
|
-
|
189
|
+
|
182
190
|
sub = all.select{ |s| s.key == "SubscriberTest1.thing_filter"}.first
|
183
191
|
sub.queue_name.should == "myqueue"
|
184
192
|
sub.class_name.should == "SubscriberTest1"
|
185
193
|
sub.key.should == "SubscriberTest1.thing_filter"
|
186
194
|
sub.matcher.filters.should == {"x"=>"y"}
|
187
|
-
|
188
|
-
Driver.perform(attributes.merge("bus_event_type" => "event_sub"))
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
195
|
+
|
196
|
+
QueueBus::Driver.perform(attributes.merge("bus_event_type" => "event_sub"))
|
197
|
+
QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["myqueue"]
|
198
|
+
|
199
|
+
pop1 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
|
200
|
+
pop2 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
|
201
|
+
|
202
|
+
if JSON.parse(pop1["args"].first)["bus_rider_sub_key"] == "SubscriberTest1.thing_filter"
|
203
|
+
hash1 = pop1
|
204
|
+
hash2 = pop2
|
205
|
+
else
|
206
|
+
hash1 = pop2
|
207
|
+
hash2 = pop1
|
208
|
+
end
|
209
|
+
|
210
|
+
hash1["class"].should == "QueueBus::Worker"
|
211
|
+
JSON.parse(hash1["args"].first).should eq({"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
|
212
|
+
"bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs))
|
213
|
+
|
214
|
+
QueueBus::Runner1.value.should == 0
|
215
|
+
QueueBus::Runner2.value.should == 0
|
216
|
+
QueueBus::Util.constantize(hash1["class"]).perform(*hash1["args"])
|
217
|
+
QueueBus::Runner1.value.should == 0
|
218
|
+
QueueBus::Runner2.value.should == 1
|
219
|
+
|
220
|
+
hash2["class"].should == "QueueBus::Worker"
|
221
|
+
hash2["args"].size.should == 1
|
222
|
+
JSON.parse(hash2["args"].first).should == {"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
|
223
|
+
"bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs)
|
224
|
+
|
225
|
+
QueueBus::Runner1.value.should == 0
|
226
|
+
QueueBus::Runner2.value.should == 1
|
227
|
+
QueueBus::Util.constantize(hash2["class"]).perform(*hash2["args"])
|
228
|
+
QueueBus::Runner1.value.should == 1
|
229
|
+
QueueBus::Runner2.value.should == 1
|
230
|
+
|
231
|
+
QueueBus::Driver.perform(attributes.merge("bus_event_type" => "event_sub_other"))
|
232
|
+
QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["myqueue"]
|
233
|
+
|
234
|
+
hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
|
235
|
+
hash["class"].should == "QueueBus::Worker"
|
236
|
+
hash["args"].size.should == 1
|
237
|
+
JSON.parse(hash["args"].first).should == {"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
|
238
|
+
"bus_event_type" => "event_sub_other", "x" => "y"}.merge(bus_attrs)
|
239
|
+
|
240
|
+
QueueBus::Runner1.value.should == 1
|
241
|
+
QueueBus::Runner2.value.should == 1
|
242
|
+
QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
|
243
|
+
QueueBus::Runner1.value.should == 1
|
244
|
+
QueueBus::Runner2.value.should == 2
|
245
|
+
|
246
|
+
QueueBus::Driver.perform({"x"=>"z"}.merge("bus_event_type" => "event_sub_other"))
|
247
|
+
QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["myqueue"]
|
248
|
+
|
249
|
+
QueueBus.redis { |redis| redis.lpop("queue:myqueue") }.should be_nil
|
231
250
|
end
|
232
|
-
|
251
|
+
|
233
252
|
describe ".perform" do
|
234
253
|
let(:attributes) { {"bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_locale" => "en", "bus_timezone" => "PST"} }
|
235
254
|
it "should call the method based on key" do
|
@@ -238,15 +257,14 @@ module ResqueBus
|
|
238
257
|
end
|
239
258
|
it "should set the timezone and locale if present" do
|
240
259
|
defined?(I18n).should be_nil
|
241
|
-
Time.respond_to?(:zone).should
|
260
|
+
Time.respond_to?(:zone).should eq(false)
|
242
261
|
|
243
262
|
stub_const("I18n", Class.new)
|
244
263
|
I18n.should_receive(:locale=).with("en")
|
245
264
|
Time.should_receive(:zone=).with("PST")
|
246
|
-
|
265
|
+
|
247
266
|
SubscriberTest1.any_instance.should_receive(:event_sub)
|
248
267
|
SubscriberTest1.perform(attributes)
|
249
268
|
end
|
250
269
|
end
|
251
270
|
end
|
252
|
-
end
|