fastly_nsq 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +6 -3
- data/.travis.yml +1 -1
- data/README.md +23 -73
- data/env_configuration_for_local_gem_tests.yml +0 -1
- data/examples/.sample.env +0 -2
- data/examples/Rakefile +20 -67
- data/lib/fastly_nsq/{message_queue/consumer.rb → consumer.rb} +8 -8
- data/lib/fastly_nsq/fake_backend.rb +100 -0
- data/lib/fastly_nsq/listener.rb +59 -0
- data/lib/fastly_nsq/message.rb +18 -0
- data/lib/fastly_nsq/{message_queue/producer.rb → producer.rb} +8 -8
- data/lib/fastly_nsq/rake_task.rb +39 -43
- data/lib/fastly_nsq/ssl_context.rb +35 -33
- data/lib/fastly_nsq/strategy.rb +38 -0
- data/lib/fastly_nsq/version.rb +1 -1
- data/lib/fastly_nsq.rb +21 -1
- data/spec/lib/fastly_nsq/consumer_spec.rb +68 -0
- data/spec/lib/fastly_nsq/{fake_message_queue_spec.rb → fake_backend_spec.rb} +23 -23
- data/spec/lib/fastly_nsq/listener_spec.rb +116 -0
- data/spec/lib/fastly_nsq/message_spec.rb +24 -0
- data/spec/lib/fastly_nsq/producer_spec.rb +56 -0
- data/spec/lib/fastly_nsq/rake_task_spec.rb +87 -74
- data/spec/lib/fastly_nsq/ssl_context_spec.rb +6 -6
- data/spec/lib/fastly_nsq/{message_queue/strategy_spec.rb → strategy_spec.rb} +6 -5
- data/spec/lib/fastly_nsq_spec.rb +18 -0
- data/spec/spec_helper.rb +1 -6
- metadata +15 -16
- data/lib/fastly_nsq/fake_message_queue.rb +0 -98
- data/lib/fastly_nsq/message_queue/listener.rb +0 -49
- data/lib/fastly_nsq/message_queue/strategy.rb +0 -34
- data/lib/fastly_nsq/message_queue.rb +0 -21
- data/lib/fastly_nsq/sample_message_processor.rb +0 -50
- data/spec/lib/fastly_nsq/message_queue/consumer_spec.rb +0 -60
- data/spec/lib/fastly_nsq/message_queue/listener_spec.rb +0 -90
- data/spec/lib/fastly_nsq/message_queue/producer_spec.rb +0 -49
- data/spec/lib/fastly_nsq/message_queue_spec.rb +0 -32
- data/spec/lib/fastly_nsq/sample_message_processor_spec.rb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2118f8fbefbf2cdd4986db89e65f2fab4c215ac8
|
4
|
+
data.tar.gz: dc20875b82f3b58a4e8eda09148f50f5d1d4e409
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7f6bf648089f9686bed42a7c2e0c16d9bb177843c9b396847139b50ed6e36b0016f7365371c1d51b5503e8ee1080ddcb2ce402a9c8481bc8459948bec09acb8
|
7
|
+
data.tar.gz: e65fd5fbf597af38de0163e30901dfcf8493e3213ae3727067a6bb2660974e8bc191a43898026f5b31d2ecb9f04f9a04450dc11270b1f0bf39b138b7c2b267fe
|
@@ -1,6 +1,8 @@
|
|
1
|
+
__NOTE: Do NOT add any Jira links to this PR description or commits. This is a public repository.__
|
2
|
+
|
1
3
|
Reason for Change
|
2
4
|
===================
|
3
|
-
Describe the big picture of your changes here to communicate why this pull
|
5
|
+
* Describe the big picture of your changes here to communicate why this pull
|
4
6
|
request should be accepted. If it fixes a bug or resolves a feature request,
|
5
7
|
be sure to link to that issue.
|
6
8
|
|
@@ -11,7 +13,7 @@ List of Changes
|
|
11
13
|
|
12
14
|
Risks
|
13
15
|
=====
|
14
|
-
Describe any risks you see to existing systems this fix or feature might have
|
16
|
+
* Describe any risks you see to existing systems this fix or feature might have
|
15
17
|
if those systems where to incorporate this change.
|
16
18
|
|
17
19
|
Checklist
|
@@ -19,6 +21,7 @@ Checklist
|
|
19
21
|
_Put an `x` in the boxes that apply.
|
20
22
|
You can also fill these out after creating the PR._
|
21
23
|
|
24
|
+
- [ ] I have NOT linked to any Jira tickets.
|
22
25
|
- [ ] I have linked to all relevant reference issues or work requests
|
23
26
|
- [ ] I have added tests that prove my fix is effective or that my feature works
|
24
27
|
- [ ] I have added or updated necessary documentation (if appropriate)
|
@@ -27,4 +30,4 @@ You can also fill these out after creating the PR._
|
|
27
30
|
|
28
31
|
Recommended Reviewers
|
29
32
|
=====================
|
30
|
-
@
|
33
|
+
@fastly/billing
|
data/.travis.yml
CHANGED
@@ -9,7 +9,7 @@ script:
|
|
9
9
|
notifications:
|
10
10
|
slack:
|
11
11
|
rooms:
|
12
|
-
|
12
|
+
secure: Uo78FoH8dwSbt7BjTcfb62RXW8aU5h2se+bs0fHSXNrzh6D3Zl+JmwzMkgN8u7M6F0WsOy6RhilYggRYAGds4K1CUi0wGJOG86NaXB4igqBIdHa6f6vzMQZoXxJk8ekc/0NFCj2UV+2Obt6FpeUZvF0Y+u029k5gN0SROfrN+ANtSq+jbI8sdsCCsedXHgDWnGsZS1hHleiRF1Q6VfKnBFcewPP/W49gzbABlKk2MjkVhse6kg+7+LRXfnDxh+aaRXBGYfzmGn4+gS6Jey63U0woAa8TdP/THlwhgvvBS0kwMMaCss+Uq1690lnVV5MX84Fsm4C+A9gbdJmlansLrYRRWm4aaAf8IGlqRTwx3F2yNHTaPA2zmkooFtn1kTuz1J8L9NjsVPPkATMAg3rTIhel+YkVK8q0Q9N9A70+P2u+Rp3htgjxVPMN7Ve9GqejUEaw690Wsp4hCK5tINj8Aq42ET/RpQt4Rzx78Vk4jEK+9UsFuuRH+awAJu/UsnA0JMTq9Smgk2kE69McVNfYPXRIRQXWAkV2dc6FwI8AAmqRusTeyWMK9SOQNXhlmry5ltWfAnHtuKdq9delbc1iOkcj8u9sCBkTz0ZogsI2oHpLlrw2oHFe49kSOkB5HMSQo8IOR7yXHAZ41lmd87/T367bak9HGqsCrweJL7Vz2Mw=
|
13
13
|
on_success: change
|
14
14
|
on_failure: change
|
15
15
|
email:
|
data/README.md
CHANGED
@@ -37,12 +37,7 @@ and `bundle install`.
|
|
37
37
|
|
38
38
|
## Usage
|
39
39
|
|
40
|
-
|
41
|
-
for this gem to work in your application.
|
42
|
-
|
43
|
-
See more information below.
|
44
|
-
|
45
|
-
### `MessageQueue::Producer`
|
40
|
+
### `FastlyNsq::Producer`
|
46
41
|
|
47
42
|
This is a class
|
48
43
|
which provides an adapter to the
|
@@ -57,7 +52,7 @@ message_data = {
|
|
57
52
|
}
|
58
53
|
}
|
59
54
|
|
60
|
-
producer =
|
55
|
+
producer = FastlyNsq::Producer.new(
|
61
56
|
nsqd: ENV.fetch('NSQD_TCP_ADDRESS'),
|
62
57
|
topic: topic,
|
63
58
|
)
|
@@ -77,7 +72,7 @@ ENV['FAKE_QUEUE'] = true
|
|
77
72
|
ENV['FAKE_QUEUE'] = false
|
78
73
|
```
|
79
74
|
|
80
|
-
### `
|
75
|
+
### `FastlyNsq::Consumer`
|
81
76
|
This is a class
|
82
77
|
which provides an adapter to the
|
83
78
|
fake and real NSQ consumers.
|
@@ -85,7 +80,7 @@ These are used to
|
|
85
80
|
read messages off of the queue:
|
86
81
|
|
87
82
|
```ruby
|
88
|
-
consumer =
|
83
|
+
consumer = FastlyNsq::Consumer.new(
|
89
84
|
topic: 'topic',
|
90
85
|
channel: 'channel'
|
91
86
|
)
|
@@ -103,48 +98,44 @@ the mock/real strategy used
|
|
103
98
|
can be switched by setting the
|
104
99
|
`FAKE_QUEUE` environment variable appropriately.
|
105
100
|
|
106
|
-
### `
|
101
|
+
### `FastlyNsq::Listener`
|
107
102
|
|
108
103
|
To process the next message on the queue:
|
109
104
|
|
110
105
|
```ruby
|
111
|
-
topic
|
112
|
-
channel
|
106
|
+
topic = 'user_created'
|
107
|
+
channel = 'my_consuming_service'
|
108
|
+
processor = MessageProcessor
|
113
109
|
|
114
|
-
|
110
|
+
FastlyNsq::Listener.new(topic: topic, channel: channel, processor: processor).go(run_once: true)
|
115
111
|
```
|
116
112
|
|
117
113
|
This will pop the next message
|
118
114
|
off of the queue
|
119
115
|
and send the JSON text body
|
120
|
-
to `MessageProcessor.
|
116
|
+
to `MessageProcessor.process(message_body, topic)`.
|
121
117
|
|
122
118
|
To initiate a blocking loop to process messages continuously:
|
123
119
|
|
124
120
|
```ruby
|
125
|
-
topic
|
126
|
-
channel
|
121
|
+
topic = 'user_created'
|
122
|
+
channel = 'my_consuming_service'
|
123
|
+
processor = MessageProcessor
|
127
124
|
|
128
|
-
|
125
|
+
FastlyNsq::Listener.new(topic: topic, channel: channel, processor: processor).go
|
129
126
|
```
|
130
127
|
|
131
128
|
This will block until
|
132
129
|
there is a new message on the queue,
|
133
130
|
pop the next message
|
134
131
|
off of the queue
|
135
|
-
and send it to `MessageProcessor.
|
132
|
+
and send it to `MessageProcessor.process(message_body, topic)`.
|
136
133
|
|
137
|
-
### `
|
134
|
+
### `FastlyNsq::RakeTask`
|
138
135
|
|
139
|
-
To help facilitate running the `
|
136
|
+
To help facilitate running the `FastlyNsq::Listener` in a blocking fashion
|
140
137
|
outside your application, a simple `RakeTask` is provided.
|
141
138
|
|
142
|
-
NOTE: The rake task expects a
|
143
|
-
`MessageProcessor.topics` method,
|
144
|
-
which must return an array of strings
|
145
|
-
defining the topics to which
|
146
|
-
we would like to listen and process messages.
|
147
|
-
|
148
139
|
The task will listen
|
149
140
|
to all specified topics,
|
150
141
|
each in a separate thread.
|
@@ -155,26 +146,14 @@ Using a block:
|
|
155
146
|
```ruby
|
156
147
|
require 'fastly_nsq/rake_task'
|
157
148
|
|
158
|
-
|
149
|
+
FastlyNsq::RakeTask.new(:listen_task) do |task|
|
159
150
|
task.channel = 'some_channel'
|
151
|
+
task.topics = {
|
152
|
+
'some_topic' => SomeMessageProcessor
|
153
|
+
}
|
160
154
|
end
|
161
155
|
|
162
|
-
|
163
|
-
`rake listen_task`
|
164
|
-
```
|
165
|
-
|
166
|
-
or using passed in values:
|
167
|
-
```ruby
|
168
|
-
require 'fastly_nsq/rake_task'
|
169
|
-
|
170
|
-
MessageQueue::RakeTask.new(:listen_task, [:channel])
|
171
|
-
|
172
|
-
# usage:
|
173
|
-
`rake listen_task['my_channel']`
|
174
|
-
```
|
175
|
-
|
176
|
-
Both methods can be used at the same time with the passed in values taking
|
177
|
-
priority over block assigned values
|
156
|
+
The task can also define a `call`-able "preprocessor" (called before any `Processor.process`) and a custom `logger`.
|
178
157
|
|
179
158
|
See the [`Rakefile`](examples/Rakefile) file
|
180
159
|
for more detail.
|
@@ -195,34 +174,6 @@ as the real adapter.
|
|
195
174
|
|
196
175
|
## Configuration
|
197
176
|
|
198
|
-
### Processing Messages
|
199
|
-
|
200
|
-
This gem expects you to create a
|
201
|
-
new class called `MessageProcessor`
|
202
|
-
which will process messages
|
203
|
-
once they are consumed off of the queue topic.
|
204
|
-
|
205
|
-
This class needs to adhere to the following API:
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
class MessageProcessor
|
209
|
-
# This an instance of NSQ::Message or FakeMessageQueue::Message
|
210
|
-
def initialize(message_body)
|
211
|
-
@message_body = message_body
|
212
|
-
end
|
213
|
-
|
214
|
-
def start
|
215
|
-
# Do things
|
216
|
-
end
|
217
|
-
|
218
|
-
private
|
219
|
-
|
220
|
-
def message
|
221
|
-
JSON.parse(@message_body)
|
222
|
-
end
|
223
|
-
end
|
224
|
-
```
|
225
|
-
|
226
177
|
### Environment Variables
|
227
178
|
|
228
179
|
The URLs for the various
|
@@ -235,7 +186,6 @@ stock NSQ on OS X,
|
|
235
186
|
installed via Homebrew:
|
236
187
|
|
237
188
|
```shell
|
238
|
-
BROADCAST_ADDRESS='127.0.0.1'
|
239
189
|
NSQD_TCP_ADDRESS='127.0.0.1:4150'
|
240
190
|
NSQD_HTTP_ADDRESS='127.0.0.1:4151'
|
241
191
|
NSQLOOKUPD_TCP_ADDRESS='127.0.0.1:4160'
|
@@ -263,7 +213,7 @@ FAKE_QUEUE=true
|
|
263
213
|
```
|
264
214
|
|
265
215
|
Also be sure call
|
266
|
-
`
|
216
|
+
`FakeBackend.reset!`
|
267
217
|
before each test in your app to ensure
|
268
218
|
there are no leftover messages.
|
269
219
|
|
data/examples/.sample.env
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
# NSQ
|
2
2
|
# In the Fastly Dev VM
|
3
|
-
BROADCAST_ADDRESS='billing'
|
4
3
|
NSQD_TCP_ADDRESS='billing:1910'
|
5
4
|
NSQD_HTTP_ADDRESS='billing:1911'
|
6
5
|
NSQLOOKUPD_TCP_ADDRESS='billing:4160'
|
7
6
|
NSQLOOKUPD_HTTP_ADDRESS='billing:4161'
|
8
7
|
|
9
8
|
# On OSX with Homebrew defaults (brew install nsq)
|
10
|
-
BROADCAST_ADDRESS='127.0.0.1'
|
11
9
|
NSQD_TCP_ADDRESS='127.0.0.1:4150'
|
12
10
|
NSQD_HTTP_ADDRESS='127.0.0.1:4151'
|
13
11
|
NSQLOOKUPD_TCP_ADDRESS='127.0.0.1:4160'
|
data/examples/Rakefile
CHANGED
@@ -1,88 +1,41 @@
|
|
1
1
|
require 'fastly_nsq/rake_task'
|
2
2
|
|
3
|
-
|
4
|
-
# You are required to to set the topics in MessageProcessor.topics as an array:
|
5
|
-
|
6
|
-
class MessageProcessor
|
7
|
-
def self.topics
|
8
|
-
%w(customer_created customer_deleted)
|
9
|
-
end
|
10
|
-
|
11
|
-
# ...
|
12
|
-
end
|
13
|
-
|
14
|
-
# A channel will need to be passed in
|
15
|
-
# to the Rake task
|
3
|
+
# A list of topics and a channel will need to be passed in to the Rake task
|
16
4
|
# when it is called.
|
17
|
-
#
|
18
|
-
# The task looks like:
|
19
|
-
|
20
|
-
desc 'Listen to the messaging queue with the given channel.'
|
21
|
-
|
22
|
-
MessageQueue::RakeTask.new(:listen_task, [:channel])
|
23
|
-
|
24
|
-
# Call `rake listen_task[my_channel]`.
|
25
|
-
# The topics will be ['customer_created', 'customer_deleted']
|
26
|
-
# and channel will be 'my_channel'.
|
27
|
-
|
28
|
-
#-------------------------------------------------------------------------------
|
29
|
-
# In Rails, you can include the application environment. The task looks like:
|
30
|
-
|
31
|
-
desc 'Listen to the messaging queue with the given channel.'
|
32
|
-
|
33
|
-
MessageQueue::RakeTask.new(:listen_task, [:channel] => :environment)
|
34
|
-
|
35
|
-
# Call `rake listen_task[your_channel]`.
|
36
|
-
# The topics will be ['customer_created', 'customer_deleted']
|
37
|
-
# and channel will be 'your_channel'.
|
38
|
-
|
39
|
-
#-------------------------------------------------------------------------------
|
40
|
-
# The channel can also be preset in the task
|
41
|
-
# by passing a block and assigning the value. The task looks like:
|
42
5
|
|
43
6
|
desc 'Listen to the messaging queue with the given channel.'
|
44
7
|
|
45
|
-
|
8
|
+
FastlyNsq::RakeTask.new(:listen_task) do |task|
|
46
9
|
task.channel = 'some_channel'
|
10
|
+
task.topics = { 'a_topic' => RespondsToProcess }
|
47
11
|
end
|
48
12
|
|
49
|
-
#
|
50
|
-
# The topics will be ['customer_created', 'customer_deleted']
|
51
|
-
# and channel will be 'some_channel'.
|
52
|
-
|
53
|
-
#-------------------------------------------------------------------------------
|
54
|
-
# Do the same thing and include the environment:
|
13
|
+
# A custom logger and preprocessor
|
55
14
|
|
56
15
|
desc 'Listen to the messaging queue with the given channel.'
|
57
16
|
|
58
|
-
|
59
|
-
task.channel
|
17
|
+
FastlyNsq::RakeTask.new(:listen_task) do |task|
|
18
|
+
task.channel = 'some_channel'
|
19
|
+
task.topics = { 'a_topic' => RespondsToProcess }
|
20
|
+
task.preprocessor = ->(message) { StatsClient.log(message) }
|
21
|
+
task.logger = Logger.new(STDERR)
|
60
22
|
end
|
61
23
|
|
62
|
-
|
63
|
-
#
|
64
|
-
|
24
|
+
#-------------------------------------------------------------------------------
|
25
|
+
# In Rails, you can include the application environment. The task looks like:
|
26
|
+
|
27
|
+
FastlyNsq::RakeTask.new(:listen_task => :environment)
|
65
28
|
|
66
29
|
#-------------------------------------------------------------------------------
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# with the ability to override at time of Rake call:
|
30
|
+
# It's also possible to define the rake task to allow for overriding channel
|
31
|
+
# and topics when calling the rake task:
|
70
32
|
|
71
|
-
|
72
|
-
task.channel = 'default_channel'
|
73
|
-
end
|
33
|
+
FastlyNsq::RakeTask.new(:listen_task, [:channel, :topics])
|
74
34
|
|
75
|
-
#
|
76
|
-
#
|
77
|
-
# and channel will be 'overridden_channel'.
|
35
|
+
# Then call the task:
|
36
|
+
# rake listen_task[my_channel, {topic: Processor}]
|
78
37
|
|
79
38
|
#-------------------------------------------------------------------------------
|
80
|
-
#
|
81
|
-
|
82
|
-
MessageQueue::RakeTask.new(:listen_task, [:channel] => :environment) do |task|
|
83
|
-
task.channel = 'default_channel'
|
84
|
-
end
|
39
|
+
# Do the same thing and include the environment:
|
85
40
|
|
86
|
-
|
87
|
-
# The topics will be ['customer_created', 'customer_deleted']
|
88
|
-
# and channel will be 'overridden_channel'.
|
41
|
+
FastlyNsq::RakeTask.new(:listen_task, [:channel, :topics] => :environment)
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
module MessageQueue
|
3
|
+
module FastlyNsq
|
6
4
|
class Consumer
|
7
5
|
extend Forwardable
|
8
6
|
def_delegator :connection, :pop
|
@@ -14,17 +12,19 @@ module MessageQueue
|
|
14
12
|
@topic = topic
|
15
13
|
@channel = channel
|
16
14
|
@ssl_context = SSLContext.new(ssl_context)
|
17
|
-
@connector = connector
|
15
|
+
@connector = connector
|
18
16
|
end
|
19
17
|
|
20
18
|
private
|
21
19
|
|
22
|
-
attr_reader :channel, :
|
23
|
-
|
24
|
-
DEFAULT_CONNECTOR = ->(params) { MessageQueue.strategy::Consumer.new(params) }
|
20
|
+
attr_reader :channel, :topic, :ssl_context
|
25
21
|
|
26
22
|
def connection
|
27
|
-
@connection ||= connector.
|
23
|
+
@connection ||= connector.new(params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def connector
|
27
|
+
@connector || FastlyNsq.strategy::Consumer
|
28
28
|
end
|
29
29
|
|
30
30
|
def params
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module FastlyNsq
|
2
|
+
module FakeBackend
|
3
|
+
@@logger = Logger.new(nil)
|
4
|
+
@@delay = 0.5
|
5
|
+
@@queue = []
|
6
|
+
|
7
|
+
def self.queue
|
8
|
+
@@queue
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.queue=(message)
|
12
|
+
@@queue = message
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.reset!
|
16
|
+
self.queue = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.logger=(logger)
|
20
|
+
@@logger = logger
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.logger
|
24
|
+
@@logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.delay
|
28
|
+
@@delay
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.delay=(delay)
|
32
|
+
@@delay = delay
|
33
|
+
end
|
34
|
+
|
35
|
+
class Producer
|
36
|
+
def initialize(topic:, nsqd: nil, ssl_context: nil)
|
37
|
+
end
|
38
|
+
|
39
|
+
def write(string)
|
40
|
+
message = Message.new(string)
|
41
|
+
queue.push(message)
|
42
|
+
end
|
43
|
+
|
44
|
+
def terminate
|
45
|
+
# noop
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def queue
|
51
|
+
FakeBackend.queue
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Consumer
|
56
|
+
def initialize(nsqlookupd: nil, topic:, channel:, ssl_context: nil)
|
57
|
+
end
|
58
|
+
|
59
|
+
def pop(delay = FakeBackend.delay)
|
60
|
+
message = nil
|
61
|
+
|
62
|
+
until message
|
63
|
+
message = queue.pop
|
64
|
+
sleep delay
|
65
|
+
end
|
66
|
+
|
67
|
+
message
|
68
|
+
end
|
69
|
+
|
70
|
+
def pop_without_blocking
|
71
|
+
queue.pop
|
72
|
+
end
|
73
|
+
|
74
|
+
def size
|
75
|
+
queue.size
|
76
|
+
end
|
77
|
+
|
78
|
+
def terminate
|
79
|
+
# noop
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def queue
|
85
|
+
FakeBackend.queue
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Message
|
90
|
+
attr_reader :body
|
91
|
+
|
92
|
+
def initialize(body)
|
93
|
+
@body = body
|
94
|
+
end
|
95
|
+
|
96
|
+
def finish
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'fastly_nsq/message'
|
2
|
+
|
3
|
+
module FastlyNsq
|
4
|
+
class Listener
|
5
|
+
def self.listen_to(**args)
|
6
|
+
new(**args).go
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(topic:, processor:, channel: nil, consumer: nil, **options)
|
10
|
+
@topic = topic
|
11
|
+
@processor = processor
|
12
|
+
@consumer = consumer || FastlyNsq::Consumer.new(topic: topic, channel: channel)
|
13
|
+
@logger = options.fetch :logger, FastlyNsq.logger
|
14
|
+
@preprocessor = options[:preprocessor]
|
15
|
+
end
|
16
|
+
|
17
|
+
def go(run_once: false)
|
18
|
+
exit_on 'INT'
|
19
|
+
exit_on 'TERM'
|
20
|
+
|
21
|
+
loop do
|
22
|
+
next_message do |message|
|
23
|
+
log message
|
24
|
+
preprocess message
|
25
|
+
processor.process message
|
26
|
+
end
|
27
|
+
|
28
|
+
break if run_once
|
29
|
+
end
|
30
|
+
|
31
|
+
consumer.terminate
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :topic, :consumer, :preprocessor, :processor, :logger
|
37
|
+
|
38
|
+
def log(message)
|
39
|
+
logger.info "[NSQ] Message Received: #{message}" if logger
|
40
|
+
end
|
41
|
+
|
42
|
+
def next_message
|
43
|
+
message = consumer.pop # TODO: consumer.pop do |message|
|
44
|
+
result = yield FastlyNsq::Message.new(message.body)
|
45
|
+
message.finish if result
|
46
|
+
end
|
47
|
+
|
48
|
+
def preprocess(message)
|
49
|
+
preprocessor.call(message) if preprocessor
|
50
|
+
end
|
51
|
+
|
52
|
+
def exit_on(signal)
|
53
|
+
Signal.trap(signal) do
|
54
|
+
consumer.terminate
|
55
|
+
exit
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class FastlyNsq::Message
|
4
|
+
attr_reader :raw_body
|
5
|
+
alias to_s raw_body
|
6
|
+
|
7
|
+
def initialize(raw_body)
|
8
|
+
@raw_body = raw_body
|
9
|
+
end
|
10
|
+
|
11
|
+
def data
|
12
|
+
@data ||= body['data']
|
13
|
+
end
|
14
|
+
|
15
|
+
def body
|
16
|
+
@body ||= JSON.parse(raw_body)
|
17
|
+
end
|
18
|
+
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
module MessageQueue
|
3
|
+
module FastlyNsq
|
6
4
|
class Producer
|
7
5
|
extend Forwardable
|
8
6
|
def_delegator :connection, :terminate
|
@@ -11,17 +9,19 @@ module MessageQueue
|
|
11
9
|
def initialize(topic:, ssl_context: nil, connector: nil)
|
12
10
|
@topic = topic
|
13
11
|
@ssl_context = SSLContext.new(ssl_context)
|
14
|
-
@connector = connector
|
12
|
+
@connector = connector
|
15
13
|
end
|
16
14
|
|
17
15
|
private
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
attr_reader :connector, :topic, :ssl_context
|
17
|
+
attr_reader :topic, :ssl_context
|
22
18
|
|
23
19
|
def connection
|
24
|
-
@connection ||= connector.
|
20
|
+
@connection ||= connector.new(params)
|
21
|
+
end
|
22
|
+
|
23
|
+
def connector
|
24
|
+
@connector || FastlyNsq.strategy::Producer
|
25
25
|
end
|
26
26
|
|
27
27
|
def params
|