queue-bus 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,5 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem "debugger"
6
- gem "rake"
5
+ gem "rake"
@@ -10,6 +10,7 @@ module QueueBus
10
10
  def enabled!
11
11
  # called the first time we know we are using this adapter
12
12
  # it would be a good spot to require the libraries you're using
13
+ # and modify QueueBus::Worker as needed
13
14
  raise NotImplementedError
14
15
  end
15
16
 
@@ -18,12 +19,12 @@ module QueueBus
18
19
  raise NotImplementedError
19
20
  end
20
21
 
21
- def enqueue(queue_name, klass, hash)
22
+ def enqueue(queue_name, klass, json)
22
23
  # enqueue the given class (Driver/Rider) in your queue
23
24
  raise NotImplementedError
24
25
  end
25
26
 
26
- def enqueue_at(epoch_seconds, queue_name, klass, hash)
27
+ def enqueue_at(epoch_seconds, queue_name, klass, json)
27
28
  # enqueue the given class (Publisher) in your queue to run at given time
28
29
  raise NotImplementedError
29
30
  end
@@ -32,10 +33,6 @@ module QueueBus
32
33
  # if possible, tell a recurring job system to publish every minute
33
34
  raise NotImplementedError
34
35
  end
35
-
36
- def worker_included(base)
37
- # optional method for including more modules in classes that work in the queue
38
- end
39
36
  end
40
37
  end
41
38
  end
@@ -15,12 +15,12 @@ module QueueBus
15
15
  block.call(@redis)
16
16
  end
17
17
 
18
- def enqueue(queue_name, klass, hash)
19
- push(queue_name, :class => klass.to_s, :args => [hash])
18
+ def enqueue(queue_name, klass, json)
19
+ push(queue_name, :class => klass.to_s, :args => [json])
20
20
  end
21
21
 
22
- def enqueue_at(epoch_seconds, queue_name, klass, hash)
23
- item = delayed_job_to_hash_with_queue(queue_name, klass, [hash])
22
+ def enqueue_at(epoch_seconds, queue_name, klass, json)
23
+ item = delayed_job_to_hash_with_queue(queue_name, klass, [json])
24
24
  delayed_push(epoch_seconds, item)
25
25
  end
26
26
 
@@ -1,7 +1,6 @@
1
1
  module QueueBus
2
2
  # fans out an event to multiple queues
3
3
  class Driver
4
- include ::QueueBus::Worker
5
4
 
6
5
  class << self
7
6
  def subscription_matches(attributes)
@@ -21,8 +20,14 @@ module QueueBus
21
20
  subscription_matches(attributes).each do |sub|
22
21
  ::QueueBus.log_worker(" ...sending to #{sub.queue_name} queue with class #{sub.class_name} for app #{sub.app_key} because of subscription: #{sub.key}")
23
22
 
24
- bus_attr = {"bus_driven_at" => Time.now.to_i, "bus_rider_queue" => sub.queue_name, "bus_rider_app_key" => sub.app_key, "bus_rider_sub_key" => sub.key, "bus_rider_class_name" => sub.class_name}
25
- ::QueueBus.enqueue_to(sub.queue_name, sub.class_name, bus_attr.merge(attributes || {}))
23
+ bus_attr = { "bus_driven_at" => Time.now.to_i,
24
+ "bus_rider_queue" => sub.queue_name,
25
+ "bus_rider_app_key" => sub.app_key,
26
+ "bus_rider_sub_key" => sub.key,
27
+ "bus_rider_class_name" => sub.class_name}
28
+ bus_attr = bus_attr.merge(attributes || {})
29
+ bus_attr["bus_class_proxy"] = sub.class_name
30
+ ::QueueBus.enqueue_to(sub.queue_name, ::QueueBus::Worker, bus_attr)
26
31
  end
27
32
  end
28
33
  end
@@ -1,7 +1,6 @@
1
1
  module QueueBus
2
2
  # publishes event about the current time
3
3
  class Heartbeat
4
- include ::QueueBus::Worker
5
4
 
6
5
  class << self
7
6
 
@@ -13,10 +13,15 @@ module QueueBus
13
13
 
14
14
  # looking for subscriptions, not queues
15
15
  subscription_matches(attributes).each do |sub|
16
- bus_attr = {"bus_driven_at" => Time.now.to_i, "bus_rider_queue" => sub.queue_name, "bus_rider_app_key" => sub.app_key, "bus_rider_sub_key" => sub.key, "bus_rider_class_name" => sub.class_name}
16
+ bus_attr = { "bus_driven_at" => Time.now.to_i,
17
+ "bus_rider_queue" => sub.queue_name,
18
+ "bus_rider_app_key" => sub.app_key,
19
+ "bus_rider_sub_key" => sub.key,
20
+ "bus_rider_class_name" => sub.class_name,
21
+ "bus_class_proxy" => sub.class_name}
17
22
  to_publish = bus_attr.merge(attributes || {})
18
23
  if ::QueueBus.local_mode == :standalone
19
- ::QueueBus.enqueue_to(sub.queue_name, sub.class_name, bus_attr.merge(attributes || {}))
24
+ ::QueueBus.enqueue_to(sub.queue_name, ::QueueBus::Worker, bus_attr.merge(attributes || {}))
20
25
  else # defaults to inline mode
21
26
  sub.execute!(to_publish)
22
27
  end
@@ -1,7 +1,6 @@
1
1
  module QueueBus
2
2
  # publishes on a delay
3
3
  class Publisher
4
- include ::QueueBus::Worker
5
4
 
6
5
  class << self
7
6
  def perform(*args)
@@ -48,11 +48,13 @@ module QueueBus
48
48
 
49
49
  def publish(event_type, attributes = {})
50
50
  to_publish = publish_metadata(event_type, attributes)
51
+ to_publish["bus_class_proxy"] = ::QueueBus::Driver.name.to_s
52
+
51
53
  ::QueueBus.log_application("Event published: #{event_type} #{to_publish.inspect}")
52
54
  if local_mode
53
55
  ::QueueBus::Local.perform(to_publish)
54
56
  else
55
- enqueue_to(::QueueBus.incoming_queue, Driver, to_publish)
57
+ enqueue_to(::QueueBus.incoming_queue, ::QueueBus::Worker, to_publish)
56
58
  end
57
59
  end
58
60
 
@@ -60,17 +62,18 @@ module QueueBus
60
62
  to_publish = publish_metadata(event_type, attributes)
61
63
  to_publish["bus_delayed_until"] ||= timestamp_or_epoch.to_i
62
64
  to_publish.delete("bus_published_at") unless attributes["bus_published_at"] # will be put on when it actually does it
65
+ to_publish["bus_class_proxy"] = ::QueueBus::Publisher.name.to_s
63
66
 
64
67
  ::QueueBus.log_application("Event published:#{event_type} #{to_publish.inspect} publish_at: #{timestamp_or_epoch.to_i}")
65
- delayed_enqueue_to(timestamp_or_epoch.to_i, incoming_queue, Publisher, to_publish)
68
+ delayed_enqueue_to(timestamp_or_epoch.to_i, incoming_queue, ::QueueBus::Worker, to_publish)
66
69
  end
67
70
 
68
71
  def enqueue_to(queue_name, klass, hash)
69
- ::QueueBus.adapter.enqueue(queue_name, klass, hash)
72
+ ::QueueBus.adapter.enqueue(queue_name, klass, ::QueueBus::Util.encode(hash || {}))
70
73
  end
71
74
 
72
75
  def delayed_enqueue_to(epoch_seconds, queue_name, klass, hash)
73
- ::QueueBus.adapter.enqueue_at(epoch_seconds, queue_name, klass, hash)
76
+ ::QueueBus.adapter.enqueue_at(epoch_seconds, queue_name, klass, ::QueueBus::Util.encode(hash || {}))
74
77
  end
75
78
 
76
79
  def heartbeat!
@@ -1,8 +1,7 @@
1
1
  module QueueBus
2
2
  # queue'd in each
3
3
  class Rider
4
- include ::QueueBus::Worker
5
-
4
+
6
5
  class << self
7
6
  def perform(attributes = {})
8
7
  sub_key = attributes["bus_rider_sub_key"]
@@ -2,7 +2,6 @@ module QueueBus
2
2
  module Subscriber
3
3
 
4
4
  def self.included(base)
5
- base.send(:include, ::QueueBus::Worker)
6
5
  base.extend ClassMethods
7
6
  end
8
7
 
@@ -1,3 +1,3 @@
1
1
  module QueueBus
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -1,14 +1,23 @@
1
1
  module QueueBus
2
- module Worker
2
+ class Worker
3
3
 
4
- def self.included(base)
5
- ::QueueBus.adapter.worker_included(base)
4
+ def self.perform(attributes)
5
+ klass = nil
6
+ begin
7
+ class_name = attributes["bus_class_proxy"]
8
+ klass = ::QueueBus::Util.constantize(class_name)
9
+ rescue NameError
10
+ # not there anymore
11
+ return
12
+ end
13
+
14
+ klass.perform(attributes)
6
15
  end
7
16
 
8
17
  # all our workers include this one
9
- def perform(*args)
18
+ def perform(attributes)
10
19
  # instance method level support for sidekiq
11
- self.class.perform(*args)
20
+ self.class.perform(attributes)
12
21
  end
13
22
  end
14
23
  end
@@ -9,7 +9,8 @@ describe "Publishing an event in the future" do
9
9
  after(:each) do
10
10
  Timecop.return
11
11
  end
12
- let(:delayed_attrs) { {"bus_delayed_until" => future.to_i,
12
+ let(:delayed_attrs) { {
13
+ "bus_delayed_until" => future.to_i,
13
14
  "bus_id" => "#{now.to_i}-idfhlkj",
14
15
  "bus_app_hostname" => `hostname 2>&1`.strip.sub(/.local/,'')} }
15
16
 
@@ -29,20 +30,22 @@ describe "Publishing an event in the future" do
29
30
  val = QueueBus.redis { |redis| redis.lpop("delayed:#{future.to_i}") }
30
31
  hash = JSON.parse(val)
31
32
 
32
- hash["class"].should == "QueueBus::Publisher"
33
- hash["args"].should == [ {"bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(delayed_attrs) ]
33
+ hash["class"].should == "QueueBus::Worker"
34
+ hash["args"].size.should == 1
35
+ JSON.parse(hash["args"].first).should == {"bus_class_proxy"=>"QueueBus::Publisher", "bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(delayed_attrs)
34
36
  hash["queue"].should == "bus_incoming"
35
37
 
36
38
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
37
39
  val.should == nil # nothing really added
38
40
 
39
41
  Timecop.freeze(worktime)
40
- QueueBus::Publisher.perform(*hash["args"])
42
+ QueueBus::Publisher.perform(JSON.parse(hash["args"].first))
41
43
 
42
44
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
43
45
  hash = JSON.parse(val)
44
- hash["class"].should == "QueueBus::Driver"
45
- hash["args"].should == [ {"bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs) ]
46
+ hash["class"].should == "QueueBus::Worker"
47
+ hash["args"].size.should == 1
48
+ JSON.parse(hash["args"].first).should == {"bus_class_proxy"=>"QueueBus::Driver", "bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs)
46
49
  end
47
50
 
48
51
  end
@@ -12,7 +12,7 @@ module QueueBus
12
12
  Timecop.return
13
13
  end
14
14
 
15
- let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i, "bus_rider_class_name"=>"::QueueBus::Rider"} }
15
+ let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i, "bus_rider_class_name"=>"::QueueBus::Rider", "bus_class_proxy" => "::QueueBus::Rider"} }
16
16
 
17
17
  describe ".subscription_matches" do
18
18
  it "return empty array when none" do
@@ -35,7 +35,7 @@ module QueueBus
35
35
  end
36
36
 
37
37
  describe ".perform" do
38
- let(:attributes) { {"x" => "y"} }
38
+ let(:attributes) { {"x" => "y", "bus_class_proxy" => "ResqueBus::Driver"} }
39
39
 
40
40
  before(:each) do
41
41
  QueueBus.redis { |redis| redis.smembers("queues") }.should == []
@@ -55,8 +55,9 @@ module QueueBus
55
55
  QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["default"]
56
56
 
57
57
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:default") })
58
- hash["class"].should == "::QueueBus::Rider"
59
- hash["args"].should == [ {"bus_rider_app_key"=>"app1", "x" => "y", "bus_event_type" => "event1", "bus_rider_sub_key"=>"event1", "bus_rider_queue" => "default"}.merge(bus_attrs) ]
58
+ hash["class"].should == "QueueBus::Worker"
59
+ hash["args"].size.should == 1
60
+ JSON.parse(hash["args"].first).should == {"bus_rider_app_key"=>"app1", "x" => "y", "bus_event_type" => "event1", "bus_rider_sub_key"=>"event1", "bus_rider_queue" => "default"}.merge(bus_attrs)
60
61
  end
61
62
 
62
63
  it "should queue up to multiple" do
@@ -64,12 +65,14 @@ module QueueBus
64
65
  QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["default", "more"]
65
66
 
66
67
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:more") })
67
- hash["class"].should == "::QueueBus::Rider"
68
- hash["args"].should == [ {"bus_rider_app_key"=>"app2", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event4", "bus_rider_queue" => "more"}.merge(bus_attrs) ]
68
+ hash["class"].should == "QueueBus::Worker"
69
+ hash["args"].size.should == 1
70
+ JSON.parse(hash["args"].first).should == {"bus_rider_app_key"=>"app2", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event4", "bus_rider_queue" => "more"}.merge(bus_attrs)
69
71
 
70
72
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:default") })
71
- hash["class"].should == "::QueueBus::Rider"
72
- hash["args"].should == [ {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs) ]
73
+ hash["class"].should == "QueueBus::Worker"
74
+ hash["args"].size.should == 1
75
+ JSON.parse(hash["args"].first).should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs)
73
76
  end
74
77
 
75
78
  it "should queue up to the same" do
@@ -81,19 +84,25 @@ module QueueBus
81
84
  pop1 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:default") })
82
85
  pop2 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:default") })
83
86
 
84
- if pop1["args"][0]["bus_rider_sub_key"] == "event5"
87
+ pargs1 = JSON.parse(pop1["args"].first)
88
+ pargs2 = JSON.parse(pop2["args"].first)
89
+ if pargs1["bus_rider_sub_key"] == "event5"
85
90
  hash1 = pop1
86
91
  hash2 = pop2
92
+ args1 = pargs1
93
+ args2 = pargs2
87
94
  else
88
95
  hash1 = pop2
89
96
  hash2 = pop1
97
+ args1 = pargs2
98
+ args2 = pargs2
90
99
  end
91
100
 
92
- hash1["class"].should == "::QueueBus::Rider"
93
- hash1["args"][0].should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event5", "bus_rider_queue" => "default"}.merge(bus_attrs)
101
+ hash1["class"].should == "QueueBus::Worker"
102
+ args1.should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event5", "bus_rider_queue" => "default"}.merge(bus_attrs)
94
103
 
95
- hash2["class"].should == "::QueueBus::Rider"
96
- hash2["args"][0].should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs)
104
+ hash2["class"].should == "QueueBus::Worker"
105
+ args2.should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs)
97
106
  end
98
107
  end
99
108
  end
@@ -9,7 +9,8 @@ describe "Publishing an event" do
9
9
  after(:each) do
10
10
  Timecop.return
11
11
  end
12
- let(:bus_attrs) { {"bus_published_at" => Time.now.to_i,
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
 
@@ -24,8 +25,9 @@ describe "Publishing an event" do
24
25
 
25
26
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
26
27
  hash = JSON.parse(val)
27
- hash["class"].should == "QueueBus::Driver"
28
- hash["args"].should == [ {"bus_event_type" => event_name, "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs) ]
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)
29
31
 
30
32
  end
31
33
 
@@ -40,8 +42,9 @@ describe "Publishing an event" do
40
42
 
41
43
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
42
44
  hash = JSON.parse(val)
43
- hash["class"].should == "QueueBus::Driver"
44
- hash["args"].should == [ {"bus_event_type" => event_name, "two"=>"here", "one"=>1}.merge(bus_attrs).merge("bus_id" => 'app-given') ]
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')
45
48
  end
46
49
 
47
50
  it "should add metadata via callback" do
@@ -62,7 +65,7 @@ describe "Publishing an event" do
62
65
 
63
66
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
64
67
  hash = JSON.parse(val)
65
- att = hash["args"].first
68
+ att = JSON.parse(hash["args"].first)
66
69
  att["mine"].should == 4
67
70
  myval.should == 1
68
71
  end
@@ -86,8 +89,8 @@ describe "Publishing an event" do
86
89
 
87
90
  val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
88
91
  hash = JSON.parse(val)
89
- hash["class"].should == "QueueBus::Driver"
90
- att = hash["args"].first
92
+ hash["class"].should == "QueueBus::Worker"
93
+ att = JSON.parse(hash["args"].first)
91
94
  att["bus_locale"].should == "jp"
92
95
  att["bus_timezone"].should == "EST"
93
96
  end
@@ -89,13 +89,14 @@ require 'spec_helper'
89
89
  QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"nice"))
90
90
 
91
91
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
92
- hash["class"].should == "SubscriberTest2"
93
- hash["args"].should eq [ {"bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
94
- "bus_event_type" => "something2", "value"=>"nice", "x"=>"y"}.merge(bus_attrs) ]
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))
95
96
 
96
97
  QueueBus::Runner1.value.should == 0
97
98
  QueueBus::Runner2.value.should == 0
98
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
99
+ QueueBus::Util.constantize(hash["class"]).perform(JSON.parse(hash["args"].first))
99
100
  QueueBus::Runner1.value.should == 1
100
101
  QueueBus::Runner2.value.should == 0
101
102
 
@@ -105,13 +106,14 @@ require 'spec_helper'
105
106
  QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"12"))
106
107
 
107
108
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
108
- hash["class"].should == "SubscriberTest2"
109
- hash["args"].should == [ {"bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
110
- "bus_event_type" => "something2", "value"=>"12", "x"=>"y"}.merge(bus_attrs) ]
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)
111
113
 
112
114
  QueueBus::Runner1.value.should == 1
113
115
  QueueBus::Runner2.value.should == 0
114
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
116
+ QueueBus::Util.constantize(hash["class"]).perform(JSON.parse(hash["args"].first))
115
117
  QueueBus::Runner1.value.should == 2
116
118
  QueueBus::Runner2.value.should == 0
117
119
 
@@ -151,24 +153,26 @@ require 'spec_helper'
151
153
  QueueBus::Driver.perform(attributes.merge("bus_event_type" => "the_event"))
152
154
 
153
155
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue1") })
154
- hash["class"].should == "SubModule::SubscriberTest3"
155
- hash["args"].should == [ {"bus_rider_app_key"=>"sub_module", "bus_rider_sub_key"=>"SubModule::SubscriberTest3.test3", "bus_rider_queue" => "sub_queue1", "bus_rider_class_name"=>"SubModule::SubscriberTest3",
156
- "bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs) ]
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)
157
160
 
158
161
  QueueBus::Runner1.value.should == 0
159
162
  QueueBus::Runner2.value.should == 0
160
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
163
+ QueueBus::Util.constantize(hash["class"]).perform(JSON.parse(hash["args"].first))
161
164
  QueueBus::Runner1.value.should == 1
162
165
  QueueBus::Runner2.value.should == 0
163
166
 
164
167
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue2") })
165
- hash["class"].should == "SubModule::SubscriberTest3"
166
- hash["args"].should == [ {"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",
167
- "bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs) ]
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)
168
172
 
169
173
  QueueBus::Runner1.value.should == 1
170
174
  QueueBus::Runner2.value.should == 0
171
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
175
+ QueueBus::Util.constantize(hash["class"]).perform(JSON.parse(hash["args"].first))
172
176
  QueueBus::Runner1.value.should == 1
173
177
  QueueBus::Runner2.value.should == 1
174
178
  end
@@ -203,23 +207,24 @@ require 'spec_helper'
203
207
  hash2 = pop1
204
208
  end
205
209
 
206
- hash1["class"].should == "SubscriberTest1"
207
- hash1["args"].first.should eq({"bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
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",
208
212
  "bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs))
209
213
 
210
214
  QueueBus::Runner1.value.should == 0
211
215
  QueueBus::Runner2.value.should == 0
212
- QueueBus::Util.constantize(hash1["class"]).perform(*hash1["args"])
216
+ QueueBus::Util.constantize(hash1["class"]).perform(JSON.parse(hash1["args"].first))
213
217
  QueueBus::Runner1.value.should == 0
214
218
  QueueBus::Runner2.value.should == 1
215
219
 
216
- hash2["class"].should == "SubscriberTest1"
217
- hash2["args"].should == [ {"bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
218
- "bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs) ]
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)
219
224
 
220
225
  QueueBus::Runner1.value.should == 0
221
226
  QueueBus::Runner2.value.should == 1
222
- QueueBus::Util.constantize(hash2["class"]).perform(*hash2["args"])
227
+ QueueBus::Util.constantize(hash2["class"]).perform(JSON.parse(hash2["args"].first))
223
228
  QueueBus::Runner1.value.should == 1
224
229
  QueueBus::Runner2.value.should == 1
225
230
 
@@ -227,13 +232,14 @@ require 'spec_helper'
227
232
  QueueBus.redis { |redis| redis.smembers("queues") }.should =~ ["myqueue"]
228
233
 
229
234
  hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
230
- hash["class"].should == "SubscriberTest1"
231
- hash["args"].should == [ {"bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
232
- "bus_event_type" => "event_sub_other", "x" => "y"}.merge(bus_attrs) ]
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)
233
239
 
234
240
  QueueBus::Runner1.value.should == 1
235
241
  QueueBus::Runner2.value.should == 1
236
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
242
+ QueueBus::Util.constantize(hash["class"]).perform(JSON.parse(hash["args"].first))
237
243
  QueueBus::Runner1.value.should == 1
238
244
  QueueBus::Runner2.value.should == 2
239
245
 
@@ -243,10 +249,6 @@ require 'spec_helper'
243
249
  QueueBus.redis { |redis| redis.lpop("queue:myqueue") }.should be_nil
244
250
  end
245
251
 
246
- it "should have gotten the perform method from the ::Worker" do
247
- SubscriberTest1.instance_methods.should include(:perform)
248
- end
249
-
250
252
  describe ".perform" do
251
253
  let(:attributes) { {"bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_locale" => "en", "bus_timezone" => "PST"} }
252
254
  it "should call the method based on key" do
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ module QueueBus
4
+ describe Worker do
5
+ it "should proxy to given class" do
6
+ hash = {"bus_class_proxy" => "QueueBus::Driver", "ok" => true}
7
+ QueueBus::Driver.should_receive(:perform).with(hash)
8
+ QueueBus::Worker.perform(hash)
9
+ end
10
+
11
+ it "should use instance" do
12
+ hash = {"bus_class_proxy" => "QueueBus::Rider", "ok" => true}
13
+ QueueBus::Rider.should_receive(:perform).with(hash)
14
+ QueueBus::Worker.new.perform(hash)
15
+ end
16
+
17
+ it "should not freak out if class not there anymore" do
18
+ hash = {"bus_class_proxy" => "QueueBus::BadClass", "ok" => true}
19
+ lambda {
20
+ QueueBus::Worker.perform(hash)
21
+ }.should_not raise_error
22
+ end
23
+
24
+ it "should raise error if proxy raises error" do
25
+ hash = {"bus_class_proxy" => "QueueBus::Rider", "ok" => true}
26
+ QueueBus::Rider.should_receive(:perform).with(hash).and_raise("rider crash")
27
+ lambda {
28
+ QueueBus::Worker.perform(hash)
29
+ }.should raise_error
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queue-bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-17 00:00:00.000000000 Z
12
+ date: 2015-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -147,6 +147,7 @@ files:
147
147
  - spec/subscriber_spec.rb
148
148
  - spec/subscription_list_spec.rb
149
149
  - spec/subscription_spec.rb
150
+ - spec/worker_spec.rb
150
151
  homepage: ''
151
152
  licenses: []
152
153
  post_install_message:
@@ -189,4 +190,5 @@ test_files:
189
190
  - spec/subscriber_spec.rb
190
191
  - spec/subscription_list_spec.rb
191
192
  - spec/subscription_spec.rb
193
+ - spec/worker_spec.rb
192
194
  has_rdoc: