pwwka 0.21.2 → 0.21.3
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/README.md +21 -11
- data/lib/pwwka.rb +4 -1
- data/lib/pwwka/channel_connector.rb +30 -10
- data/lib/pwwka/configuration.rb +17 -2
- data/lib/pwwka/send_message_async_sidekiq_job.rb +29 -0
- data/lib/pwwka/transmitter.rb +19 -9
- data/lib/pwwka/version.rb +1 -1
- data/pwwka.gemspec +3 -1
- data/spec/spec_helper.rb +2 -0
- data/spec/unit/configuration_spec.rb +28 -0
- data/spec/unit/send_message_async_sidekiq_job_spec.rb +43 -0
- data/spec/unit/transmitter_spec.rb +158 -33
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a449a47ab0a38c86349112177ed30351d69b151fa47289545eab2f976969342
|
4
|
+
data.tar.gz: 7f1a1b34abaad80936b8ec1018233310c6e6746b5262731e291002937c0b47b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38d0afcaf9e40c7a97a466f8188854b25f3426ba63ae64d622ba18b4035427ad02a875abf53b64ef4df0ee10f35b7c1b73ccd44263a2bdb8ce0b8f2291acf766
|
7
|
+
data.tar.gz: eead705f93110e06656a747f6c894aac8236fcff670286e5685bbc6853ae49896d1f39db9548d46ee4f8f4c70c1f47f315734f6e4f3057dde896b1f2d8ebdd41
|
data/README.md
CHANGED
@@ -129,7 +129,13 @@ Pwwka::Transmitter.send_message!(
|
|
129
129
|
|
130
130
|
* `:raise` - Log the error and raise the exception received from Bunny. (default strategy)
|
131
131
|
* `:ignore` - Log the error and return false.
|
132
|
-
* `:
|
132
|
+
* `:retry_async` - Log the error and return false. Also, enqueue a job with the configured background job processor (`:resque` or `:sidekiq`). **Note, this doesn't guarantee the message will actually be sent— it just guarantees an attempt is made to queue a background job [which could fail]**. The background job processor will default to Resque, but can be configured to Sidekiq:
|
133
|
+
|
134
|
+
```
|
135
|
+
Pwwka.configure do |config|
|
136
|
+
config.background_job_processor = :sidekiq
|
137
|
+
end
|
138
|
+
```
|
133
139
|
|
134
140
|
Example usage:
|
135
141
|
|
@@ -157,28 +163,32 @@ Pwwka::Transmitter.send_message!(payload, routing_key, delayed: true, delay_by:
|
|
157
163
|
These extra arguments work for all message sending methods - the safe ones, the handling, and the `message_queuer` methods (see below).
|
158
164
|
|
159
165
|
|
160
|
-
#### Sending message
|
161
|
-
|
162
|
-
To enqueue a message in a background Resque job, use `Transmitter.send_message_async`
|
166
|
+
#### Sending message asynchronously
|
167
|
+
To enqueue a message in a background job, use `Pwwka::Transmitter.send_message_async`:
|
163
168
|
```ruby
|
164
169
|
Pwwka::Transmitter.send_message_async(payload, routing_key, delay_by_ms: 5000) # default delay is 0
|
165
170
|
```
|
166
171
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
172
|
+
The job will be enqueued using the configured background job processor. This will default to Resque, but can be configured to use Sidekiq:
|
173
|
+
```
|
174
|
+
Pwwka.configure do |config|
|
175
|
+
config.background_job_processor = :sidekiq
|
176
|
+
end
|
177
|
+
```
|
173
178
|
|
174
|
-
|
179
|
+
Regardless of which processor you use, the name of the queue created is `pwwka_send_message_async`.
|
175
180
|
|
181
|
+
You can also configure Pwwka to use your own custom job using the `async_job_klass` configuration option. An example might be:
|
176
182
|
```
|
177
183
|
Pwwka.configure do |config|
|
178
184
|
config.async_job_klass = YourApp::PwwkaAsyncJob
|
179
185
|
end
|
180
186
|
```
|
181
187
|
|
188
|
+
If you are using Resque and `Resque::Plugins::ExponentialBackoff` is available, the job will use it. (Important: Your load/require order is important if you want exponential backoff with the built-in job due to [its error handling](https://github.com/stitchfix/pwwka/blob/713c6003fa6cf52cb4713c02b39fe7ee07ebe2e9/lib/pwwka/send_message_async_job.rb#L8)). Customize the backoff intervals using the configuration `send_message_resque_backoff_strategy`. The default backoff will retry quickly in case of an intermittent glitch, and then every ten minutes for half an hour
|
189
|
+
|
190
|
+
|
191
|
+
|
182
192
|
#### Message Queuer
|
183
193
|
|
184
194
|
You can queue up messages and send them in a batch. This is most useful when multiple messages
|
data/lib/pwwka.rb
CHANGED
@@ -14,16 +14,27 @@ module Pwwka
|
|
14
14
|
@configuration = Pwwka.configuration
|
15
15
|
connection_options = {automatically_recover: false}.merge(configuration.options)
|
16
16
|
connection_options = {client_properties: {connection_name: connection_name}}.merge(connection_options) if connection_name
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
|
18
|
+
begin
|
19
|
+
@connection = Bunny.new(configuration.rabbit_mq_host, connection_options)
|
20
|
+
@connection.start
|
21
|
+
rescue => e
|
22
|
+
logf "ERROR Connecting to RabbitMQ", error: e
|
23
|
+
@connection.close if @connection
|
24
|
+
raise e
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
@channel = @connection.create_channel
|
29
|
+
rescue => e
|
30
|
+
logf "ERROR Opening RabbitMQ channel", error: e
|
31
|
+
@connection.close if @connection
|
32
|
+
raise e
|
33
|
+
end
|
34
|
+
|
20
35
|
if prefetch
|
21
36
|
@channel.prefetch(prefetch.to_i)
|
22
37
|
end
|
23
|
-
rescue => e
|
24
|
-
logf "ERROR Connecting to RabbitMQ", error: e
|
25
|
-
@connection.close if @connection
|
26
|
-
raise e
|
27
38
|
end
|
28
39
|
|
29
40
|
def topic_exchange
|
@@ -67,9 +78,18 @@ module Pwwka
|
|
67
78
|
end
|
68
79
|
|
69
80
|
def connection_close
|
70
|
-
|
71
|
-
|
72
|
-
|
81
|
+
begin
|
82
|
+
channel.close
|
83
|
+
rescue => e
|
84
|
+
logf "ERROR Closing RabbitMQ channel", error: e
|
85
|
+
end
|
73
86
|
|
87
|
+
begin
|
88
|
+
connection.close
|
89
|
+
rescue => e
|
90
|
+
logf "ERROR Closing connection to RabbitMQ", error: e
|
91
|
+
raise e
|
92
|
+
end
|
93
|
+
end
|
74
94
|
end
|
75
95
|
end
|
data/lib/pwwka/configuration.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'bunny'
|
2
2
|
require 'mono_logger'
|
3
|
+
|
3
4
|
module Pwwka
|
4
5
|
class ConfigurationError < StandardError; end
|
5
6
|
class Configuration
|
@@ -10,12 +11,13 @@ module Pwwka
|
|
10
11
|
attr_accessor :logger
|
11
12
|
attr_accessor :log_level
|
12
13
|
attr_accessor :options
|
13
|
-
attr_accessor :
|
14
|
+
attr_accessor :background_job_processor
|
14
15
|
attr_accessor :send_message_resque_backoff_strategy
|
15
16
|
attr_accessor :default_prefetch
|
16
17
|
attr_accessor :process_name
|
17
18
|
attr_reader :requeue_on_error
|
18
19
|
attr_writer :app_id
|
20
|
+
attr_writer :async_job_klass
|
19
21
|
attr_writer :error_handling_chain
|
20
22
|
|
21
23
|
def initialize
|
@@ -30,7 +32,7 @@ module Pwwka
|
|
30
32
|
600, 600, 600] # longer-term outage?
|
31
33
|
@requeue_on_error = false
|
32
34
|
@keep_alive_on_handler_klass_exceptions = false
|
33
|
-
@
|
35
|
+
@background_job_processor = :resque
|
34
36
|
@default_prefetch = nil
|
35
37
|
@receive_raw_payload = false
|
36
38
|
@process_name = ""
|
@@ -56,6 +58,10 @@ module Pwwka
|
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
61
|
+
def async_job_klass
|
62
|
+
@async_job_klass || background_jobs[background_job_processor]
|
63
|
+
end
|
64
|
+
|
59
65
|
def payload_logging
|
60
66
|
@payload_logging || :info
|
61
67
|
end
|
@@ -139,5 +145,14 @@ module Pwwka
|
|
139
145
|
return true if @receive_raw_payload
|
140
146
|
Pwwka::Logging::LEVELS[Pwwka.configuration.payload_logging.to_sym] > Pwwka::Logging::LEVELS[level_of_message_with_payload.to_sym]
|
141
147
|
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def background_jobs
|
152
|
+
{
|
153
|
+
resque: Pwwka::SendMessageAsyncJob,
|
154
|
+
sidekiq: Pwwka::SendMessageAsyncSidekiqJob,
|
155
|
+
}
|
156
|
+
end
|
142
157
|
end
|
143
158
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'sidekiq'
|
2
|
+
|
3
|
+
module Pwwka
|
4
|
+
class SendMessageAsyncSidekiqJob
|
5
|
+
include Sidekiq::Worker
|
6
|
+
extend Pwwka::Logging
|
7
|
+
|
8
|
+
sidekiq_options queue: 'pwwka_send_message_async', retry: 3
|
9
|
+
|
10
|
+
def perform(payload, routing_key, options = {})
|
11
|
+
type = options["type"]
|
12
|
+
message_id = options["message_id"] || "auto_generate"
|
13
|
+
headers = options["headers"]
|
14
|
+
|
15
|
+
logger.info("Sending message async #{routing_key}, #{payload}")
|
16
|
+
|
17
|
+
message_id = message_id.to_sym if message_id == "auto_generate"
|
18
|
+
|
19
|
+
Pwwka::Transmitter.send_message!(
|
20
|
+
payload,
|
21
|
+
routing_key,
|
22
|
+
type: type,
|
23
|
+
message_id: message_id,
|
24
|
+
headers: headers,
|
25
|
+
on_error: :raise,
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/pwwka/transmitter.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative "publish_options"
|
|
3
3
|
begin # optional dependency
|
4
4
|
require 'resque'
|
5
5
|
require 'resque-retry'
|
6
|
+
require 'sidekiq'
|
6
7
|
rescue LoadError
|
7
8
|
end
|
8
9
|
|
@@ -43,6 +44,7 @@ module Pwwka
|
|
43
44
|
# - :ignore (aka as send_message_safely)
|
44
45
|
# - :raise
|
45
46
|
# - :resque -- use Resque to try to send the message later
|
47
|
+
# - :retry_async -- use the configured background job processor to retry sending the message later
|
46
48
|
#
|
47
49
|
# Returns true
|
48
50
|
#
|
@@ -70,31 +72,39 @@ module Pwwka
|
|
70
72
|
when :raise
|
71
73
|
raise e
|
72
74
|
|
73
|
-
when :resque
|
75
|
+
when :resque, :retry_async
|
74
76
|
begin
|
75
77
|
send_message_async(payload, routing_key, delay_by_ms: delayed ? delay_by || DEFAULT_DELAY_BY_MS : 0)
|
76
|
-
rescue =>
|
77
|
-
warn(
|
78
|
+
rescue => exception
|
79
|
+
warn(exception.message)
|
78
80
|
raise e
|
79
81
|
end
|
82
|
+
|
80
83
|
else # ignore
|
81
84
|
end
|
82
85
|
false
|
83
86
|
end
|
84
87
|
|
85
|
-
#
|
88
|
+
# Enqueue the message with the configured background processor.
|
86
89
|
# - :delay_by_ms:: Integer milliseconds to delay the message. Default is 0.
|
87
90
|
def self.send_message_async(payload, routing_key,
|
88
91
|
delay_by_ms: 0,
|
89
92
|
type: nil,
|
90
93
|
message_id: :auto_generate,
|
91
94
|
headers: nil)
|
95
|
+
background_job_processor = Pwwka.configuration.background_job_processor
|
92
96
|
job = Pwwka.configuration.async_job_klass
|
93
|
-
|
94
|
-
if
|
95
|
-
|
96
|
-
|
97
|
-
|
97
|
+
|
98
|
+
if background_job_processor == :resque
|
99
|
+
# Be perhaps too carefully making sure we queue jobs in the legacy way
|
100
|
+
if type == nil && message_id == :auto_generate && headers == nil
|
101
|
+
Resque.enqueue_in(delay_by_ms/1000, job, payload, routing_key)
|
102
|
+
else
|
103
|
+
Resque.enqueue_in(delay_by_ms/1000, job, payload, routing_key, type: type, message_id: message_id, headers: headers)
|
104
|
+
end
|
105
|
+
elsif background_job_processor == :sidekiq
|
106
|
+
options = { delay_by_ms: delay_by_ms, type: type, message_id: message_id, headers: headers }
|
107
|
+
job.perform_async(payload, routing_key, options)
|
98
108
|
end
|
99
109
|
end
|
100
110
|
|
data/lib/pwwka/version.rb
CHANGED
data/pwwka.gemspec
CHANGED
@@ -27,8 +27,10 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.add_development_dependency("rspec")
|
28
28
|
s.add_development_dependency("resque")
|
29
29
|
s.add_development_dependency("resque-retry")
|
30
|
+
s.add_development_dependency("sidekiq")
|
30
31
|
s.add_development_dependency("simplecov")
|
31
32
|
s.add_development_dependency("resqutils")
|
32
33
|
s.add_development_dependency("rainbow")
|
33
|
-
s.add_development_dependency(
|
34
|
+
s.add_development_dependency("rspec_junit_formatter")
|
35
|
+
s.add_development_dependency("pry-byebug")
|
34
36
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,7 @@ GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__),'..'))
|
|
3
3
|
ENV['RAILS_ENV'] ||= 'test'
|
4
4
|
|
5
5
|
require 'simplecov'
|
6
|
+
require 'pry-byebug'
|
6
7
|
|
7
8
|
SimpleCov.start do
|
8
9
|
add_filter "/spec/"
|
@@ -16,6 +17,7 @@ require 'active_support/core_ext/hash'
|
|
16
17
|
# an error if missing. Requiring here so their absence will fail the tests
|
17
18
|
require 'resque'
|
18
19
|
require 'resque-retry'
|
20
|
+
require 'sidekiq'
|
19
21
|
|
20
22
|
require 'support/test_configuration'
|
21
23
|
|
@@ -155,4 +155,32 @@ describe Pwwka::Configuration do
|
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
158
|
+
describe "#background_job_processor" do
|
159
|
+
it "is :resque by default" do
|
160
|
+
expect(configuration.background_job_processor).to eq(:resque)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "can be overridden" do
|
164
|
+
configuration.background_job_processor = :sidekiq
|
165
|
+
expect(configuration.background_job_processor).to eq(:sidekiq)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "#async_job_klass" do
|
170
|
+
it "is SendMessageAsyncJob by default" do
|
171
|
+
expect(configuration.async_job_klass).to eq(Pwwka::SendMessageAsyncJob)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "is SendMessageAsyncSidekiqJob when background_job_processor=:sidekiq" do
|
175
|
+
configuration.background_job_processor = :sidekiq
|
176
|
+
expect(configuration.async_job_klass).to eq(Pwwka::SendMessageAsyncSidekiqJob)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "can be configured to a custom class" do
|
180
|
+
class CustomBackgroundJob
|
181
|
+
end
|
182
|
+
configuration.async_job_klass = CustomBackgroundJob
|
183
|
+
expect(configuration.async_job_klass).to eq(CustomBackgroundJob)
|
184
|
+
end
|
185
|
+
end
|
158
186
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Pwwka::SendMessageAsyncSidekiqJob do
|
4
|
+
describe "#perform" do
|
5
|
+
before do
|
6
|
+
allow(Pwwka::Transmitter).to receive(:send_message!)
|
7
|
+
end
|
8
|
+
|
9
|
+
context "with just two arguments" do
|
10
|
+
it "calls through to Pwwka::Transmitter, setting error handling to 'raise'" do
|
11
|
+
described_class.new.perform({ "foo" => "bar"} , "some.routing.key")
|
12
|
+
expect(Pwwka::Transmitter).to have_received(:send_message!).with(
|
13
|
+
{ "foo" => "bar" },
|
14
|
+
"some.routing.key",
|
15
|
+
type: nil,
|
16
|
+
message_id: :auto_generate,
|
17
|
+
headers: nil,
|
18
|
+
on_error: :raise
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "with optional values" do
|
24
|
+
it "passes them through to Pwwka::Transmitter" do
|
25
|
+
described_class.new.perform(
|
26
|
+
{ "foo" => "bar"},
|
27
|
+
"some.routing.key",
|
28
|
+
"type" => "Customer",
|
29
|
+
"message_id" => "foobar",
|
30
|
+
"headers" => { "x" => "y" }
|
31
|
+
)
|
32
|
+
expect(Pwwka::Transmitter).to have_received(:send_message!).with(
|
33
|
+
{ "foo" => "bar" },
|
34
|
+
"some.routing.key",
|
35
|
+
type: "Customer",
|
36
|
+
message_id: "foobar",
|
37
|
+
headers: { "x" => "y" },
|
38
|
+
on_error: :raise
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -33,41 +33,91 @@ describe Pwwka::Transmitter do
|
|
33
33
|
subject(:transmitter) { described_class.new }
|
34
34
|
|
35
35
|
describe ".send_message_async" do
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
36
|
+
context "when configured background_job_processor is Resque" do
|
37
|
+
before do
|
38
|
+
allow(Resque).to receive(:enqueue_in)
|
39
|
+
end
|
40
|
+
context "with only basic required arguments" do
|
41
|
+
it "queues a Resque job with no extra args" do
|
42
|
+
delay_by_ms = 3_000
|
43
|
+
described_class.send_message_async(payload,routing_key,delay_by_ms: delay_by_ms)
|
44
|
+
expect(Resque).to have_received(:enqueue_in).with(delay_by_ms/1_000,Pwwka::SendMessageAsyncJob,payload,routing_key)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
context "with everything overridden" do
|
48
|
+
it "queues a Resque job with the various arguments" do
|
49
|
+
delay_by_ms = 3_000
|
50
|
+
described_class.send_message_async(
|
51
|
+
payload,routing_key,
|
52
|
+
delay_by_ms: delay_by_ms,
|
53
|
+
message_id: "snowflake id that is likely a bad idea, but if you must",
|
54
|
+
type: "Customer",
|
55
|
+
headers: {
|
56
|
+
"custom" => "value",
|
57
|
+
"other_custom" => "other_value",
|
58
|
+
}
|
59
|
+
)
|
60
|
+
expect(Resque).to have_received(:enqueue_in).with(
|
61
|
+
delay_by_ms/1_000,
|
62
|
+
Pwwka::SendMessageAsyncJob,
|
63
|
+
payload,
|
64
|
+
routing_key,
|
65
|
+
message_id: "snowflake id that is likely a bad idea, but if you must",
|
66
|
+
type: "Customer",
|
67
|
+
headers: {
|
68
|
+
"custom" => "value",
|
69
|
+
"other_custom" => "other_value",
|
70
|
+
}
|
71
|
+
)
|
72
|
+
end
|
44
73
|
end
|
45
74
|
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
delay_by_ms
|
61
|
-
Pwwka::
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
75
|
+
|
76
|
+
context "when the configured background_job_processor is Sidekiq" do
|
77
|
+
before do
|
78
|
+
allow(Pwwka::SendMessageAsyncSidekiqJob).to receive(:perform_async)
|
79
|
+
Pwwka.configuration.background_job_processor = :sidekiq
|
80
|
+
end
|
81
|
+
|
82
|
+
after do
|
83
|
+
Pwwka::configuration.background_job_processor = :resque
|
84
|
+
end
|
85
|
+
|
86
|
+
context "with only basic required arguments" do
|
87
|
+
it "queues a Sidekiq job with no extra arguments" do
|
88
|
+
options = { delay_by_ms: 3_000, type: nil, message_id: :auto_generate, headers: nil}
|
89
|
+
described_class.send_message_async(payload, routing_key, delay_by_ms: 3_000)
|
90
|
+
expect(Pwwka::SendMessageAsyncSidekiqJob).to have_received(:perform_async)
|
91
|
+
.with(payload, routing_key, options)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "with everything overridden" do
|
96
|
+
it "queues a Sidekiq job with the various arguments" do
|
97
|
+
described_class.send_message_async(
|
98
|
+
payload,routing_key,
|
99
|
+
delay_by_ms: 3_000,
|
100
|
+
message_id: "snowflake id that is likely a bad idea, but if you must",
|
101
|
+
type: "Customer",
|
102
|
+
headers: {
|
103
|
+
"custom" => "value",
|
104
|
+
"other_custom" => "other_value",
|
105
|
+
}
|
106
|
+
)
|
107
|
+
expect(Pwwka::SendMessageAsyncSidekiqJob).to have_received(:perform_async).with(
|
108
|
+
payload,
|
109
|
+
routing_key,
|
110
|
+
{
|
111
|
+
delay_by_ms: 3_000,
|
112
|
+
type: "Customer",
|
113
|
+
message_id: "snowflake id that is likely a bad idea, but if you must",
|
114
|
+
headers: {
|
115
|
+
"custom" => "value",
|
116
|
+
"other_custom" => "other_value",
|
117
|
+
}
|
118
|
+
}
|
119
|
+
)
|
120
|
+
end
|
71
121
|
end
|
72
122
|
end
|
73
123
|
end
|
@@ -283,6 +333,81 @@ describe Pwwka::Transmitter do
|
|
283
333
|
end
|
284
334
|
end
|
285
335
|
end
|
336
|
+
|
337
|
+
context "on_error: :retry_async" do
|
338
|
+
context "when configured background_job_processor is Resque" do
|
339
|
+
context "when the job is queued successfully" do
|
340
|
+
before do
|
341
|
+
allow(Resque).to receive(:enqueue_in)
|
342
|
+
end
|
343
|
+
|
344
|
+
it "queues a Resque job" do
|
345
|
+
described_class.send_message!(payload, routing_key, on_error: :retry_async)
|
346
|
+
expect(Resque).to have_received(:enqueue_in).with(0, Pwwka::SendMessageAsyncJob, payload, routing_key)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
context "when there is a problem queueing the Resque job" do
|
351
|
+
before do
|
352
|
+
allow(Resque).to receive(:enqueue_in).and_raise("NOPE")
|
353
|
+
end
|
354
|
+
|
355
|
+
it "raises the original exception job" do
|
356
|
+
expect {
|
357
|
+
described_class.send_message!(payload,routing_key, on_error: :retry_async)
|
358
|
+
}.to raise_error(/OH NOES/)
|
359
|
+
end
|
360
|
+
|
361
|
+
it "logs the Resque error as a warning" do
|
362
|
+
begin
|
363
|
+
described_class.send_message!(payload,routing_key, on_error: :resque)
|
364
|
+
rescue => ex
|
365
|
+
end
|
366
|
+
expect(logger).to have_received(:warn).with(/NOPE/)
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
context "when configured background_job_processor is Sidekiq" do
|
372
|
+
before do
|
373
|
+
Pwwka.configuration.background_job_processor = :sidekiq
|
374
|
+
end
|
375
|
+
|
376
|
+
after do
|
377
|
+
Pwwka::configuration.background_job_processor = :resque
|
378
|
+
end
|
379
|
+
|
380
|
+
context "when the job is queued sucessfully" do
|
381
|
+
it "queues a Sidekiq job" do
|
382
|
+
allow(Pwwka::SendMessageAsyncSidekiqJob).to receive(:perform_async)
|
383
|
+
described_class.send_message!(payload, routing_key, on_error: :retry_async)
|
384
|
+
expect(Pwwka::SendMessageAsyncSidekiqJob).to have_received(:perform_async).with(
|
385
|
+
payload,
|
386
|
+
routing_key,
|
387
|
+
{delay_by_ms: 0, headers: nil, message_id: :auto_generate, type: nil}
|
388
|
+
)
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
context "when there is a problem queueing the Sidekiq job" do
|
393
|
+
it "raises the original exception job" do
|
394
|
+
allow(Pwwka::SendMessageAsyncSidekiqJob).to receive(:perform_async).and_raise("NOPE")
|
395
|
+
expect {
|
396
|
+
described_class.send_message!(payload, routing_key, on_error: :retry_async)
|
397
|
+
}.to raise_error(/OH NOES/)
|
398
|
+
end
|
399
|
+
|
400
|
+
it "logs the Sidekiq error as a warning" do
|
401
|
+
allow(Pwwka::SendMessageAsyncSidekiqJob).to receive(:perform_async).and_raise("NOPE")
|
402
|
+
begin
|
403
|
+
described_class.send_message!(payload,routing_key, on_error: :retry_async)
|
404
|
+
rescue => ex
|
405
|
+
end
|
406
|
+
expect(logger).to have_received(:warn).with(/NOPE/)
|
407
|
+
end
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
286
411
|
end
|
287
412
|
end
|
288
413
|
describe ".send_message_safely" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pwwka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.21.
|
4
|
+
version: 0.21.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stitch Fix Engineering
|
@@ -15,7 +15,7 @@ authors:
|
|
15
15
|
autorequire:
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
|
-
date: 2019-01-
|
18
|
+
date: 2019-01-16 00:00:00.000000000 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: bunny
|
@@ -129,6 +129,20 @@ dependencies:
|
|
129
129
|
- - ">="
|
130
130
|
- !ruby/object:Gem::Version
|
131
131
|
version: '0'
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
name: sidekiq
|
134
|
+
requirement: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
132
146
|
- !ruby/object:Gem::Dependency
|
133
147
|
name: simplecov
|
134
148
|
requirement: !ruby/object:Gem::Requirement
|
@@ -185,6 +199,20 @@ dependencies:
|
|
185
199
|
- - ">="
|
186
200
|
- !ruby/object:Gem::Version
|
187
201
|
version: '0'
|
202
|
+
- !ruby/object:Gem::Dependency
|
203
|
+
name: pry-byebug
|
204
|
+
requirement: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
209
|
+
type: :development
|
210
|
+
prerelease: false
|
211
|
+
version_requirements: !ruby/object:Gem::Requirement
|
212
|
+
requirements:
|
213
|
+
- - ">="
|
214
|
+
- !ruby/object:Gem::Version
|
215
|
+
version: '0'
|
188
216
|
description: |-
|
189
217
|
The purpose of this gem is to normalise the sending and
|
190
218
|
receiving of messages between Rails apps using the shared RabbitMQ
|
@@ -239,6 +267,7 @@ files:
|
|
239
267
|
- lib/pwwka/queue_resque_job_handler.rb
|
240
268
|
- lib/pwwka/receiver.rb
|
241
269
|
- lib/pwwka/send_message_async_job.rb
|
270
|
+
- lib/pwwka/send_message_async_sidekiq_job.rb
|
242
271
|
- lib/pwwka/tasks.rb
|
243
272
|
- lib/pwwka/test_handler.rb
|
244
273
|
- lib/pwwka/transmitter.rb
|
@@ -266,6 +295,7 @@ files:
|
|
266
295
|
- spec/unit/queue_resque_job_handler_spec.rb
|
267
296
|
- spec/unit/receiver_spec.rb
|
268
297
|
- spec/unit/send_message_async_job_spec.rb
|
298
|
+
- spec/unit/send_message_async_sidekiq_job_spec.rb
|
269
299
|
- spec/unit/test_handler_message_spec.rb
|
270
300
|
- spec/unit/transmitter_spec.rb
|
271
301
|
homepage: https://github.com/stitchfix/pwwka
|
@@ -314,5 +344,6 @@ test_files:
|
|
314
344
|
- spec/unit/queue_resque_job_handler_spec.rb
|
315
345
|
- spec/unit/receiver_spec.rb
|
316
346
|
- spec/unit/send_message_async_job_spec.rb
|
347
|
+
- spec/unit/send_message_async_sidekiq_job_spec.rb
|
317
348
|
- spec/unit/test_handler_message_spec.rb
|
318
349
|
- spec/unit/transmitter_spec.rb
|