fastly_nsq 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2f03d7a90d220d61bbbab80806d305f49017361
4
- data.tar.gz: fa5f44e2035bd6a9bf655040498bbdeb9b378b37
3
+ metadata.gz: 99e79a12dff28f51a1a2d2a16fc456afbf8e5e4d
4
+ data.tar.gz: a0c405bf11017a747d3667f05770a21533db4ffc
5
5
  SHA512:
6
- metadata.gz: f34fd7c48a23702fb39115295110d3b1a1aab08f882a514cb83ad9eddcfe7a75e2d67f64a5b6414c543bd557e44c113479d64d45b47e7d010747a8559988169f
7
- data.tar.gz: 4a68b4cab8892638444669e2ce3c3780b9df7596a0ebd9753df6f4dd1909a33f7b4ba96c90fc0027de3066da937d9b49486bcc12f43956b476d7c18c7ff3d31c
6
+ metadata.gz: 0ecb27b7315a6e0296a6d6ce77fd55fcbbad53dfa7c256b1bd72ac200e8f85fff5dff888f35d5f90cb8df6dc9b808bc77096711e308673c2666c135137331b75
7
+ data.tar.gz: a43d3792df3c2f93910d32fa22409eba609f93458404f5defc455a8a08aea069d485072343c54e821b4e87527135d7db1fe6bb862675f169983c482c83fd8db8
data/ChangeLog.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 0.9.0 / 2016-12-20
2
+
3
+ * Add FastlyNsq::Messenger to persist Producers
4
+
1
5
  ### 0.8.0 / 2016-11-23
2
6
 
3
7
  * Upgrade nsq-ruby to version 2.0.3
data/README.md CHANGED
@@ -158,6 +158,40 @@ The task can also define a `call`-able "preprocessor" (called before any `Proces
158
158
  See the [`Rakefile`](examples/Rakefile) file
159
159
  for more detail.
160
160
 
161
+ ### FastlyNsq::Messgener
162
+
163
+ Wrapper around a producer for sending messages and persisting producer objects.
164
+
165
+ ```ruby
166
+ FastlyNsq::Messenger.deliver(message: msg, on_topic: 'my_topic', originating_service: 'my service')
167
+ ```
168
+
169
+ This will use a FastlyNsq::Producer for the given topic or create on if it isn't
170
+ already persisted. Then it will write the passed message to the queue. If you don't set
171
+ the originating service it will use `unknown`
172
+
173
+ You can also set the originating service for all `deliver` calls:
174
+
175
+ ```ruby
176
+ FastlyNsq::Messenger.originating_service = 'some awesome service'
177
+ ```
178
+
179
+ `FastlyNsq::Messenger` can also be used to manage Producer connections
180
+
181
+ ```ruby
182
+ # get a producer:
183
+ producer = FastlyNsq::Messenger.producer_for(topic: 'hot_topic')
184
+
185
+ # get a hash of all persisted producers:
186
+ producers = FastlyNsq::Messenger.producers
187
+
188
+ # terminate a producer
189
+ FastlyNsq::Messenger.terminate_producer(topic: 'hot_topic')
190
+
191
+ # terminate all producers
192
+ FastlyNsq::Messenger.terminate_all_producers
193
+ ```
194
+
161
195
  ### Real vs. Fake
162
196
 
163
197
  The real strategy
@@ -223,16 +257,20 @@ You can also refer to the latter
223
257
  as an example of how
224
258
  you might write your own processor.
225
259
 
226
- ## Acknowledgements
260
+ ## Contributors
227
261
 
228
- * Documentation inspired by [Steve Losh's "Teach Don't Tell"] post.
229
- * Thanks to Wistia for `nsq-ruby`.
262
+ * Adarsh Pandit ([@adarsh](https://github.com/adarsh))
263
+ * Thomas O'Neil ([@alieander](https://github.com/alieander))
264
+ * Joshua Wehner ([@jaw6](https://github.com/jaw6))
265
+ * Lukas Eklund ([@leklund](https://github.com/leklund))
230
266
 
231
- [Steve Losh's "Teach Don't Tell"]: http://stevelosh.com/blog/2013/09/teach-dont-tell/
267
+ ## Acknowledgements
232
268
 
269
+ * Documentation inspired by [Steve Losh's "Teach Don't Tell"](http://stevelosh.com/blog/2013/09/teach-dont-tell/) post.
270
+ * Thanks to Wistia for [`nsq-ruby`](https://github.com/wistia/nsq-ruby).
233
271
 
234
272
  ## Copyright
235
273
 
236
- Copyright (c) 2016 Fastly, Inc under an MIT license.
274
+ Copyright (c) 2016 [Fastly, Inc](https://fastly.com) under an MIT license.
237
275
 
238
276
  See [LICENSE.txt](LICENSE.txt) for details.
data/fastly_nsq.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.summary = 'Fastly NSQ Adapter'
11
11
  gem.description = "Helper classes for Fastly's NSQ Services"
12
12
  gem.license = 'MIT'
13
- gem.authors = ["Tommy O'Neil", 'Adarsh Pandit']
13
+ gem.authors = ["Tommy O'Neil", 'Adarsh Pandit', 'Joshua Wehner', 'Lukas Eklund']
14
14
  gem.email = 'tommy@fastly.com'
15
15
  gem.homepage = 'https://github.com/fastly/fastly-nsq'
16
16
 
data/lib/fastly_nsq.rb CHANGED
@@ -3,6 +3,7 @@ require 'fastly_nsq/consumer'
3
3
  require 'fastly_nsq/fake_backend'
4
4
  require 'fastly_nsq/listener'
5
5
  require 'fastly_nsq/message'
6
+ require 'fastly_nsq/messenger'
6
7
  require 'fastly_nsq/producer'
7
8
  require 'fastly_nsq/strategy'
8
9
  require 'fastly_nsq/tls_options'
@@ -0,0 +1,48 @@
1
+ module FastlyNsq::Messenger
2
+ def self.deliver(message:, on_topic:, originating_service: nil)
3
+ payload = {
4
+ data: message,
5
+ meta: {
6
+ originating_service: originating_service || self.originating_service,
7
+ },
8
+ }
9
+
10
+ producer_for(topic: on_topic) do |producer|
11
+ producer.write payload.to_json
12
+ end
13
+ end
14
+
15
+ def self.originating_service=(service)
16
+ @originating_service = service
17
+ end
18
+
19
+ def self.producer_for(topic:)
20
+ producer = producers[topic]
21
+
22
+ yield producer if block_given?
23
+
24
+ producer
25
+ end
26
+
27
+ def self.producers
28
+ @producers ||= Hash.new { |hash, topic| hash[topic] = FastlyNsq::Producer.new(topic: topic) }
29
+ end
30
+
31
+ def self.terminate_producer(topic:)
32
+ producer_for(topic: topic).terminate
33
+ producers.delete(topic)
34
+ end
35
+
36
+ def self.terminate_all_producers
37
+ producers.each do |topic, producer|
38
+ producer.terminate
39
+ producers.delete(topic)
40
+ end
41
+ end
42
+
43
+ private_class_method
44
+
45
+ def self.originating_service
46
+ @originating_service || 'Unknown'.freeze
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module FastlyNsq
2
- VERSION = '0.8.0'.freeze
2
+ VERSION = '0.9.0'.freeze
3
3
  end
@@ -0,0 +1,104 @@
1
+
2
+ require 'spec_helper'
3
+ require 'json'
4
+
5
+ RSpec.describe FastlyNsq::Messenger do
6
+ let(:message) { { sample: 'sample', message: 'message' } }
7
+ let(:producer) { double 'FastlyNsq::Producer', write: nil, terminate: :terminated }
8
+ let(:origin) { 'originating_service' }
9
+
10
+ let(:expected_attributes) do
11
+ {
12
+ data: message,
13
+ meta: {
14
+ originating_service: 'originating_service',
15
+ },
16
+ }
17
+ end
18
+
19
+ subject { FastlyNsq::Messenger }
20
+
21
+ before do
22
+ FastlyNsq::Messenger.instance_variable_set(:@producers, nil)
23
+ end
24
+
25
+ describe '#deliver' do
26
+ it 'writes a single message on a producer' do
27
+ subject.producers['topic'] = producer
28
+
29
+ subject.deliver message: message, on_topic: 'topic', originating_service: origin
30
+
31
+ expect(producer).to have_received(:write).with(expected_attributes.to_json)
32
+ end
33
+
34
+ it 'uses a Unknown for the default originating_service' do
35
+ subject.producers['topic'] = producer
36
+ expected_attributes[:meta][:originating_service] = 'Unknown'
37
+
38
+ subject.deliver message: message, on_topic: 'topic'
39
+
40
+ expect(producer).to have_received(:write).with(expected_attributes.to_json)
41
+ end
42
+ end
43
+
44
+ describe '#originating_service=' do
45
+ it "set's the default originating service" do
46
+ subject.producers['nanotopic'] = producer
47
+ service = 'nano service'
48
+ subject.originating_service = service
49
+ expected_attributes[:meta][:originating_service] = service
50
+
51
+ subject.deliver message: message, on_topic: 'nanotopic'
52
+
53
+ expect(producer).to have_received(:write).with(expected_attributes.to_json)
54
+
55
+ # reset
56
+ subject.originating_service = nil
57
+ end
58
+ end
59
+
60
+ describe '#producer_for' do
61
+ it 'returns an FastlyNsq::Producer for the given topic' do
62
+ my_producer = subject.producer_for(topic: 'my_topic')
63
+ expect(my_producer).to be_a(FastlyNsq::Producer)
64
+ end
65
+
66
+ it 'persists producers' do
67
+ subject.producers['topic'] = producer
68
+
69
+ my_producer = subject.producer_for(topic: 'topic')
70
+
71
+ expect(my_producer).to eq(producer)
72
+ end
73
+ end
74
+
75
+ describe '#terminate_producer' do
76
+ before do
77
+ subject.producers['topic'] = producer
78
+ subject.terminate_producer(topic: 'topic')
79
+ end
80
+
81
+ it 'terminates a producer' do
82
+ expect(producer).to have_received(:terminate)
83
+ end
84
+
85
+ it 'removes the producer from the persisted producers' do
86
+ expect(subject.producers.key?('topic')).to be(false)
87
+ end
88
+ end
89
+
90
+ describe '#terminate_all_producers' do
91
+ let(:producer_2) { double 'FastlyNsq::Producer', write: nil, terminate: :terminated }
92
+
93
+ it 'terminates all the producers and resets the hash or producers' do
94
+ subject.producers['topic'] = producer
95
+ subject.producers['topic_2'] = producer_2
96
+
97
+ subject.terminate_all_producers
98
+
99
+ expect(producer).to have_received(:terminate)
100
+ expect(producer_2).to have_received(:terminate)
101
+ expect(subject.producers).to be_empty
102
+ end
103
+ end
104
+ end
metadata CHANGED
@@ -1,15 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly_nsq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy O'Neil
8
8
  - Adarsh Pandit
9
+ - Joshua Wehner
10
+ - Lukas Eklund
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
- date: 2016-11-29 00:00:00.000000000 Z
14
+ date: 2016-12-20 00:00:00.000000000 Z
13
15
  dependencies:
14
16
  - !ruby/object:Gem::Dependency
15
17
  name: awesome_print
@@ -199,6 +201,7 @@ files:
199
201
  - lib/fastly_nsq/fake_backend.rb
200
202
  - lib/fastly_nsq/listener.rb
201
203
  - lib/fastly_nsq/message.rb
204
+ - lib/fastly_nsq/messenger.rb
202
205
  - lib/fastly_nsq/producer.rb
203
206
  - lib/fastly_nsq/rake_task.rb
204
207
  - lib/fastly_nsq/strategy.rb
@@ -209,6 +212,7 @@ files:
209
212
  - spec/lib/fastly_nsq/fastly_nsq_spec.rb
210
213
  - spec/lib/fastly_nsq/listener_spec.rb
211
214
  - spec/lib/fastly_nsq/message_spec.rb
215
+ - spec/lib/fastly_nsq/messenger_spec.rb
212
216
  - spec/lib/fastly_nsq/producer_spec.rb
213
217
  - spec/lib/fastly_nsq/rake_task_spec.rb
214
218
  - spec/lib/fastly_nsq/strategy_spec.rb