fastly_nsq 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -1,10 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'fastly_nsq/rake_task'
|
3
3
|
|
4
|
-
RSpec.describe
|
4
|
+
RSpec.describe FastlyNsq::RakeTask do
|
5
5
|
before(:each) do
|
6
6
|
Rake::Task.clear
|
7
|
-
|
7
|
+
@original_logger = FastlyNsq.logger
|
8
|
+
FastlyNsq.logger = Logger.new(nil)
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
FastlyNsq.logger = @original_logger
|
8
13
|
end
|
9
14
|
|
10
15
|
describe 'when defining tasks' do
|
@@ -12,7 +17,7 @@ RSpec.describe MessageQueue::RakeTask do
|
|
12
17
|
it 'creates a task with the default name' do
|
13
18
|
default_task_name = 'begin_listening'
|
14
19
|
|
15
|
-
|
20
|
+
FastlyNsq::RakeTask.new
|
16
21
|
defined_tasks = Rake::Task.tasks
|
17
22
|
first_task_name = defined_tasks.first.name
|
18
23
|
|
@@ -24,7 +29,7 @@ RSpec.describe MessageQueue::RakeTask do
|
|
24
29
|
it 'creates a task with the provided name' do
|
25
30
|
task_name = 'test_name'
|
26
31
|
|
27
|
-
|
32
|
+
FastlyNsq::RakeTask.new(task_name.to_sym)
|
28
33
|
defined_tasks = Rake::Task.tasks
|
29
34
|
first_task_name = defined_tasks.first.name
|
30
35
|
|
@@ -34,95 +39,103 @@ RSpec.describe MessageQueue::RakeTask do
|
|
34
39
|
end
|
35
40
|
|
36
41
|
describe 'when running tasks' do
|
37
|
-
context 'when
|
38
|
-
it '
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
allow(MessageQueue::Listener).to receive(:new).
|
44
|
-
and_return(message_queue_listener)
|
45
|
-
|
46
|
-
MessageQueue::RakeTask.new(:begin_listening, [:channel])
|
47
|
-
Rake::Task['begin_listening'].execute(channel: channel)
|
48
|
-
|
49
|
-
topics.each do |topic|
|
50
|
-
expect(MessageQueue::Listener).to have_received(:new).
|
51
|
-
with(topic: topic, channel: channel)
|
52
|
-
end
|
42
|
+
context 'when no channel is provided' do
|
43
|
+
it 'raises an error' do
|
44
|
+
expect do
|
45
|
+
FastlyNsq::RakeTask.new(:begin_listening, [:channel])
|
46
|
+
Rake::Task['begin_listening'].execute
|
47
|
+
end.to raise_error(ArgumentError, /required.+channel/)
|
53
48
|
end
|
54
49
|
end
|
55
50
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
|
60
|
-
|
61
|
-
message_queue_listener = double('listener', go: nil)
|
62
|
-
expect(MessageQueue::Listener).to receive(:new).
|
63
|
-
with(topic: topics.first, channel: channel).
|
64
|
-
and_return(message_queue_listener)
|
51
|
+
context 'when no topics are provided' do
|
52
|
+
it 'raises an error' do
|
53
|
+
channel = 'best_server_number_1'
|
65
54
|
|
66
|
-
|
67
|
-
|
55
|
+
expect do
|
56
|
+
FastlyNsq::RakeTask.new(:begin_listening, [:channel])
|
57
|
+
Rake::Task['begin_listening'].execute(channel: channel)
|
58
|
+
end.to raise_error(ArgumentError, /required.+topics/)
|
59
|
+
end
|
68
60
|
end
|
69
61
|
|
70
|
-
|
71
|
-
channel
|
72
|
-
topics
|
73
|
-
|
62
|
+
context 'when a channel and topics are defined' do
|
63
|
+
let(:channel) { 'clown_generating_service' }
|
64
|
+
let(:topics) { { customer_created: :fake_processor } }
|
65
|
+
let(:listener) { class_double FastlyNsq::Listener, listen_to: nil }
|
66
|
+
|
67
|
+
it 'configures via a block if one is given' do
|
68
|
+
FastlyNsq::RakeTask.new(:begin_listening, [:channel, :topics]) do |task|
|
69
|
+
task.channel = channel
|
70
|
+
task.topics = topics
|
71
|
+
task.listener = listener
|
72
|
+
end
|
74
73
|
|
75
|
-
|
76
|
-
expect(MessageQueue::Listener).to receive(:new).
|
77
|
-
with(topic: topics.first, channel: channel).
|
78
|
-
and_return(message_queue_listener)
|
74
|
+
Rake::Task['begin_listening'].execute
|
79
75
|
|
80
|
-
|
81
|
-
|
76
|
+
expect(listener).to have_received(:listen_to).
|
77
|
+
with(hash_including(topic: :customer_created, channel: channel))
|
82
78
|
end
|
83
|
-
Rake::Task['begin_listening'].execute(channel: channel)
|
84
|
-
end
|
85
79
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
80
|
+
it 'prefers inline channel definition over block assignments' do
|
81
|
+
new_channel = 'send_balloons_to_customer_service'
|
82
|
+
|
83
|
+
FastlyNsq::RakeTask.new(:begin_listening, [:channel, :topics]) do |task|
|
84
|
+
task.channel = channel
|
85
|
+
task.topics = topics
|
86
|
+
task.listener = listener
|
87
|
+
end
|
91
88
|
|
92
|
-
|
93
|
-
expect(MessageQueue::Listener).to receive(:new).
|
94
|
-
with(topic: topics.first, channel: new_channel).
|
95
|
-
and_return(message_queue_listener)
|
89
|
+
Rake::Task['begin_listening'].execute(channel: new_channel, topics: topics, listener: listener)
|
96
90
|
|
97
|
-
|
98
|
-
|
91
|
+
expect(listener).to have_received(:listen_to).
|
92
|
+
with(hash_including(topic: :customer_created, channel: channel))
|
99
93
|
end
|
100
|
-
Rake::Task['begin_listening'].execute(channel: new_channel)
|
101
|
-
end
|
102
94
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
95
|
+
it 'configures a listener for each topic if there are multiple' do
|
96
|
+
topics = %w(foo bar baz quuz etc)
|
97
|
+
|
98
|
+
FastlyNsq::RakeTask.new(:begin_listening, [:channel, :topics, :listener])
|
99
|
+
Rake::Task['begin_listening'].execute(channel: channel, topics: topics, listener: listener)
|
100
|
+
|
101
|
+
topics.each do |(topic, processor)|
|
102
|
+
expect(listener).to have_received(:listen_to).
|
103
|
+
with(hash_including(topic: topic, channel: channel, processor: processor))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'and preprocessor is defined' do
|
108
|
+
it 'passes preprocessor to the listener' do
|
109
|
+
FastlyNsq::RakeTask.new(:begin_listening) do |task|
|
110
|
+
task.channel = channel
|
111
|
+
task.topics = topics
|
112
|
+
task.listener = listener
|
113
|
+
task.preprocessor = :noop
|
114
|
+
end
|
107
115
|
|
108
|
-
expect do
|
109
|
-
MessageQueue::RakeTask.new(:begin_listening, [:channel])
|
110
116
|
Rake::Task['begin_listening'].execute
|
111
|
-
|
117
|
+
|
118
|
+
expect(listener).to have_received(:listen_to).
|
119
|
+
with(hash_including(preprocessor: :noop))
|
120
|
+
end
|
112
121
|
end
|
113
|
-
end
|
114
122
|
|
115
|
-
|
116
|
-
|
117
|
-
channel = 'best_server_number_1'
|
118
|
-
error_message = /MessageProcessor.topics is not defined/
|
119
|
-
allow(SampleMessageProcessor).to receive(:topics).
|
120
|
-
and_raise(NoMethodError, "undefined method `topics'")
|
123
|
+
context 'and logger is defined' do
|
124
|
+
let(:logger) { double 'Logger', info: nil }
|
121
125
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
+
it 'passes logger to the listener' do
|
127
|
+
FastlyNsq::RakeTask.new(:begin_listening) do |task|
|
128
|
+
task.channel = channel
|
129
|
+
task.topics = topics
|
130
|
+
task.listener = listener
|
131
|
+
task.logger = logger
|
132
|
+
end
|
133
|
+
|
134
|
+
Rake::Task['begin_listening'].execute
|
135
|
+
|
136
|
+
expect(listener).to have_received(:listen_to).
|
137
|
+
with(hash_including(logger: logger))
|
138
|
+
end
|
126
139
|
end
|
127
140
|
end
|
128
141
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe SSLContext do
|
3
|
+
RSpec.describe FastlyNsq::SSLContext do
|
4
4
|
describe 'when SSL ENV variables are not set' do
|
5
5
|
describe '.to_h' do
|
6
6
|
it 'returns nil when initialized without parameters' do
|
7
|
-
context = SSLContext.new
|
7
|
+
context = FastlyNsq::SSLContext.new
|
8
8
|
|
9
9
|
expect(context.to_h).to be_nil
|
10
10
|
end
|
@@ -15,7 +15,7 @@ RSpec.describe SSLContext do
|
|
15
15
|
certificate: 'certificate',
|
16
16
|
ca_certificate: 'ca_certificate',
|
17
17
|
}
|
18
|
-
context = SSLContext.new(ssl_context_hash)
|
18
|
+
context = FastlyNsq::SSLContext.new(ssl_context_hash)
|
19
19
|
|
20
20
|
expect(context.to_h).to eq(ssl_context_hash)
|
21
21
|
end
|
@@ -25,7 +25,7 @@ RSpec.describe SSLContext do
|
|
25
25
|
key: 'key',
|
26
26
|
certificate: 'certificate',
|
27
27
|
}
|
28
|
-
context = SSLContext.new(ssl_context_hash)
|
28
|
+
context = FastlyNsq::SSLContext.new(ssl_context_hash)
|
29
29
|
ca_certificate = context.to_h[:ca_certificate]
|
30
30
|
|
31
31
|
expect(ca_certificate).to be_nil
|
@@ -53,7 +53,7 @@ RSpec.describe SSLContext do
|
|
53
53
|
certificate: ENV['NSQ_SSL_CERTIFICATE'],
|
54
54
|
ca_certificate: ENV['NSQ_SSL_CA_CERTIFICATE'],
|
55
55
|
}
|
56
|
-
context = SSLContext.new
|
56
|
+
context = FastlyNsq::SSLContext.new
|
57
57
|
|
58
58
|
expect(context.to_h).to eq(expected_hash)
|
59
59
|
end
|
@@ -65,7 +65,7 @@ RSpec.describe SSLContext do
|
|
65
65
|
certificate: passed_certificate,
|
66
66
|
ca_certificate: ENV['NSQ_SSL_CA_CERTIFICATE'],
|
67
67
|
}
|
68
|
-
context = SSLContext.new(certificate: passed_certificate)
|
68
|
+
context = FastlyNsq::SSLContext.new(certificate: passed_certificate)
|
69
69
|
|
70
70
|
expect(context.to_h).to eq(expected_hash)
|
71
71
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe
|
3
|
+
RSpec.describe FastlyNsq::Strategy do
|
4
4
|
describe 'when FAKE_QUEUE is falsy' do
|
5
5
|
it 'returns the strategy based on the ENV variable' do
|
6
6
|
[false, 0, '0', 'false', 'FALSE', 'off', 'OFF', nil].each do |no|
|
7
7
|
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(no)
|
8
8
|
|
9
|
-
strategy =
|
9
|
+
strategy = FastlyNsq::Strategy.for_queue
|
10
10
|
|
11
11
|
expect(strategy).to eq Nsq
|
12
12
|
end
|
@@ -18,9 +18,9 @@ RSpec.describe MessageQueue::Strategy do
|
|
18
18
|
[true, 1, '1', 'true', 'TRUE', 'on', 'ON'].each do |yes|
|
19
19
|
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(yes)
|
20
20
|
|
21
|
-
strategy =
|
21
|
+
strategy = FastlyNsq::Strategy.for_queue
|
22
22
|
|
23
|
-
expect(strategy).to eq
|
23
|
+
expect(strategy).to eq FastlyNsq::FakeBackend
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -29,7 +29,8 @@ RSpec.describe MessageQueue::Strategy do
|
|
29
29
|
it 'raises with a helpful error' do
|
30
30
|
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return('taco')
|
31
31
|
|
32
|
-
expect {
|
32
|
+
expect { FastlyNsq::Strategy.for_queue }.to \
|
33
|
+
raise_error(FastlyNsq::Strategy::InvalidParameterError)
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe FastlyNsq do
|
4
|
+
module TestStrategy; end
|
5
|
+
|
6
|
+
it 'allows the logger to be set and retrieved' do
|
7
|
+
logger = Logger.new(STDOUT)
|
8
|
+
FastlyNsq.logger = logger
|
9
|
+
|
10
|
+
expect(FastlyNsq.logger).to eq logger
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns the current Strategy' do
|
14
|
+
allow(FastlyNsq::Strategy).to receive(:for_queue).and_return(TestStrategy)
|
15
|
+
|
16
|
+
expect(FastlyNsq.strategy).to eql(TestStrategy)
|
17
|
+
end
|
18
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,6 @@ require 'fastly_nsq'
|
|
2
2
|
require 'awesome_print'
|
3
3
|
require 'pry-byebug'
|
4
4
|
|
5
|
-
require_relative '../lib/fastly_nsq/sample_message_processor'
|
6
5
|
require_relative 'support/env_helpers'
|
7
6
|
|
8
7
|
RSpec.configure do |config|
|
@@ -27,13 +26,9 @@ RSpec.configure do |config|
|
|
27
26
|
config.default_formatter = 'doc'
|
28
27
|
end
|
29
28
|
|
30
|
-
config.before(:suite) do
|
31
|
-
MessageProcessor = SampleMessageProcessor
|
32
|
-
end
|
33
|
-
|
34
29
|
config.before(:each) do
|
35
30
|
load_sample_environment_variables
|
36
|
-
|
31
|
+
FastlyNsq::FakeBackend.reset!
|
37
32
|
end
|
38
33
|
|
39
34
|
config.around(:each, fake_queue: true) do |example|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastly_nsq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommy O'Neil
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-08-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: awesome_print
|
@@ -195,26 +195,25 @@ files:
|
|
195
195
|
- examples/Rakefile
|
196
196
|
- fastly_nsq.gemspec
|
197
197
|
- lib/fastly_nsq.rb
|
198
|
-
- lib/fastly_nsq/
|
199
|
-
- lib/fastly_nsq/
|
200
|
-
- lib/fastly_nsq/
|
201
|
-
- lib/fastly_nsq/
|
202
|
-
- lib/fastly_nsq/
|
203
|
-
- lib/fastly_nsq/message_queue/strategy.rb
|
198
|
+
- lib/fastly_nsq/consumer.rb
|
199
|
+
- lib/fastly_nsq/fake_backend.rb
|
200
|
+
- lib/fastly_nsq/listener.rb
|
201
|
+
- lib/fastly_nsq/message.rb
|
202
|
+
- lib/fastly_nsq/producer.rb
|
204
203
|
- lib/fastly_nsq/rake_task.rb
|
205
|
-
- lib/fastly_nsq/sample_message_processor.rb
|
206
204
|
- lib/fastly_nsq/ssl_context.rb
|
205
|
+
- lib/fastly_nsq/strategy.rb
|
207
206
|
- lib/fastly_nsq/version.rb
|
208
|
-
- spec/lib/fastly_nsq/
|
207
|
+
- spec/lib/fastly_nsq/consumer_spec.rb
|
208
|
+
- spec/lib/fastly_nsq/fake_backend_spec.rb
|
209
209
|
- spec/lib/fastly_nsq/fastly_nsq_spec.rb
|
210
|
-
- spec/lib/fastly_nsq/
|
211
|
-
- spec/lib/fastly_nsq/
|
212
|
-
- spec/lib/fastly_nsq/
|
213
|
-
- spec/lib/fastly_nsq/message_queue/strategy_spec.rb
|
214
|
-
- spec/lib/fastly_nsq/message_queue_spec.rb
|
210
|
+
- spec/lib/fastly_nsq/listener_spec.rb
|
211
|
+
- spec/lib/fastly_nsq/message_spec.rb
|
212
|
+
- spec/lib/fastly_nsq/producer_spec.rb
|
215
213
|
- spec/lib/fastly_nsq/rake_task_spec.rb
|
216
|
-
- spec/lib/fastly_nsq/sample_message_processor_spec.rb
|
217
214
|
- spec/lib/fastly_nsq/ssl_context_spec.rb
|
215
|
+
- spec/lib/fastly_nsq/strategy_spec.rb
|
216
|
+
- spec/lib/fastly_nsq_spec.rb
|
218
217
|
- spec/spec_helper.rb
|
219
218
|
- spec/support/env_helpers.rb
|
220
219
|
homepage: https://github.com/fastly/fastly-nsq
|
@@ -1,98 +0,0 @@
|
|
1
|
-
module FakeMessageQueue
|
2
|
-
@@logger = Logger.new(nil)
|
3
|
-
@@delay = 0.5
|
4
|
-
@@queue = []
|
5
|
-
|
6
|
-
def self.queue
|
7
|
-
@@queue
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.queue=(message)
|
11
|
-
@@queue = message
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.reset!
|
15
|
-
self.queue = []
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.logger=(logger)
|
19
|
-
@@logger = logger
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.logger
|
23
|
-
@@logger
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.delay
|
27
|
-
@@delay
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.delay=(delay)
|
31
|
-
@@delay = delay
|
32
|
-
end
|
33
|
-
|
34
|
-
class Producer
|
35
|
-
def initialize(topic:, nsqd: nil, ssl_context: nil)
|
36
|
-
end
|
37
|
-
|
38
|
-
def write(string)
|
39
|
-
message = Message.new(string)
|
40
|
-
queue.push(message)
|
41
|
-
end
|
42
|
-
|
43
|
-
def terminate
|
44
|
-
# noop
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def queue
|
50
|
-
FakeMessageQueue.queue
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
class Consumer
|
55
|
-
def initialize(nsqlookupd: nil, topic:, channel:, ssl_context: nil)
|
56
|
-
end
|
57
|
-
|
58
|
-
def pop(delay = FakeMessageQueue.delay)
|
59
|
-
message = nil
|
60
|
-
|
61
|
-
until message
|
62
|
-
message = queue.pop
|
63
|
-
sleep delay
|
64
|
-
end
|
65
|
-
|
66
|
-
message
|
67
|
-
end
|
68
|
-
|
69
|
-
def pop_without_blocking
|
70
|
-
queue.pop
|
71
|
-
end
|
72
|
-
|
73
|
-
def size
|
74
|
-
queue.size
|
75
|
-
end
|
76
|
-
|
77
|
-
def terminate
|
78
|
-
# noop
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def queue
|
84
|
-
FakeMessageQueue.queue
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
class Message
|
89
|
-
attr_reader :body
|
90
|
-
|
91
|
-
def initialize(body)
|
92
|
-
@body = body
|
93
|
-
end
|
94
|
-
|
95
|
-
def finish
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module MessageQueue
|
2
|
-
class Listener
|
3
|
-
def initialize(topic:, channel:, processor: nil, consumer: nil)
|
4
|
-
@topic = topic
|
5
|
-
@channel = channel
|
6
|
-
@processor = processor || DEFAULT_PROCESSOR
|
7
|
-
@consumer = consumer || MessageQueue::Consumer.new(consumer_params)
|
8
|
-
end
|
9
|
-
|
10
|
-
def go
|
11
|
-
Signal.trap('INT') do
|
12
|
-
shutdown
|
13
|
-
end
|
14
|
-
|
15
|
-
Signal.trap('TERM') do
|
16
|
-
shutdown
|
17
|
-
end
|
18
|
-
|
19
|
-
loop do
|
20
|
-
process_one_message
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def process_next_message
|
25
|
-
process_one_message
|
26
|
-
consumer.terminate
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
attr_reader :channel, :topic, :processor, :consumer
|
32
|
-
DEFAULT_PROCESSOR = ->(body, topic) { MessageProcessor.new(message_body: body, topic: topic).go }
|
33
|
-
|
34
|
-
def process_one_message
|
35
|
-
message = consumer.pop
|
36
|
-
processor.call(message.body, topic)
|
37
|
-
message.finish
|
38
|
-
end
|
39
|
-
|
40
|
-
def consumer_params
|
41
|
-
{ topic: topic, channel: channel }
|
42
|
-
end
|
43
|
-
|
44
|
-
def shutdown
|
45
|
-
consumer.terminate
|
46
|
-
exit
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module MessageQueue::Strategy
|
2
|
-
module_function
|
3
|
-
|
4
|
-
def for_queue
|
5
|
-
real_queue || fake_queue || error
|
6
|
-
end
|
7
|
-
|
8
|
-
private_class_method
|
9
|
-
|
10
|
-
ERR_MESSAGE = "You must set ENV['FAKE_QUEUE'] to either true or false".freeze
|
11
|
-
|
12
|
-
def error
|
13
|
-
raise InvalidParameterError, ERR_MESSAGE
|
14
|
-
end
|
15
|
-
|
16
|
-
FALSY_VALUES = [false, 0, '0', 'false', 'FALSE', 'off', 'OFF', nil].freeze
|
17
|
-
TRUTHY_VALUES = [true, 1, '1', 'true', 'TRUE', 'on', 'ON'].freeze
|
18
|
-
|
19
|
-
def fake_queue
|
20
|
-
FakeMessageQueue if should_use_fake_queue?
|
21
|
-
end
|
22
|
-
|
23
|
-
def should_use_real_queue?
|
24
|
-
FALSY_VALUES.include? ENV['FAKE_QUEUE']
|
25
|
-
end
|
26
|
-
|
27
|
-
def real_queue
|
28
|
-
Nsq if should_use_real_queue?
|
29
|
-
end
|
30
|
-
|
31
|
-
def should_use_fake_queue?
|
32
|
-
TRUTHY_VALUES.include? ENV['FAKE_QUEUE']
|
33
|
-
end
|
34
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'nsq'
|
2
|
-
require_relative 'fake_message_queue'
|
3
|
-
require_relative 'message_queue/listener'
|
4
|
-
require_relative 'message_queue/producer'
|
5
|
-
require_relative 'message_queue/consumer'
|
6
|
-
require_relative 'message_queue/strategy'
|
7
|
-
require_relative 'ssl_context'
|
8
|
-
|
9
|
-
module MessageQueue
|
10
|
-
def self.logger=(logger)
|
11
|
-
strategy.logger = logger
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.logger
|
15
|
-
strategy.logger
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.strategy
|
19
|
-
Strategy.for_queue
|
20
|
-
end
|
21
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
class HeartbeatWorker
|
2
|
-
def self.perform_async(_data)
|
3
|
-
# noop
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class UnknownMessageWorker
|
8
|
-
def self.perform_async(_data)
|
9
|
-
# noop
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class SampleMessageProcessor
|
14
|
-
TOPIC_TO_WORKER_MAP = {
|
15
|
-
'heartbeat' => HeartbeatWorker,
|
16
|
-
}.freeze
|
17
|
-
|
18
|
-
def self.topics
|
19
|
-
TOPIC_TO_WORKER_MAP.keys
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize(message_body:, topic:)
|
23
|
-
@message_body = message_body
|
24
|
-
@topic = topic
|
25
|
-
end
|
26
|
-
|
27
|
-
def go
|
28
|
-
process_message_body
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
attr_reader :message_body, :topic
|
34
|
-
|
35
|
-
def process_message_body
|
36
|
-
message_processor.perform_async(message_data)
|
37
|
-
end
|
38
|
-
|
39
|
-
def message_processor
|
40
|
-
TOPIC_TO_WORKER_MAP.fetch(topic, UnknownMessageWorker)
|
41
|
-
end
|
42
|
-
|
43
|
-
def message_data
|
44
|
-
parsed_message_body['data']
|
45
|
-
end
|
46
|
-
|
47
|
-
def parsed_message_body
|
48
|
-
JSON.parse(message_body)
|
49
|
-
end
|
50
|
-
end
|