sneakers 0.0.7 → 1.0.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 +7 -0
- data/.gitignore +4 -1
- data/CHANGELOG.md +20 -0
- data/LICENSE.txt +2 -2
- data/README.md +15 -0
- data/ROADMAP.md +18 -0
- data/Rakefile +1 -0
- data/examples/benchmark_worker.rb +0 -1
- data/examples/max_retry_handler.rb +78 -0
- data/examples/metrics_worker.rb +1 -1
- data/examples/newrelic_metrics_worker.rb +40 -0
- data/examples/profiling_worker.rb +35 -21
- data/examples/workflow_worker.rb +0 -1
- data/lib/sneakers/cli.rb +7 -3
- data/lib/sneakers/concerns/logging.rb +1 -1
- data/lib/sneakers/configuration.rb +47 -0
- data/lib/sneakers/handlers/maxretry.rb +183 -0
- data/lib/sneakers/handlers/oneshot.rb +11 -10
- data/lib/sneakers/metrics/newrelic_metrics.rb +37 -0
- data/lib/sneakers/metrics/statsd_metrics.rb +1 -1
- data/lib/sneakers/publisher.rb +11 -12
- data/lib/sneakers/queue.rb +22 -7
- data/lib/sneakers/runner.rb +4 -4
- data/lib/sneakers/support/utils.rb +1 -1
- data/lib/sneakers/tasks.rb +1 -1
- data/lib/sneakers/version.rb +1 -1
- data/lib/sneakers/worker.rb +53 -23
- data/lib/sneakers/workergroup.rb +2 -2
- data/lib/sneakers.rb +23 -49
- data/sneakers.gemspec +22 -22
- data/spec/sneakers/concerns/{logging.rb → logging_spec.rb} +1 -1
- data/spec/sneakers/configuration_spec.rb +42 -0
- data/spec/sneakers/publisher_spec.rb +58 -17
- data/spec/sneakers/queue_spec.rb +64 -18
- data/spec/sneakers/sneakers_spec.rb +15 -6
- data/spec/sneakers/support/utils_spec.rb +44 -0
- data/spec/sneakers/worker_handlers_spec.rb +350 -0
- data/spec/sneakers/worker_spec.rb +130 -45
- data/spec/spec_helper.rb +3 -1
- metadata +52 -67
- data/Gemfile.lock +0 -153
- data/lib/sneakers/support/queue_name.rb +0 -14
- /data/spec/sneakers/concerns/{metrics.rb → metrics_spec.rb} +0 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 9d39e1dc0f9c583d63d7ba5d6cf8078efe94f93f
|
|
4
|
+
data.tar.gz: 7bf1d48e4a492d454db3afc7474b9053adc05531
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d7c50e77e8a9de0e4ebed4d6120e78c5b5438d4d430ed776a686a69e6a288f000a4a1f40cf9a0bb5ab861bc7e5cb8fc93a5d8241301f15888b5ac3682447ae58
|
|
7
|
+
data.tar.gz: 80942f71ebc85d9365ada65413ec16f0f3444631e568f930de6279d6924301f74745336449e72a7da1963b01e0945fa0d155654164b7f3af44e22c9b6851def0
|
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 0.1.0, Not released yet
|
|
2
|
+
|
|
3
|
+
+ Added newrelic stats reporter (@arielze)
|
|
4
|
+
+ Added explicit routing_key at queue (@arielze)
|
|
5
|
+
+ Depracating self-daemonization (@jondot)
|
|
6
|
+
+ updating bunny (@michaelklishin)
|
|
7
|
+
+ Fix reference in publisher (@sergei-matheson)
|
|
8
|
+
+ Allow binding of multiple routing keys (@SebastianEdwards)
|
|
9
|
+
+ added work_with_params for advanced handling of work items with amqp
|
|
10
|
+
headers. (#12)
|
|
11
|
+
+ Sneakers.not_environmental! will no disable auto-environment discovery.
|
|
12
|
+
(#15)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# 0.0.7
|
|
18
|
+
|
|
19
|
+
+ Sneakers core.
|
|
20
|
+
|
data/LICENSE.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c) 2013 Dotan Nahum
|
|
1
|
+
Copyright (c) 2013-2014 Dotan Nahum
|
|
2
2
|
|
|
3
3
|
MIT License
|
|
4
4
|
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
A high-performance RabbitMQ background processing framework for
|
|
13
13
|
Ruby.
|
|
14
14
|
|
|
15
|
+
|
|
16
|
+
Sneakers is being used in production for both I/O and CPU intensive workloads, and have achieved the goals of high-performance and 0-maintenance, as designed.
|
|
17
|
+
|
|
18
|
+
|
|
15
19
|
Visit the [wiki](https://github.com/jondot/sneakers/wiki) for
|
|
16
20
|
complete docs.
|
|
17
21
|
|
|
@@ -146,6 +150,17 @@ From here, you can continue over to the
|
|
|
146
150
|
|
|
147
151
|
Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).
|
|
148
152
|
|
|
153
|
+
|
|
154
|
+
### Thanks:
|
|
155
|
+
|
|
156
|
+
* Michael Klishin - @michaelklishin
|
|
157
|
+
* Ariel Zerahia - @arielze
|
|
158
|
+
* @sergei-matheson
|
|
159
|
+
* Sebastian Edwards - @SebastianEdwards
|
|
160
|
+
* Tor Ivry - @torkale
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
149
164
|
# Copyright
|
|
150
165
|
|
|
151
166
|
Copyright (c) 2013 [Dotan Nahum](http://gplus.to/dotan) [@jondot](http://twitter.com/jondot). See MIT-LICENSE for further details.
|
data/ROADMAP.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Roadmap
|
|
2
|
+
|
|
3
|
+
It is essential to keep a clear roadmap.
|
|
4
|
+
In its core, Sneakers is already production-ready. However, it is vital
|
|
5
|
+
to take in feedback and improve what can be improved :)
|
|
6
|
+
|
|
7
|
+
This document will communicate the stages that the next version is at.
|
|
8
|
+
See `CHANGELOG.md` for new features that are in.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# v0.1.0 (next version)
|
|
12
|
+
|
|
13
|
+
- Taking in community pull requests and feedback
|
|
14
|
+
- Verify production readiness and performance
|
|
15
|
+
- Full performance test suite
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
data/Rakefile
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
|
2
|
+
require 'sneakers'
|
|
3
|
+
require 'sneakers/runner'
|
|
4
|
+
require 'sneakers/handlers/maxretry'
|
|
5
|
+
require 'logger'
|
|
6
|
+
|
|
7
|
+
Sneakers.configure(:handler => Sneakers::Handlers::Maxretry,
|
|
8
|
+
:workers => 1,
|
|
9
|
+
:threads => 1,
|
|
10
|
+
:prefetch => 1,
|
|
11
|
+
:exchange => 'sneakers',
|
|
12
|
+
:exchange_type => 'topic',
|
|
13
|
+
:routing_key => ['#', 'something'],
|
|
14
|
+
:durable => true,
|
|
15
|
+
)
|
|
16
|
+
Sneakers.logger.level = Logger::DEBUG
|
|
17
|
+
|
|
18
|
+
WORKER_OPTIONS = {
|
|
19
|
+
:ack => true,
|
|
20
|
+
:threads => 1,
|
|
21
|
+
:prefetch => 1,
|
|
22
|
+
:timeout_job_after => 60,
|
|
23
|
+
:heartbeat => 5,
|
|
24
|
+
:retry_timeout => 5000
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# Example of how to write a retry worker. If your rabbit system is empty, then
|
|
28
|
+
# you must run this twice. Once to setup the exchanges, queues and bindings a
|
|
29
|
+
# second time to have the sent message end up on the downloads queue.
|
|
30
|
+
#
|
|
31
|
+
# Run this via:
|
|
32
|
+
# bundle exec ruby examples/max_retry_handler.rb
|
|
33
|
+
#
|
|
34
|
+
class MaxRetryWorker
|
|
35
|
+
include Sneakers::Worker
|
|
36
|
+
from_queue 'downloads',
|
|
37
|
+
WORKER_OPTIONS.merge({
|
|
38
|
+
:arguments => {
|
|
39
|
+
:'x-dead-letter-exchange' => 'downloads-retry'
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
def work(msg)
|
|
44
|
+
logger.info("MaxRetryWorker rejecting msg: #{msg.inspect}")
|
|
45
|
+
|
|
46
|
+
# We always want to reject to see if we do the proper timeout
|
|
47
|
+
reject!
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Example of a worker on the same exchange that does not fail, so it should only
|
|
52
|
+
# see the message once.
|
|
53
|
+
class SucceedingWorker
|
|
54
|
+
include Sneakers::Worker
|
|
55
|
+
from_queue 'uploads',
|
|
56
|
+
WORKER_OPTIONS.merge({
|
|
57
|
+
:arguments => {
|
|
58
|
+
:'x-dead-letter-exchange' => 'uploads-retry'
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
def work(msg)
|
|
63
|
+
logger.info("SucceedingWorker succeeding on msg: #{msg.inspect}")
|
|
64
|
+
ack!
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
messages = 1
|
|
69
|
+
puts "feeding messages in"
|
|
70
|
+
messages.times {
|
|
71
|
+
Sneakers.publish(" -- message -- ",
|
|
72
|
+
:to_queue => 'anywhere',
|
|
73
|
+
:persistence => true)
|
|
74
|
+
}
|
|
75
|
+
puts "done"
|
|
76
|
+
|
|
77
|
+
r = Sneakers::Runner.new([MaxRetryWorker, SucceedingWorker])
|
|
78
|
+
r.run
|
data/examples/metrics_worker.rb
CHANGED
|
@@ -19,7 +19,7 @@ class MetricsWorker
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
Sneakers.configure(
|
|
22
|
+
Sneakers.configure(:metrics => Sneakers::Metrics::LoggingMetrics.new)
|
|
23
23
|
r = Sneakers::Runner.new([ MetricsWorker ])
|
|
24
24
|
r.run
|
|
25
25
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
|
2
|
+
require 'sneakers'
|
|
3
|
+
require 'sneakers/runner'
|
|
4
|
+
require 'sneakers/metrics/newrelic_metrics'
|
|
5
|
+
require 'open-uri'
|
|
6
|
+
require 'nokogiri'
|
|
7
|
+
require 'newrelic_rpm'
|
|
8
|
+
|
|
9
|
+
# With this configuration will send two types of data to newrelic server:
|
|
10
|
+
# 1. Transaction data which you would see under 'Applications'
|
|
11
|
+
# 2. Metrics where you will be able to see by configuring a dashboardi, available for enterprise accounts
|
|
12
|
+
#
|
|
13
|
+
# You should have newrelic.yml in the 'config' folder with the proper account settings
|
|
14
|
+
|
|
15
|
+
Sneakers::Metrics::NewrelicMetrics.eagent ::NewRelic
|
|
16
|
+
Sneakers.configure metrics: Sneakers::Metrics::NewrelicMetrics.new
|
|
17
|
+
|
|
18
|
+
class MetricsWorker
|
|
19
|
+
include Sneakers::Worker
|
|
20
|
+
include ::NewRelic::Agent::Instrumentation::ControllerInstrumentation
|
|
21
|
+
|
|
22
|
+
from_queue 'downloads'
|
|
23
|
+
|
|
24
|
+
def work(msg)
|
|
25
|
+
doc = Nokogiri::HTML(open(msg))
|
|
26
|
+
logger.info "FOUND <#{doc.css('title').text}>"
|
|
27
|
+
ack!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
add_transaction_tracer :work, name: 'MetricsWorker', params: 'args[0]'
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
r = Sneakers::Runner.new([ MetricsWorker ])
|
|
36
|
+
r.run
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
$: << File.expand_path('../lib', File.dirname(__FILE__))
|
|
2
2
|
require 'sneakers'
|
|
3
3
|
require 'sneakers/runner'
|
|
4
|
-
require '
|
|
4
|
+
require 'logger'
|
|
5
5
|
|
|
6
6
|
|
|
7
|
+
profiling = ARGV[0]
|
|
8
|
+
messages = 100_000
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
if profiling
|
|
12
|
+
require 'ruby-prof'
|
|
13
|
+
messages /= 100 # profiling makes everything much slower (around 300req/s)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Sneakers.configure
|
|
17
|
+
Sneakers.logger.level = Logger::ERROR
|
|
18
|
+
|
|
19
|
+
Sneakers::Worker.configure_logger(Logger.new('/dev/null'))
|
|
20
|
+
|
|
7
21
|
puts "feeding messages in"
|
|
8
|
-
|
|
22
|
+
messages.times {
|
|
9
23
|
Sneakers.publish("{}", :to_queue => 'downloads')
|
|
24
|
+
}
|
|
10
25
|
puts "done"
|
|
11
26
|
|
|
12
27
|
|
|
13
28
|
class ProfilingWorker
|
|
14
29
|
include Sneakers::Worker
|
|
15
30
|
from_queue 'downloads',
|
|
16
|
-
:env => '',
|
|
17
|
-
:durable => false,
|
|
18
31
|
:ack => true,
|
|
19
32
|
:threads => 50,
|
|
20
33
|
:prefetch => 50,
|
|
21
34
|
:timeout_job_after => 1,
|
|
22
|
-
:exchange => '
|
|
35
|
+
:exchange => 'sneakers',
|
|
23
36
|
:heartbeat => 5
|
|
24
37
|
def work(msg)
|
|
25
38
|
ack!
|
|
@@ -28,28 +41,29 @@ end
|
|
|
28
41
|
|
|
29
42
|
|
|
30
43
|
|
|
31
|
-
r = Sneakers::Runner.new
|
|
32
|
-
Sneakers::Worker.configure_logger(Logger.new('/dev/null'))
|
|
44
|
+
r = Sneakers::Runner.new([ProfilingWorker])
|
|
33
45
|
|
|
34
46
|
# ctrl-c and Ruby 2.0 breaks signal handling
|
|
35
47
|
# Sidekiq has same issues
|
|
36
48
|
# https://github.com/mperham/sidekiq/issues/728
|
|
37
49
|
#
|
|
38
50
|
# so we use a timeout and a thread that kills profiling
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
if profiling
|
|
52
|
+
puts "profiling start"
|
|
53
|
+
RubyProf.start
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
Thread.new do
|
|
57
|
+
sleep 10
|
|
58
|
+
puts "stopping profiler"
|
|
59
|
+
result = RubyProf.stop
|
|
60
|
+
|
|
61
|
+
# Print a flat profile to text
|
|
62
|
+
printer = RubyProf::FlatPrinter.new(result)
|
|
63
|
+
printer.print(STDOUT)
|
|
64
|
+
exit(0)
|
|
65
|
+
end
|
|
52
66
|
end
|
|
53
67
|
|
|
54
|
-
r.run
|
|
68
|
+
r.run
|
|
55
69
|
|
data/examples/workflow_worker.rb
CHANGED
data/lib/sneakers/cli.rb
CHANGED
|
@@ -35,6 +35,10 @@ module Sneakers
|
|
|
35
35
|
|
|
36
36
|
opts[:log] = opts[:daemonize] ? 'sneakers.log' : STDOUT
|
|
37
37
|
|
|
38
|
+
if opts[:daemonize]
|
|
39
|
+
puts "*** DEPRACATED: self-daemonization '--daemonize' is considered a bad practice, which is why this feature will be removed in future versions. Please run Sneakers in front, and use things like upstart, systemd, or supervisor to manage it as a daemon."
|
|
40
|
+
end
|
|
41
|
+
|
|
38
42
|
|
|
39
43
|
Sneakers.configure(opts)
|
|
40
44
|
|
|
@@ -62,11 +66,11 @@ module Sneakers
|
|
|
62
66
|
|
|
63
67
|
r = Sneakers::Runner.new(workers)
|
|
64
68
|
|
|
65
|
-
pid = Sneakers::
|
|
69
|
+
pid = Sneakers::CONFIG[:pid_path]
|
|
66
70
|
|
|
67
71
|
say SNEAKERS
|
|
68
72
|
say "Workers ....: #{em workers.join(', ')}"
|
|
69
|
-
say "Log ........: #{em (Sneakers::
|
|
73
|
+
say "Log ........: #{em (Sneakers::CONFIG[:log] == STDOUT ? 'Console' : Sneakers::CONFIG[:log]) }"
|
|
70
74
|
say "PID ........: #{em pid}"
|
|
71
75
|
say ""
|
|
72
76
|
say (" "*31)+"Process control"
|
|
@@ -82,7 +86,7 @@ module Sneakers
|
|
|
82
86
|
|
|
83
87
|
if options[:debug]
|
|
84
88
|
say "==== configuration ==="
|
|
85
|
-
say Sneakers::
|
|
89
|
+
say Sneakers::CONFIG.inspect
|
|
86
90
|
say "======================"
|
|
87
91
|
end
|
|
88
92
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Sneakers
|
|
2
|
+
class Configuration
|
|
3
|
+
|
|
4
|
+
extend Forwardable
|
|
5
|
+
def_delegators :@hash, :to_hash, :[], :[]=, :merge!, :==, :fetch, :delete
|
|
6
|
+
|
|
7
|
+
DEFAULTS = {
|
|
8
|
+
# runner
|
|
9
|
+
:runner_config_file => nil,
|
|
10
|
+
:metrics => nil,
|
|
11
|
+
:daemonize => false,
|
|
12
|
+
:start_worker_delay => 0.2,
|
|
13
|
+
:workers => 4,
|
|
14
|
+
:log => STDOUT,
|
|
15
|
+
:pid_path => 'sneakers.pid',
|
|
16
|
+
|
|
17
|
+
# workers
|
|
18
|
+
:timeout_job_after => 5,
|
|
19
|
+
:prefetch => 10,
|
|
20
|
+
:threads => 10,
|
|
21
|
+
:durable => true,
|
|
22
|
+
:ack => true,
|
|
23
|
+
:heartbeat => 2,
|
|
24
|
+
:exchange => 'sneakers',
|
|
25
|
+
:exchange_type => :direct,
|
|
26
|
+
:hooks => {}
|
|
27
|
+
}.freeze
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def initialize
|
|
31
|
+
clear
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def clear
|
|
35
|
+
@hash = DEFAULTS.dup
|
|
36
|
+
@hash[:amqp] = ENV.fetch('RABBITMQ_URL', 'amqp://guest:guest@localhost:5672')
|
|
37
|
+
@hash[:vhost] = AMQ::Settings.parse_amqp_url(@hash[:amqp]).fetch(:vhost, '/')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def merge(hash)
|
|
41
|
+
instance = self.class.new
|
|
42
|
+
instance.merge! to_hash
|
|
43
|
+
instance.merge! hash
|
|
44
|
+
instance
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
require 'base64'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
module Sneakers
|
|
5
|
+
module Handlers
|
|
6
|
+
#
|
|
7
|
+
# Maxretry uses dead letter policies on Rabbitmq to requeue and retry
|
|
8
|
+
# messages after failure (rejections, errors and timeouts). When the maximum
|
|
9
|
+
# number of retries is reached it will put the message on an error queue.
|
|
10
|
+
# This handler will only retry at the queue level. To accomplish that, the
|
|
11
|
+
# setup is a bit complex.
|
|
12
|
+
#
|
|
13
|
+
# Input:
|
|
14
|
+
# worker_exchange (eXchange)
|
|
15
|
+
# worker_queue (Queue)
|
|
16
|
+
# We create:
|
|
17
|
+
# worker_queue-retry - (X) where we setup the worker queue to dead-letter.
|
|
18
|
+
# worker_queue-retry - (Q) queue bound to ^ exchange, dead-letters to
|
|
19
|
+
# worker_queue-retry-requeue.
|
|
20
|
+
# worker_queue-error - (X) where to send max-retry failures
|
|
21
|
+
# worker_queue-error - (Q) bound to worker_queue-error.
|
|
22
|
+
# worker_queue-retry-requeue - (X) exchange to bind worker_queue to for
|
|
23
|
+
# requeuing directly to the worker_queue.
|
|
24
|
+
#
|
|
25
|
+
# This requires that you setup arguments to the worker queue to line up the
|
|
26
|
+
# dead letter queue. See the example for more information.
|
|
27
|
+
#
|
|
28
|
+
# Many of these can be override with options:
|
|
29
|
+
# - retry_exchange - sets retry exchange & queue
|
|
30
|
+
# - retry_error_exchange - sets error exchange and queue
|
|
31
|
+
# - retry_requeue_exchange - sets the exchange created to re-queue things
|
|
32
|
+
# back to the worker queue.
|
|
33
|
+
#
|
|
34
|
+
class Maxretry
|
|
35
|
+
|
|
36
|
+
def initialize(channel, queue, opts)
|
|
37
|
+
@worker_queue_name = queue.name
|
|
38
|
+
Sneakers.logger.debug do
|
|
39
|
+
"#{log_prefix} creating handler, opts=#{opts}"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
@channel = channel
|
|
43
|
+
@opts = opts
|
|
44
|
+
|
|
45
|
+
# Construct names, defaulting where suitable
|
|
46
|
+
retry_name = @opts[:retry_exchange] || "#{@worker_queue_name}-retry"
|
|
47
|
+
error_name = @opts[:retry_error_exchange] || "#{@worker_queue_name}-error"
|
|
48
|
+
requeue_name = @opts[:retry_requeue_exchange] || "#{@worker_queue_name}-retry-requeue"
|
|
49
|
+
|
|
50
|
+
# Create the exchanges
|
|
51
|
+
@retry_exchange, @error_exchange, @requeue_exchange = [retry_name, error_name, requeue_name].map do |name|
|
|
52
|
+
Sneakers.logger.debug { "#{log_prefix} creating exchange=#{name}" }
|
|
53
|
+
@channel.exchange(name,
|
|
54
|
+
:type => 'topic',
|
|
55
|
+
:durable => opts[:durable])
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Create the queues and bindings
|
|
59
|
+
Sneakers.logger.debug do
|
|
60
|
+
"#{log_prefix} creating queue=#{retry_name} x-dead-letter-exchange=#{requeue_name}"
|
|
61
|
+
end
|
|
62
|
+
@retry_queue = @channel.queue(retry_name,
|
|
63
|
+
:durable => opts[:durable],
|
|
64
|
+
:arguments => {
|
|
65
|
+
:'x-dead-letter-exchange' => requeue_name,
|
|
66
|
+
:'x-message-ttl' => @opts[:retry_timeout] || 60000
|
|
67
|
+
})
|
|
68
|
+
@retry_queue.bind(@retry_exchange, :routing_key => '#')
|
|
69
|
+
|
|
70
|
+
Sneakers.logger.debug do
|
|
71
|
+
"#{log_prefix} creating queue=#{error_name}"
|
|
72
|
+
end
|
|
73
|
+
@error_queue = @channel.queue(error_name,
|
|
74
|
+
:durable => opts[:durable])
|
|
75
|
+
@error_queue.bind(@error_exchange, :routing_key => '#')
|
|
76
|
+
|
|
77
|
+
# Finally, bind the worker queue to our requeue exchange
|
|
78
|
+
queue.bind(@requeue_exchange, :routing_key => '#')
|
|
79
|
+
|
|
80
|
+
@max_retries = @opts[:retry_max_times] || 5
|
|
81
|
+
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def acknowledge(hdr, props, msg)
|
|
85
|
+
@channel.acknowledge(hdr.delivery_tag, false)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def reject(hdr, props, msg, requeue = false)
|
|
89
|
+
if requeue
|
|
90
|
+
# This was explicitly rejected specifying it be requeued so we do not
|
|
91
|
+
# want it to pass through our retry logic.
|
|
92
|
+
@channel.reject(hdr.delivery_tag, requeue)
|
|
93
|
+
else
|
|
94
|
+
handle_retry(hdr, props, msg, :reject)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def error(hdr, props, msg, err)
|
|
100
|
+
handle_retry(hdr, props, msg, err)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def timeout(hdr, props, msg)
|
|
104
|
+
handle_retry(hdr, props, msg, :timeout)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def noop(hdr, props, msg)
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Helper logic for retry handling. This will reject the message if there
|
|
112
|
+
# are remaining retries left on it, otherwise it will publish it to the
|
|
113
|
+
# error exchange along with the reason.
|
|
114
|
+
# @param hdr [Bunny::DeliveryInfo]
|
|
115
|
+
# @param props [Bunny::MessageProperties]
|
|
116
|
+
# @param msg [String] The message
|
|
117
|
+
# @param reason [String, Symbol, Exception] Reason for the retry, included
|
|
118
|
+
# in the JSON we put on the error exchange.
|
|
119
|
+
def handle_retry(hdr, props, msg, reason)
|
|
120
|
+
# +1 for the current attempt
|
|
121
|
+
num_attempts = failure_count(props[:headers]) + 1
|
|
122
|
+
if num_attempts <= @max_retries
|
|
123
|
+
# We call reject which will route the message to the
|
|
124
|
+
# x-dead-letter-exchange (ie. retry exchange) on the queue
|
|
125
|
+
Sneakers.logger.info do
|
|
126
|
+
"#{log_prefix} msg=retrying, count=#{num_attempts}, headers=#{props[:headers]}"
|
|
127
|
+
end
|
|
128
|
+
@channel.reject(hdr.delivery_tag, false)
|
|
129
|
+
# TODO: metrics
|
|
130
|
+
else
|
|
131
|
+
# Retried more than the max times
|
|
132
|
+
# Publish the original message with the routing_key to the error exchange
|
|
133
|
+
Sneakers.logger.info do
|
|
134
|
+
"#{log_prefix} msg=failing, retry_count=#{num_attempts}, reason=#{reason}"
|
|
135
|
+
end
|
|
136
|
+
data = {
|
|
137
|
+
error: reason,
|
|
138
|
+
num_attempts: num_attempts,
|
|
139
|
+
failed_at: Time.now.iso8601,
|
|
140
|
+
payload: Base64.encode64(msg.to_s)
|
|
141
|
+
}.tap do |hash|
|
|
142
|
+
if reason.is_a?(Exception)
|
|
143
|
+
hash[:error_class] = reason.class
|
|
144
|
+
if reason.backtrace
|
|
145
|
+
hash[:backtrace] = reason.backtrace.take(10).join(', ')
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end.to_json
|
|
149
|
+
@error_exchange.publish(data, :routing_key => hdr.routing_key)
|
|
150
|
+
@channel.acknowledge(hdr.delivery_tag, false)
|
|
151
|
+
# TODO: metrics
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
private :handle_retry
|
|
155
|
+
|
|
156
|
+
# Uses the x-death header to determine the number of failures this job has
|
|
157
|
+
# seen in the past. This does not count the current failure. So for
|
|
158
|
+
# instance, the first time the job fails, this will return 0, the second
|
|
159
|
+
# time, 1, etc.
|
|
160
|
+
# @param headers [Hash] Hash of headers that Rabbit delivers as part of
|
|
161
|
+
# the message
|
|
162
|
+
# @return [Integer] Count of number of failures.
|
|
163
|
+
def failure_count(headers)
|
|
164
|
+
if headers.nil? || headers['x-death'].nil?
|
|
165
|
+
0
|
|
166
|
+
else
|
|
167
|
+
headers['x-death'].select do |x_death|
|
|
168
|
+
x_death['queue'] == @worker_queue_name
|
|
169
|
+
end.count
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
private :failure_count
|
|
173
|
+
|
|
174
|
+
# Prefix all of our log messages so they are easier to find. We don't have
|
|
175
|
+
# the worker, so the next best thing is the queue name.
|
|
176
|
+
def log_prefix
|
|
177
|
+
"Maxretry handler [queue=#{@worker_queue_name}]"
|
|
178
|
+
end
|
|
179
|
+
private :log_prefix
|
|
180
|
+
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
module Sneakers
|
|
2
2
|
module Handlers
|
|
3
3
|
class Oneshot
|
|
4
|
-
def initialize(channel)
|
|
4
|
+
def initialize(channel, queue, opts)
|
|
5
5
|
@channel = channel
|
|
6
|
+
@opts = opts
|
|
6
7
|
end
|
|
7
8
|
|
|
8
|
-
def acknowledge(
|
|
9
|
-
@channel.acknowledge(
|
|
9
|
+
def acknowledge(hdr, props, msg)
|
|
10
|
+
@channel.acknowledge(hdr.delivery_tag, false)
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
def reject(
|
|
13
|
-
@channel.reject(
|
|
13
|
+
def reject(hdr, props, msg, requeue=false)
|
|
14
|
+
@channel.reject(hdr.delivery_tag, requeue)
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
def error(
|
|
17
|
-
reject(
|
|
17
|
+
def error(hdr, props, msg, err)
|
|
18
|
+
reject(hdr, props, msg)
|
|
18
19
|
end
|
|
19
20
|
|
|
20
|
-
def timeout(
|
|
21
|
-
reject(
|
|
21
|
+
def timeout(hdr, props, msg)
|
|
22
|
+
reject(hdr, props, msg)
|
|
22
23
|
end
|
|
23
24
|
|
|
24
|
-
def noop(
|
|
25
|
+
def noop(hdr, props, msg)
|
|
25
26
|
|
|
26
27
|
end
|
|
27
28
|
end
|