racecar 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/LICENSE.txt +176 -21
- data/README.md +27 -6
- data/examples/cat_consumer.rb +7 -0
- data/exe/racecar +60 -0
- data/lib/racecar.rb +20 -2
- data/lib/racecar/config.rb +117 -0
- data/lib/racecar/consumer.rb +21 -0
- data/lib/racecar/runner.rb +71 -0
- data/lib/racecar/version.rb +1 -1
- data/racecar.gemspec +1 -1
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f99bf49d325678924cc1650412797964ca9fb6c7
|
4
|
+
data.tar.gz: 3411bf0ee53db4ef17d7254d57b3a9c0782bc7aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8fb917397c7f8c812ec569af3e3796254a1ac6450598538c44a88dae60c3701fc2f2cc34de52e069bdba6efad30cfcf42c56d20743fef6d8cd499254d489d47
|
7
|
+
data.tar.gz: 9d5e71b4fd8f4777c6a7f7928dae2bfb87410093f247cb6c4b608c9673ae452db56fe7b668c7d2377a8a6e4fb950bcf07bd934bcaebc708034ea2812e959bde9
|
data/.rspec
CHANGED
data/LICENSE.txt
CHANGED
@@ -1,21 +1,176 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Racecar
|
2
2
|
|
3
|
-
|
3
|
+
Introducing Racecar, your friendly and easy-to-approach Kafka consumer framework!
|
4
4
|
|
5
|
-
|
5
|
+
Using [ruby-kafka](https://github.com/zendesk/ruby-kafka) directly can be a challenge: it's a flexible library with lots of knobs and options. Most users don't need that level of flexibility, though.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,24 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
Add a file in e.g. `app/consumers/user_ban_consumer.rb`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class UserBanConsumer < Racecar::Consumer
|
29
|
+
subscribes_to "user_banned"
|
30
|
+
|
31
|
+
def process(message)
|
32
|
+
data = JSON.parse(message.value)
|
33
|
+
user = User.find(data["user_id"])
|
34
|
+
user.banned = true
|
35
|
+
user.save!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Now run your consumer with `bundle exec racecar UserBanConsumer`.
|
41
|
+
|
42
|
+
That's all there is to it.
|
26
43
|
|
27
44
|
## Development
|
28
45
|
|
@@ -32,10 +49,14 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
49
|
|
33
50
|
## Contributing
|
34
51
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zendesk/racecar.
|
53
|
+
|
54
|
+
## Copyright and license
|
36
55
|
|
56
|
+
Copyright 2015 Zendesk
|
37
57
|
|
38
|
-
|
58
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
39
59
|
|
40
|
-
|
60
|
+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
41
61
|
|
62
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
data/exe/racecar
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "racecar"
|
5
|
+
|
6
|
+
parser = OptionParser.new do |opts|
|
7
|
+
opts.banner = "Usage: racecar MyConsumer [options]"
|
8
|
+
|
9
|
+
opts.on("-r", "--require LIBRARY", "Require the LIBRARY before starting the consumer") do |lib|
|
10
|
+
require lib
|
11
|
+
end
|
12
|
+
|
13
|
+
opts.on_tail("--version", "Show Racecar version") do
|
14
|
+
require "racecar/version"
|
15
|
+
puts "Racecar #{Racecar::VERSION}"
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
parser.parse!(ARGV)
|
21
|
+
|
22
|
+
consumer_name = ARGV.first or raise "No consumer specified"
|
23
|
+
config_file = "config/racecar.yml"
|
24
|
+
|
25
|
+
puts "=> Starting Racecar consumer #{consumer_name}..."
|
26
|
+
|
27
|
+
if defined?(Rails)
|
28
|
+
puts "=> Booting Rails application..."
|
29
|
+
|
30
|
+
require_relative "../config/environment"
|
31
|
+
|
32
|
+
Racecar.config.load_file(config_file, Rails.env)
|
33
|
+
|
34
|
+
if Racecar.config.log_to_stdout
|
35
|
+
# Write to STDOUT as well as to the log file.
|
36
|
+
console = ActiveSupport::Logger.new($stdout)
|
37
|
+
console.formatter = Rails.logger.formatter
|
38
|
+
console.level = Rails.logger.level
|
39
|
+
Rails.logger.extend(ActiveSupport::Logger.broadcast(console))
|
40
|
+
end
|
41
|
+
|
42
|
+
Racecar.logger = Rails.logger
|
43
|
+
end
|
44
|
+
|
45
|
+
# Find the consumer class by name.
|
46
|
+
consumer_class = Kernel.const_get(consumer_name)
|
47
|
+
|
48
|
+
# Load config defined by the consumer class itself.
|
49
|
+
Racecar.config.load_consumer_class(consumer_class)
|
50
|
+
|
51
|
+
Racecar.config.validate!
|
52
|
+
|
53
|
+
puts "=> Wrooooom!"
|
54
|
+
puts "=> Ctrl-C to shutdown consumer"
|
55
|
+
|
56
|
+
processor = consumer_class.new
|
57
|
+
|
58
|
+
Racecar.run(processor)
|
59
|
+
|
60
|
+
puts "=> Shut down"
|
data/lib/racecar.rb
CHANGED
@@ -1,5 +1,23 @@
|
|
1
|
-
require "
|
1
|
+
require "logger"
|
2
|
+
|
3
|
+
require "racecar/consumer"
|
4
|
+
require "racecar/runner"
|
5
|
+
require "racecar/config"
|
2
6
|
|
3
7
|
module Racecar
|
4
|
-
|
8
|
+
def self.config
|
9
|
+
@config ||= Config.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.logger
|
13
|
+
@logger ||= Logger.new(STDOUT)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.logger=(logger)
|
17
|
+
@logger = logger
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.run(processor)
|
21
|
+
Runner.new(processor, config: config, logger: logger).run
|
22
|
+
end
|
5
23
|
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module Racecar
|
5
|
+
class Config
|
6
|
+
ALLOWED_KEYS = %w(
|
7
|
+
brokers
|
8
|
+
client_id
|
9
|
+
offset_commit_interval
|
10
|
+
offset_commit_threshold
|
11
|
+
heartbeat_interval
|
12
|
+
pause_timeout
|
13
|
+
connect_timeout
|
14
|
+
socket_timeout
|
15
|
+
group_id_prefix
|
16
|
+
group_id
|
17
|
+
subscriptions
|
18
|
+
error_handler
|
19
|
+
max_wait_time
|
20
|
+
log_to_stdout
|
21
|
+
)
|
22
|
+
|
23
|
+
REQUIRED_KEYS = %w(
|
24
|
+
brokers
|
25
|
+
)
|
26
|
+
|
27
|
+
DEFAULT_CONFIG = {
|
28
|
+
client_id: "racecar",
|
29
|
+
group_id_prefix: nil,
|
30
|
+
|
31
|
+
subscriptions: [],
|
32
|
+
|
33
|
+
# Default is to commit offsets every 10 seconds.
|
34
|
+
offset_commit_interval: 10,
|
35
|
+
|
36
|
+
# Default is no buffer threshold trigger.
|
37
|
+
offset_commit_threshold: 0,
|
38
|
+
|
39
|
+
# Default is to send a heartbeat every 10 seconds.
|
40
|
+
heartbeat_interval: 10,
|
41
|
+
|
42
|
+
# Default is to not pause partitions on processing errors.
|
43
|
+
pause_timeout: 0,
|
44
|
+
|
45
|
+
connect_timeout: nil,
|
46
|
+
socket_timeout: nil,
|
47
|
+
error_handler: proc {},
|
48
|
+
max_wait_time: 5,
|
49
|
+
log_to_stdout: false,
|
50
|
+
}
|
51
|
+
|
52
|
+
attr_accessor(*ALLOWED_KEYS)
|
53
|
+
|
54
|
+
def initialize
|
55
|
+
load(DEFAULT_CONFIG)
|
56
|
+
load_env!
|
57
|
+
end
|
58
|
+
|
59
|
+
def validate!
|
60
|
+
REQUIRED_KEYS.each do |key|
|
61
|
+
if send(key).nil?
|
62
|
+
raise "required configuration key `#{key}` not defined"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def load_file(path, environment)
|
68
|
+
# First, load the ERB template from disk.
|
69
|
+
template = ERB.new(File.new(path).read)
|
70
|
+
|
71
|
+
# The last argument to `safe_load` allows us to use aliasing to share
|
72
|
+
# configuration between environments.
|
73
|
+
processed = YAML.safe_load(template.result(binding), [], [], true)
|
74
|
+
|
75
|
+
data = processed.fetch(environment)
|
76
|
+
|
77
|
+
load(data)
|
78
|
+
end
|
79
|
+
|
80
|
+
def load(data)
|
81
|
+
data.each do |key, value|
|
82
|
+
unless ALLOWED_KEYS.include?(key.to_s)
|
83
|
+
raise "unknown configuration key `#{key}`"
|
84
|
+
end
|
85
|
+
|
86
|
+
instance_variable_set("@#{key}", value)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def load_consumer_class(consumer_class)
|
91
|
+
@group_id ||= consumer_class.group_id
|
92
|
+
|
93
|
+
@group_id ||= [
|
94
|
+
# Configurable and optional prefix:
|
95
|
+
group_id_prefix,
|
96
|
+
|
97
|
+
# MyFunnyConsumer => my-funny-consumer
|
98
|
+
consumer_class.name.gsub(/[a-z][A-Z]/) {|str| str[0] << "-" << str[1] }.downcase,
|
99
|
+
].compact.join("")
|
100
|
+
|
101
|
+
@subscriptions = consumer_class.subscriptions
|
102
|
+
@max_wait_time = consumer_class.max_wait_time
|
103
|
+
end
|
104
|
+
|
105
|
+
def on_error(&handler)
|
106
|
+
@error_handler = handler
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def load_env!
|
112
|
+
if ENV.key?("RACECAR_BROKERS")
|
113
|
+
@brokers = ENV["RACECAR_BROKERS"].split(",")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Racecar
|
2
|
+
class Consumer
|
3
|
+
Subscription = Struct.new(:topic, :start_from_beginning)
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :max_wait_time
|
7
|
+
attr_accessor :group_id
|
8
|
+
|
9
|
+
def subscriptions
|
10
|
+
@subscriptions ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
# Adds one or more topic subscriptions.
|
14
|
+
def subscribes_to(*topics, start_from_beginning: true)
|
15
|
+
topics.each do |topic|
|
16
|
+
subscriptions << Subscription.new(topic, start_from_beginning)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "kafka"
|
2
|
+
|
3
|
+
module Racecar
|
4
|
+
class Runner
|
5
|
+
attr_reader :processor, :config, :logger
|
6
|
+
|
7
|
+
def initialize(processor, config:, logger:)
|
8
|
+
@processor, @config, @logger = processor, config, logger
|
9
|
+
end
|
10
|
+
|
11
|
+
def run
|
12
|
+
kafka = Kafka.new(
|
13
|
+
client_id: config.client_id,
|
14
|
+
seed_brokers: config.brokers,
|
15
|
+
logger: logger,
|
16
|
+
connect_timeout: config.connect_timeout,
|
17
|
+
socket_timeout: config.socket_timeout,
|
18
|
+
)
|
19
|
+
|
20
|
+
consumer = kafka.consumer(
|
21
|
+
group_id: config.group_id,
|
22
|
+
offset_commit_interval: config.offset_commit_interval,
|
23
|
+
offset_commit_threshold: config.offset_commit_threshold,
|
24
|
+
heartbeat_interval: config.heartbeat_interval,
|
25
|
+
)
|
26
|
+
|
27
|
+
# Stop the consumer on SIGINT and SIGQUIT.
|
28
|
+
trap("QUIT") { consumer.stop }
|
29
|
+
trap("INT") { consumer.stop }
|
30
|
+
|
31
|
+
config.subscriptions.each do |subscription|
|
32
|
+
topic = subscription.topic
|
33
|
+
start_from_beginning = subscription.start_from_beginning
|
34
|
+
|
35
|
+
consumer.subscribe(topic, start_from_beginning: start_from_beginning)
|
36
|
+
end
|
37
|
+
|
38
|
+
begin
|
39
|
+
consumer.each_message(max_wait_time: config.max_wait_time) do |message|
|
40
|
+
processor.process(message)
|
41
|
+
end
|
42
|
+
rescue Kafka::ProcessingError => e
|
43
|
+
@logger.error "Error processing partition #{e.topic}/#{e.partition} at offset #{e.offset}"
|
44
|
+
|
45
|
+
if config.pause_timeout > 0
|
46
|
+
# Pause fetches from the partition. We'll continue processing the other partitions in the topic.
|
47
|
+
# The partition is automatically resumed after the specified timeout, and will continue where we
|
48
|
+
# left off.
|
49
|
+
@logger.warn "Pausing partition #{e.topic}/#{e.partition} for #{config.pause_timeout} seconds"
|
50
|
+
consumer.pause(e.topic, e.partition, timeout: config.pause_timeout)
|
51
|
+
end
|
52
|
+
|
53
|
+
config.error_handler.call(e.cause, {
|
54
|
+
topic: e.topic,
|
55
|
+
partition: e.partition,
|
56
|
+
offset: e.offset,
|
57
|
+
})
|
58
|
+
|
59
|
+
# Restart the consumer loop.
|
60
|
+
retry
|
61
|
+
rescue Kafka::Error => e
|
62
|
+
error = "#{e.class}: #{e.message}\n" + e.backtrace.join("\n")
|
63
|
+
@logger.error "Consumer thread crashed: #{error}"
|
64
|
+
|
65
|
+
config.error_handler.call(e)
|
66
|
+
end
|
67
|
+
|
68
|
+
@logger.info "Gracefully shutting down"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/racecar/version.rb
CHANGED
data/racecar.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = "A framework for running Kafka consumers"
|
13
13
|
spec.homepage = "https://github.com/zendesk/racecar"
|
14
|
-
spec.license = "
|
14
|
+
spec.license = "Apache License Version 2.0"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
17
|
f.match(%r{^(test|spec|features)/})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: racecar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schierbeck
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -57,7 +57,8 @@ description:
|
|
57
57
|
email:
|
58
58
|
- dschierbeck@zendesk.com
|
59
59
|
- bquorning@zendesk.com
|
60
|
-
executables:
|
60
|
+
executables:
|
61
|
+
- racecar
|
61
62
|
extensions: []
|
62
63
|
extra_rdoc_files: []
|
63
64
|
files:
|
@@ -70,12 +71,17 @@ files:
|
|
70
71
|
- Rakefile
|
71
72
|
- bin/console
|
72
73
|
- bin/setup
|
74
|
+
- examples/cat_consumer.rb
|
75
|
+
- exe/racecar
|
73
76
|
- lib/racecar.rb
|
77
|
+
- lib/racecar/config.rb
|
78
|
+
- lib/racecar/consumer.rb
|
79
|
+
- lib/racecar/runner.rb
|
74
80
|
- lib/racecar/version.rb
|
75
81
|
- racecar.gemspec
|
76
82
|
homepage: https://github.com/zendesk/racecar
|
77
83
|
licenses:
|
78
|
-
-
|
84
|
+
- Apache License Version 2.0
|
79
85
|
metadata: {}
|
80
86
|
post_install_message:
|
81
87
|
rdoc_options: []
|