action_subscriber 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/action_subscriber.rb +5 -0
- data/lib/action_subscriber/configuration.rb +3 -2
- data/lib/action_subscriber/publisher.rb +44 -0
- data/lib/action_subscriber/version.rb +1 -1
- data/spec/integration/basic_subscriber_spec.rb +2 -4
- data/spec/lib/action_subscriber/publisher_spec.rb +35 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81810a9b42c428f46bdf5c4ee1ddae28c8b1c0d9
|
4
|
+
data.tar.gz: 592323b59305d76ab8eb989ddb1f48f02c05974f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e66ba065d0ce05d32e9981915c4e3008f9216e5e671e02dbc37784645b14b68b42bf0c53200fbae027290eecb6231f7b5cccfb30ecab582d9519bd38e8865e9
|
7
|
+
data.tar.gz: 3215858c226c65d6c54929381b41d7dcb9ca5639ab2b5f2b1bf2dc38e19eb3b655a5588e91fffe58a0c25055605caff77b55938538baebe7699c047dbf889dff
|
data/lib/action_subscriber.rb
CHANGED
@@ -20,6 +20,7 @@ require "action_subscriber/subscribable"
|
|
20
20
|
require "action_subscriber/bunny/subscriber"
|
21
21
|
require "action_subscriber/march_hare/subscriber"
|
22
22
|
require "action_subscriber/babou"
|
23
|
+
require "action_subscriber/publisher"
|
23
24
|
require "action_subscriber/threadpool"
|
24
25
|
require "action_subscriber/base"
|
25
26
|
|
@@ -92,3 +93,7 @@ module ActionSubscriber
|
|
92
93
|
end
|
93
94
|
|
94
95
|
require "action_subscriber/railtie" if defined?(Rails)
|
96
|
+
|
97
|
+
at_exit do
|
98
|
+
::ActionSubscriber::RabbitConnection.publisher_disconnect!
|
99
|
+
end
|
@@ -11,11 +11,12 @@ module ActionSubscriber
|
|
11
11
|
:pop_interval,
|
12
12
|
:port,
|
13
13
|
:prefetch,
|
14
|
+
:publisher_confirms,
|
14
15
|
:threadpool_size,
|
15
16
|
:timeout,
|
16
17
|
:times_to_pop
|
17
18
|
|
18
|
-
DEFAULTS = {
|
19
|
+
DEFAULTS = {
|
19
20
|
:allow_low_priority_methods => false,
|
20
21
|
:default_exchange => 'events',
|
21
22
|
:heartbeat => 5,
|
@@ -25,6 +26,7 @@ module ActionSubscriber
|
|
25
26
|
:pop_interval => 100, # in milliseconds
|
26
27
|
:port => 5672,
|
27
28
|
:prefetch => 200,
|
29
|
+
:publisher_confirms => false,
|
28
30
|
:threadpool_size => 8,
|
29
31
|
:timeout => 1,
|
30
32
|
:times_to_pop => 8
|
@@ -51,7 +53,6 @@ module ActionSubscriber
|
|
51
53
|
##
|
52
54
|
# Instance Methods
|
53
55
|
#
|
54
|
-
|
55
56
|
def initialize
|
56
57
|
self.decoder = {
|
57
58
|
'application/json' => lambda { |payload| JSON.parse(payload) },
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ActionSubscriber
|
2
|
+
module Publisher
|
3
|
+
# Publish a message to RabbitMQ
|
4
|
+
#
|
5
|
+
# @param [String] route The routing key to use for this message.
|
6
|
+
# @param [String] payload The message you are sending. Should already be encoded as a string.
|
7
|
+
# @param [String] exchange The exchange you want to publish to.
|
8
|
+
# @param [Hash] options hash to set message parameters (e.g. headers)
|
9
|
+
def self.publish(route, payload, exchange_name, options = {})
|
10
|
+
with_exchange(exchange_name) do |exchange|
|
11
|
+
exchange.publish(payload, publishing_options(route, options))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.with_exchange(exchange_name)
|
16
|
+
connection = RabbitConnection.publisher_connection
|
17
|
+
channel = connection.create_channel
|
18
|
+
begin
|
19
|
+
channel.confirm_select if ActionSubscriber.configuration.publisher_confirms
|
20
|
+
exchange = channel.topic(exchange_name)
|
21
|
+
yield(exchange)
|
22
|
+
channel.wait_for_confirms if ActionSubscriber.configuration.publisher_confirms
|
23
|
+
ensure
|
24
|
+
channel.close rescue nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.publishing_options(route, options = {})
|
29
|
+
options[:mandatory] = false unless options.key(:mandatory)
|
30
|
+
options[:persistent] = false unless options.key(:persistent)
|
31
|
+
options[:routing_key] = route
|
32
|
+
|
33
|
+
if ::RUBY_PLATFORM == "java"
|
34
|
+
java_options = {}
|
35
|
+
java_options[:mandatory] = options.delete(:mandatory)
|
36
|
+
java_options[:routing_key] = options.delete(:routing_key)
|
37
|
+
java_options[:properties] = options
|
38
|
+
java_options
|
39
|
+
else
|
40
|
+
options
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -21,10 +21,8 @@ describe "A Basic Subscriber", :integration => true do
|
|
21
21
|
|
22
22
|
context "ActionSubscriber.auto_pop!" do
|
23
23
|
it "routes messages to the right place" do
|
24
|
-
|
25
|
-
|
26
|
-
exchange.publish("Ohai Booked", :routing_key => "greg.basic_push.booked")
|
27
|
-
exchange.publish("Ohai Cancelled", :routing_key => "basic.cancelled")
|
24
|
+
::ActionSubscriber::Publisher.publish("greg.basic_push.booked", "Ohai Booked", "events")
|
25
|
+
::ActionSubscriber::Publisher.publish("basic.cancelled", "Ohai Cancelled", "events")
|
28
26
|
|
29
27
|
verify_expectation_within(2.0) do
|
30
28
|
::ActionSubscriber.auto_pop!
|
@@ -0,0 +1,35 @@
|
|
1
|
+
describe ::ActionSubscriber::Publisher do
|
2
|
+
let(:exchange) { double("Rabbit Exchange") }
|
3
|
+
let(:exchange_name) { "events" }
|
4
|
+
let(:payload) { "Yo Dawg" }
|
5
|
+
let(:route) { "bob.users.created" }
|
6
|
+
|
7
|
+
before { allow(described_class).to receive(:with_exchange).with(exchange_name).and_yield(exchange) }
|
8
|
+
|
9
|
+
describe '.publish' do
|
10
|
+
|
11
|
+
if ::RUBY_PLATFORM == "java"
|
12
|
+
it "publishes to the exchange with default options for march_hare" do
|
13
|
+
expect(exchange).to receive(:publish) do |published_payload, published_options|
|
14
|
+
expect(published_payload).to eq(payload)
|
15
|
+
expect(published_options[:routing_key]).to eq(route)
|
16
|
+
expect(published_options[:mandatory]).to eq(false)
|
17
|
+
expect(published_options[:properties][:persistent]).to eq(false)
|
18
|
+
end
|
19
|
+
|
20
|
+
described_class.publish(route, payload, exchange_name)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
it "publishes to the exchange with default options for bunny" do
|
24
|
+
expect(exchange).to receive(:publish) do |published_payload, published_options|
|
25
|
+
expect(published_payload).to eq(payload)
|
26
|
+
expect(published_options[:routing_key]).to eq(route)
|
27
|
+
expect(published_options[:persistent]).to eq(false)
|
28
|
+
expect(published_options[:mandatory]).to eq(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
described_class.publish(route, payload, exchange_name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_subscriber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Stien
|
@@ -12,7 +12,7 @@ authors:
|
|
12
12
|
autorequire:
|
13
13
|
bindir: bin
|
14
14
|
cert_chain: []
|
15
|
-
date: 2015-09-
|
15
|
+
date: 2015-09-29 00:00:00.000000000 Z
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: activesupport
|
@@ -197,6 +197,7 @@ files:
|
|
197
197
|
- lib/action_subscriber/middleware/error_handler.rb
|
198
198
|
- lib/action_subscriber/middleware/router.rb
|
199
199
|
- lib/action_subscriber/middleware/runner.rb
|
200
|
+
- lib/action_subscriber/publisher.rb
|
200
201
|
- lib/action_subscriber/rabbit_connection.rb
|
201
202
|
- lib/action_subscriber/railtie.rb
|
202
203
|
- lib/action_subscriber/rspec.rb
|
@@ -220,6 +221,7 @@ files:
|
|
220
221
|
- spec/lib/action_subscriber/middleware/error_handler_spec.rb
|
221
222
|
- spec/lib/action_subscriber/middleware/router_spec.rb
|
222
223
|
- spec/lib/action_subscriber/middleware/runner_spec.rb
|
224
|
+
- spec/lib/action_subscriber/publisher_spec.rb
|
223
225
|
- spec/lib/action_subscriber/subscribable_spec.rb
|
224
226
|
- spec/lib/action_subscriber/threadpool_spec.rb
|
225
227
|
- spec/spec_helper.rb
|
@@ -267,6 +269,7 @@ test_files:
|
|
267
269
|
- spec/lib/action_subscriber/middleware/error_handler_spec.rb
|
268
270
|
- spec/lib/action_subscriber/middleware/router_spec.rb
|
269
271
|
- spec/lib/action_subscriber/middleware/runner_spec.rb
|
272
|
+
- spec/lib/action_subscriber/publisher_spec.rb
|
270
273
|
- spec/lib/action_subscriber/subscribable_spec.rb
|
271
274
|
- spec/lib/action_subscriber/threadpool_spec.rb
|
272
275
|
- spec/spec_helper.rb
|