sneakers_exponential_retry 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/README.md +65 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sneakers_exponential_retry.rb +97 -0
- data/lib/sneakers_exponential_retry/version.rb +3 -0
- data/sneakers_exponential_retry.gemspec +23 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: af906d8bafee5024ee77370c828d92a22628ecdc
|
4
|
+
data.tar.gz: 20a9007c85f5317d311f65a9a4f7a4988aebe6ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6df19505aeff4c55d4c13d32686543da6ad73a4ee4b2fe2ac7de38eb44c911c8b2becf996acbc01e31ac6734b3c98a4a43b184bfe1ab2b5d00d020cb7b5a9c0
|
7
|
+
data.tar.gz: f6ec2b59258761e81d0ca4f378762aa6129f278fcde47204ddd0b07c132dc4ae74ac1466730f0bd223dfec7752ae5c691fade4e74444bc9ab7a52c9fe4916037
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at yanghsing.lin@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# SneakersExponentialRetry
|
2
|
+
|
3
|
+
Exponential Retry Handler for [Sneakers](https://github.com/jondot/sneakers) that just works.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sneakers_exponential_retry'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sneakers_exponential_retry
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Configure your Sneakers by the following:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'sneakers_exponential_retry'
|
27
|
+
|
28
|
+
Sneakers.configure :handler => SneakersExponentialRetry,
|
29
|
+
:handler_options => {
|
30
|
+
:max_retry_count => 3,
|
31
|
+
:logger => Sneakers.logger
|
32
|
+
},
|
33
|
+
:exchange => 'MyExchangeName',
|
34
|
+
:exchange_type => :topic
|
35
|
+
```
|
36
|
+
|
37
|
+
## Options:
|
38
|
+
|
39
|
+
- `exchange`: Exchange name to be used, required for `SneakersExponentialRetry`
|
40
|
+
- `handler_options`:
|
41
|
+
- `max_retry_count`: (optional) Max retry count, default to 14
|
42
|
+
- `logger`: (optional) logger instance, default to nil, which would not log anything related to retrying.
|
43
|
+
|
44
|
+
## How it works:
|
45
|
+
|
46
|
+
TODO
|
47
|
+
|
48
|
+
## Testing:
|
49
|
+
|
50
|
+
Run tests by:
|
51
|
+
|
52
|
+
```
|
53
|
+
$ bundle exec rspec
|
54
|
+
```
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mz026/sneakers_exponential_retry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
65
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sneakers_exponential_retry"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "sneakers_exponential_retry/version"
|
2
|
+
|
3
|
+
class ExponentialRetry
|
4
|
+
MINUTE = 60 * 1000
|
5
|
+
DEFAULT_MAX_RETRY_COUNT = 14
|
6
|
+
|
7
|
+
def initialize(channel, queue, opts)
|
8
|
+
@channel = channel
|
9
|
+
@max_retry_count = calculate_max_retry_count(opts)
|
10
|
+
@retry_exchange = create_retry_exchange(queue.name)
|
11
|
+
@logger = opts[:handler_options] ? opts[:handler_options][:logger] : nil
|
12
|
+
|
13
|
+
create_retry_queue(queue.name, opts[:exchange])
|
14
|
+
.bind(@retry_exchange, :routing_key => '#')
|
15
|
+
end
|
16
|
+
|
17
|
+
def calculate_max_retry_count opts
|
18
|
+
handler_options = opts[:handler_options] || {}
|
19
|
+
( handler_options[:max_retry_count] || DEFAULT_MAX_RETRY_COUNT ).to_i
|
20
|
+
end
|
21
|
+
private :calculate_max_retry_count
|
22
|
+
|
23
|
+
def create_retry_exchange queue_name
|
24
|
+
@channel.exchange("#{queue_name}-retry-ex",
|
25
|
+
:type => 'topic',
|
26
|
+
:durable => true)
|
27
|
+
end
|
28
|
+
private :create_retry_exchange
|
29
|
+
|
30
|
+
def create_retry_queue queue_name, exchange_name
|
31
|
+
@channel.queue(
|
32
|
+
"#{queue_name}-retry-queue",
|
33
|
+
durable: true,
|
34
|
+
arguments: {
|
35
|
+
:'x-dead-letter-exchange' => exchange_name
|
36
|
+
}
|
37
|
+
)
|
38
|
+
end
|
39
|
+
private :create_retry_queue
|
40
|
+
|
41
|
+
def acknowledge(hdr, props, msg)
|
42
|
+
@channel.acknowledge(hdr.delivery_tag, false)
|
43
|
+
end
|
44
|
+
|
45
|
+
def reject(hdr, props, msg, requeue=false)
|
46
|
+
@channel.reject(hdr.delivery_tag, requeue)
|
47
|
+
end
|
48
|
+
|
49
|
+
def error(hdr, props, msg, err)
|
50
|
+
handle_failing_message(hdr, props, msg, err)
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_failing_message hdr, props, msg, error
|
54
|
+
retry_count = get_retry_count(props[:headers])
|
55
|
+
if retry_count >= @max_retry_count
|
56
|
+
reject(hdr, props, msg)
|
57
|
+
return
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
@retry_exchange.publish(msg,
|
62
|
+
:headers => {
|
63
|
+
'retry-count' => retry_count + 1
|
64
|
+
},
|
65
|
+
:routing_key => hdr.routing_key,
|
66
|
+
:expiration => expiration_time(retry_count))
|
67
|
+
log_retry(retry_count)
|
68
|
+
acknowledge(hdr, props, msg)
|
69
|
+
end
|
70
|
+
private :handle_failing_message
|
71
|
+
|
72
|
+
def log_retry count
|
73
|
+
return unless @logger
|
74
|
+
@logger.info do
|
75
|
+
"retry_count: #{count + 1}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
private :log_retry
|
79
|
+
|
80
|
+
def get_retry_count headers
|
81
|
+
return 0 unless headers
|
82
|
+
headers['retry-count'].to_i
|
83
|
+
end
|
84
|
+
private :get_retry_count
|
85
|
+
|
86
|
+
def expiration_time retry_count
|
87
|
+
(2 ** retry_count) * MINUTE
|
88
|
+
end
|
89
|
+
private :expiration_time
|
90
|
+
|
91
|
+
def timeout(hdr, props, msg)
|
92
|
+
reject(hdr, props, msg)
|
93
|
+
end
|
94
|
+
|
95
|
+
def noop(hdr, props, msg)
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sneakers_exponential_retry/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sneakers_exponential_retry"
|
8
|
+
spec.version = SneakersExponentialRetry::VERSION
|
9
|
+
spec.authors = ["Yang-Hsing Lin"]
|
10
|
+
spec.email = ["yanghsing.lin@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Exponential Retry Handler for Sneakers that just works.}
|
13
|
+
spec.homepage = "https://github.com/mz026/sneakers_exponential_retry"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sneakers_exponential_retry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yang-Hsing Lin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- yanghsing.lin@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/sneakers_exponential_retry.rb
|
72
|
+
- lib/sneakers_exponential_retry/version.rb
|
73
|
+
- sneakers_exponential_retry.gemspec
|
74
|
+
homepage: https://github.com/mz026/sneakers_exponential_retry
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.6.4
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Exponential Retry Handler for Sneakers that just works.
|
97
|
+
test_files: []
|
98
|
+
has_rdoc:
|