kafka-retryable 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 +12 -0
- data/.rspec +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/errors.yml +8 -0
- data/kafka-retryable.gemspec +40 -0
- data/lib/kafka/retryable.rb +22 -0
- data/lib/kafka/retryable/config.rb +33 -0
- data/lib/kafka/retryable/errors/invalid_handling_options.rb +11 -0
- data/lib/kafka/retryable/errors/invalid_policy.rb +7 -0
- data/lib/kafka/retryable/handle_failure.rb +109 -0
- data/lib/kafka/retryable/message_dispatcher/kafka.rb +17 -0
- data/lib/kafka/retryable/policies/kafka_handler.rb +18 -0
- data/lib/kafka/retryable/policy_finder.rb +20 -0
- data/lib/kafka/retryable/schemas/failure_handler_options.rb +42 -0
- data/lib/kafka/retryable/version.rb +8 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bd674f1b3d3ff8a567ae3f9d383b417cbb3b7351
|
4
|
+
data.tar.gz: 566f57dc39ac0512d3c5f377687f5858b8f9febc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2888a1f9de6321b137734c282e1d8e8b157fabb21386863bedd598297d0688a09e6e4e1904461d015195a80a4c6749eadadbdcf2439197de38b8da68f854fffe
|
7
|
+
data.tar.gz: b630ddcdff1afb647ac3557048905ed1a62fa14f1895dff1e5e2ac3a48c7c13ab3c8f590b2c42bb56806cc6dfd88732646be89f2ea2017a8226040bc796feea9
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ankitagupta12391@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ankita Gupta
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Kafka::Retryable
|
2
|
+
|
3
|
+
Consumer failures are common for Kafka consumers, and if these are left unhandled, it can lead to data losses. There are many ways to buffer and retry messages that encounter failures. The aim of `kafka-retryable` is to provide multiple types of failure handling policies that can be used depending on the need.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'kafka-retryable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install kafka-retryable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`kafka_retryable` allows failed messages to be buffered into a `dead_letter_queue` inside Kafka. In the future, it will provision for retry mechanisms and the ability to buffer in other messaging systems.
|
24
|
+
|
25
|
+
|
26
|
+
1. Setup the gem by setting configuration parameters. In a rails project, this can be done inside `config/initializers/kafka_retryable.rb` for instance:
|
27
|
+
|
28
|
+
```
|
29
|
+
Kafka::Retryable.setup do |config|
|
30
|
+
config.failure_handling.enabled = true
|
31
|
+
config.buffer.kafka.seed_brokers = ['kafka://localhost:9092']
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
These are the available configurations:
|
36
|
+
|
37
|
+
| Option | Value type | Description | Default |
|
38
|
+
|-------------------------------|---------------|----------------------------------|---------|
|
39
|
+
| failure_handling.enabled | Boolean | Set if buffering failed messages to a topic in Kafka is enabled | true |
|
40
|
+
| buffer.kafka.seedbrokers | Array | Kafka broker URL. Example: kafka://127.0.0.1:9092 or kafka+ssl://127.0.0.1:909 | nil |
|
41
|
+
|
42
|
+
|
43
|
+
2. Start with declaring configuration parameters for your class using the following syntax:
|
44
|
+
|
45
|
+
```
|
46
|
+
Class KafkaConsumer
|
47
|
+
include Kafka::Retryable::HandleFailure
|
48
|
+
|
49
|
+
failure_handler buffer: :kafka,
|
50
|
+
dead_letter_queue: :topic_t1,
|
51
|
+
exception_blacklist: [Karafka::InvalidMessageError],
|
52
|
+
after_failure: ->(error, message) { Bugsnag.notify("#{error}-#{message}") }
|
53
|
+
|
54
|
+
def consume
|
55
|
+
# Message consumption logic goes here
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
- `dead_letter_queue`: Topic where the consumer should enqueue the failure message.
|
61
|
+
- `exception_blacklist`: List of exception classes for which the error handling logic does not apply
|
62
|
+
- `exception_whitelist`: List of exception classes for which the error handling logic should apply
|
63
|
+
- `after_failure`: Accepts a proc that is executed after failure handling is completed
|
64
|
+
|
65
|
+
If `exception_blacklist` and `exception_whitelist` are both missing, then the error handling logic will apply for all exception classes.
|
66
|
+
|
67
|
+
3. Enclose message processing logic inside `handle_failure(message)` helper method provided by `kafka-retryable`
|
68
|
+
|
69
|
+
```
|
70
|
+
handle_failure(message) do
|
71
|
+
MessageProcessor.new(message).perform
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
Overall, this is how Kafka Consumers using `kafka-retryable` will look like:
|
76
|
+
|
77
|
+
```
|
78
|
+
Class KafkaConsumer
|
79
|
+
include Kafka::Retryable::HandleFailure
|
80
|
+
|
81
|
+
failure_handler buffer: :kafka,
|
82
|
+
dead_letter_queue: :topic_t1,
|
83
|
+
exception_blacklist: [Karafka::InvalidMessageError],
|
84
|
+
after_failure: ->(error, message) { Bugsnag.notify("#{error}-#{message}") }
|
85
|
+
|
86
|
+
def consume
|
87
|
+
handle_failure(message)
|
88
|
+
# Message consumption logic goes here
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
`kafka-retryable` will make sure any exceptions outside the `exception_blacklist` will cause the message to be enqueued into the `dead_letter_queue` specified in the `failure_handler` configuration.
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
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).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kafka-retryable. 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.
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
109
|
+
|
110
|
+
## Code of Conduct
|
111
|
+
|
112
|
+
Everyone interacting in the Kafka::Retryable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/kafka-retryable/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "kafka/retryable"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/config/errors.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# coding: utf-8
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'kafka/retryable/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'kafka-retryable'
|
9
|
+
spec.version = Kafka::Retryable::VERSION
|
10
|
+
spec.authors = ['Ankita Gupta']
|
11
|
+
spec.email = ['ankita@indydevs.org']
|
12
|
+
|
13
|
+
spec.summary = 'Allows defining failure handling strategy for Kafka consumers'
|
14
|
+
spec.description = 'Allws specifying different failure handling in case of consumer failures'
|
15
|
+
spec.homepage = 'https://github.com/ankitagupta12/kafka-retryable'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
"public gem pushes."
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(spec)/})
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_dependency 'dry-configurable', '~> 0.7'
|
35
|
+
spec.add_dependency 'dry-validation', '~> 0.11'
|
36
|
+
spec.add_dependency 'pheromone', '~> 0.5'
|
37
|
+
|
38
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
39
|
+
spec.add_development_dependency 'rspec-rails', '~> 3.5'
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'kafka/retryable/handle_failure'
|
2
|
+
require 'kafka/retryable/config'
|
3
|
+
require 'waterdrop'
|
4
|
+
module Kafka
|
5
|
+
module Retryable
|
6
|
+
class << self
|
7
|
+
# return config object
|
8
|
+
def config
|
9
|
+
Config.config
|
10
|
+
end
|
11
|
+
|
12
|
+
# Provides a block to override default config
|
13
|
+
def setup(&block)
|
14
|
+
Config.setup(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def enabled?
|
18
|
+
config.failure_handling.enabled
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'dry-configurable'
|
3
|
+
module Kafka::Retryable
|
4
|
+
# configurator for setting up all the configurable settings for pheromone
|
5
|
+
class Config
|
6
|
+
extend Dry::Configurable
|
7
|
+
# specify message queue settings
|
8
|
+
setting :buffer do
|
9
|
+
setting :kafka do
|
10
|
+
setting :seed_brokers
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
setting :failure_handling do
|
15
|
+
# specify if failure handling is enabled
|
16
|
+
setting :enabled, true
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def setup(&block)
|
21
|
+
configure(&block)
|
22
|
+
setup_waterdrop if config.buffer.kafka.seed_brokers
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_waterdrop
|
26
|
+
WaterDrop.setup do |waterdrop_config|
|
27
|
+
waterdrop_config.deliver = config.failure_handling.enabled
|
28
|
+
waterdrop_config.kafka.seed_brokers = config.buffer.kafka.seed_brokers
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Kafka
|
3
|
+
module Retryable
|
4
|
+
# Error class for invalid failure handling configuration
|
5
|
+
class InvalidHandlingOptions < StandardError
|
6
|
+
def initialize(msg = 'Invalid options', validation_errors: {})
|
7
|
+
super("#{msg}: #{validation_errors}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'kafka/retryable'
|
2
|
+
require 'kafka/retryable/schemas/failure_handler_options'
|
3
|
+
require 'kafka/retryable/errors/invalid_handling_options'
|
4
|
+
require 'kafka/retryable/policy_finder'
|
5
|
+
module Kafka
|
6
|
+
module Retryable
|
7
|
+
module HandleFailure
|
8
|
+
module ClassMethods
|
9
|
+
# Define failure handler configuration here
|
10
|
+
# failure_handler buffer: :kafka,
|
11
|
+
# dead_letter_queue: :topic_t1,
|
12
|
+
# exception_blacklist: [Karafka::InvalidMessageError],
|
13
|
+
# after_failure: ->(error, _) { Bugsnag.notify(error) }
|
14
|
+
#
|
15
|
+
# Only buffer and dead_letter_queue are required arguments
|
16
|
+
def failure_handler(failure_handling_options)
|
17
|
+
validate_options(failure_handling_options)
|
18
|
+
@@failure_configuration ||= {}
|
19
|
+
@@failure_configuration[to_s] = failure_handling_options
|
20
|
+
end
|
21
|
+
|
22
|
+
# @@failure_configuration is stored in a hash when the class is first loaded
|
23
|
+
def failure_configuration
|
24
|
+
@@failure_configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
# Validate if the configuration passed is a valid one
|
28
|
+
# If the configuration is invalid, raise exception
|
29
|
+
def validate_options(failure_handling_options)
|
30
|
+
options_validator = Kafka::Retryable::Schemas::FailureHandlerOptions.call(
|
31
|
+
failure_handling_options
|
32
|
+
)
|
33
|
+
return if options_validator.success?
|
34
|
+
raise Kafka::Retryable::InvalidHandlingOptions.new(
|
35
|
+
validation_errors: options_validator.errors
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module InstanceMethods
|
41
|
+
# The message consumption logic can be passed in to this method as a block
|
42
|
+
# If any exceptions occur within the message consumption logic, then this
|
43
|
+
# block will be responsible for error handling, buffering, and retry logic
|
44
|
+
|
45
|
+
# @param message [Hash|String] this should be the exact message the needs to be
|
46
|
+
# sent to Kafka. kafka_retryable does not perform any modifications on this
|
47
|
+
# message and sends it as it is to Kafka
|
48
|
+
def handle_failure(message)
|
49
|
+
yield
|
50
|
+
rescue => error
|
51
|
+
run_failure_hooks(error, message)
|
52
|
+
run_after_failure_action(error, message)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# Find the failure policy based on the buffer, and execute the logic within the policy
|
58
|
+
# @param error [Class]
|
59
|
+
# @param message [Hash|String]
|
60
|
+
def run_failure_hooks(error, message)
|
61
|
+
return if error_in_blacklist?(error, configuration[:exception_blacklist])
|
62
|
+
return unless error_in_whitelist?(error, configuration[:exception_whitelist])
|
63
|
+
|
64
|
+
policy = PolicyFinder.find_by(configuration[:buffer])
|
65
|
+
policy.new(configuration[:dead_letter_queue], message).perform_failure_recovery
|
66
|
+
end
|
67
|
+
|
68
|
+
# Run any post-processing logic here, e.g. notifying Bugsnag
|
69
|
+
# @param error [Class]
|
70
|
+
# @param message [Hash|String]
|
71
|
+
def run_after_failure_action(error, message)
|
72
|
+
configuration[:after_failure].call(error, message)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Fetch configuration for the class
|
76
|
+
def configuration
|
77
|
+
@configuration ||= failure_configuration[self.class.name]
|
78
|
+
end
|
79
|
+
|
80
|
+
# @@failure_configuration is stored in a hash when the class is first loaded
|
81
|
+
def failure_configuration
|
82
|
+
self.class.failure_configuration
|
83
|
+
end
|
84
|
+
|
85
|
+
# This is false by default for all error classes
|
86
|
+
# It means that all error classes will trigger failure handling unless they are
|
87
|
+
# specified in the blacklist
|
88
|
+
def error_in_blacklist?(error, exception_blacklist)
|
89
|
+
return false if (exception_blacklist || []).empty?
|
90
|
+
exception_blacklist.include?(error)
|
91
|
+
end
|
92
|
+
|
93
|
+
# This is true by default for all error classes
|
94
|
+
# It means that all error classes will trigger failure handling by default
|
95
|
+
# If this list exists then only the errors in this list will trigger failure handling
|
96
|
+
def error_in_whitelist?(error, exception_whitelist)
|
97
|
+
return true if (exception_whitelist || []).empty?
|
98
|
+
!exception_whitelist.include?(error)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.included(base)
|
103
|
+
base.send :extend, ClassMethods
|
104
|
+
base.send :include, InstanceMethods
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Kafka
|
2
|
+
module Retryable::MessageDispatcher
|
3
|
+
class Kafka
|
4
|
+
def initialize(topic, message)
|
5
|
+
@topic = topic
|
6
|
+
@message = message
|
7
|
+
end
|
8
|
+
|
9
|
+
def perform
|
10
|
+
WaterDrop::SyncProducer.call(
|
11
|
+
@message,
|
12
|
+
{ topic: @topic.to_s }
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'kafka/retryable/message_dispatcher/kafka'
|
3
|
+
module Kafka
|
4
|
+
module Retryable::Policies
|
5
|
+
# Policy for storing failed messages in a queue on Kafka
|
6
|
+
class KafkaHandler
|
7
|
+
def initialize(message, topic)
|
8
|
+
@message = message
|
9
|
+
@topic = topic
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform_failure_recovery
|
13
|
+
return unless Kafka::Retryable.enabled?
|
14
|
+
Kafka::Retryable::MessageDispatcher::Kafka.new(@message, @topic).perform
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'kafka/retryable/policies/kafka_handler'
|
3
|
+
require 'kafka/retryable/errors/invalid_policy'
|
4
|
+
|
5
|
+
module Kafka
|
6
|
+
module Retryable
|
7
|
+
# Find policy based on the failure handler settings
|
8
|
+
class PolicyFinder
|
9
|
+
POLICY_MAPPING = {
|
10
|
+
kafka: Kafka::Retryable::Policies::KafkaHandler
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
def self.find_by(buffer)
|
14
|
+
POLICY_MAPPING[buffer].tap do |policy|
|
15
|
+
raise Kafka::Retryable::InvalidPolicy.new('Policy not found') unless policy
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Kafka::Retryable
|
3
|
+
module Schemas
|
4
|
+
# Schema for validating failure handling options
|
5
|
+
FailureHandlerOptions = Dry::Validation.Schema do
|
6
|
+
VALID_BUFFERS = %i(kafka).freeze
|
7
|
+
TOPIC_REGEXP = /\A(\w|\-|\.)+\z/
|
8
|
+
|
9
|
+
configure do
|
10
|
+
config.messages_file = File.join(
|
11
|
+
Pathname.new(File.expand_path('../../../../../', __FILE__)), 'config', 'errors.yml'
|
12
|
+
)
|
13
|
+
|
14
|
+
# Validator to check the buffer name
|
15
|
+
def valid_buffer?(buffer)
|
16
|
+
VALID_BUFFERS.include?(buffer)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Validate if the option is a string
|
20
|
+
def sym?(option)
|
21
|
+
option.is_a? Symbol
|
22
|
+
end
|
23
|
+
|
24
|
+
# Check if the option passed in is a valid class
|
25
|
+
def klass?(option)
|
26
|
+
Object.const_defined?(option.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Check if the option passed in is a valid proc
|
30
|
+
def is_a_proc?(option)
|
31
|
+
option.respond_to?(:call)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
required(:buffer).filled { sym? & valid_buffer? }
|
36
|
+
optional(:exception_whitelist).filled { each(:klass?) }
|
37
|
+
optional(:exception_blacklist).filled { each(:klass?) }
|
38
|
+
required(:dead_letter_queue).filled(:str?, format?: TOPIC_REGEXP)
|
39
|
+
optional(:after_failure).maybe { is_a_proc? }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kafka-retryable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ankita Gupta
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-validation
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.11'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pheromone
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.15'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.15'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.5'
|
83
|
+
description: Allws specifying different failure handling in case of consumer failures
|
84
|
+
email:
|
85
|
+
- ankita@indydevs.org
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- config/errors.yml
|
100
|
+
- kafka-retryable.gemspec
|
101
|
+
- lib/kafka/retryable.rb
|
102
|
+
- lib/kafka/retryable/config.rb
|
103
|
+
- lib/kafka/retryable/errors/invalid_handling_options.rb
|
104
|
+
- lib/kafka/retryable/errors/invalid_policy.rb
|
105
|
+
- lib/kafka/retryable/handle_failure.rb
|
106
|
+
- lib/kafka/retryable/message_dispatcher/kafka.rb
|
107
|
+
- lib/kafka/retryable/policies/kafka_handler.rb
|
108
|
+
- lib/kafka/retryable/policy_finder.rb
|
109
|
+
- lib/kafka/retryable/schemas/failure_handler_options.rb
|
110
|
+
- lib/kafka/retryable/version.rb
|
111
|
+
homepage: https://github.com/ankitagupta12/kafka-retryable
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata:
|
115
|
+
allowed_push_host: https://rubygems.org
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.6.7
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: Allows defining failure handling strategy for Kafka consumers
|
136
|
+
test_files: []
|