phobos 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Dockerfile +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +176 -0
- data/README.md +413 -0
- data/Rakefile +6 -0
- data/bin/console +17 -0
- data/bin/phobos +9 -0
- data/bin/setup +8 -0
- data/circle.yml +27 -0
- data/config/phobos.yml.example +78 -0
- data/examples/handler_saving_events_database.rb +49 -0
- data/examples/handler_using_async_producer.rb +15 -0
- data/examples/publishing_messages_without_consumer.rb.rb +72 -0
- data/lib/phobos.rb +62 -0
- data/lib/phobos/cli.rb +61 -0
- data/lib/phobos/cli/runner.rb +48 -0
- data/lib/phobos/cli/start.rb +47 -0
- data/lib/phobos/echo_handler.rb +9 -0
- data/lib/phobos/errors.rb +4 -0
- data/lib/phobos/executor.rb +83 -0
- data/lib/phobos/handler.rb +23 -0
- data/lib/phobos/instrumentation.rb +21 -0
- data/lib/phobos/listener.rb +153 -0
- data/lib/phobos/producer.rb +122 -0
- data/lib/phobos/version.rb +3 -0
- data/phobos.gemspec +59 -0
- data/utils/create-topic.sh +17 -0
- data/utils/env.sh +11 -0
- data/utils/kafka.sh +43 -0
- data/utils/start-all.sh +9 -0
- data/utils/stop-all.sh +9 -0
- data/utils/zk.sh +36 -0
- metadata +275 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 97e08e50c3b2cfdb020c71c181989cb90e865316
|
4
|
+
data.tar.gz: 21bab9e83626f738b5903355e477b03ad5ff4d89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 17421090032e4212c626f708e80f5dca3d3c6421757982268980d954841473ab5176fdb907ac594bc079a5595b4f405b6402362767e01e12ec6b9a45d83e3d01
|
7
|
+
data.tar.gz: aa1b7bbf6f07c99cde3f2246a0dc848b9e7a1cb395e42e1513e5a1e7265488cb46b81dc48ae9d049abf61ebab4854dece2b4d594ab52a2e3310fd97412a2c406
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
FROM ruby:2.3.1-alpine
|
2
|
+
|
3
|
+
RUN apk update && apk upgrade && \
|
4
|
+
apk add --no-cache bash git openssh build-base
|
5
|
+
|
6
|
+
WORKDIR /opt/phobos
|
7
|
+
|
8
|
+
ADD Gemfile Gemfile
|
9
|
+
ADD phobos.gemspec phobos.gemspec
|
10
|
+
ADD lib/phobos/version.rb lib/phobos/version.rb
|
11
|
+
|
12
|
+
RUN ["bundle", "install"]
|
13
|
+
ADD . .
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,176 @@
|
|
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
ADDED
@@ -0,0 +1,413 @@
|
|
1
|
+
![Circle CI](https://circleci.com/gh/klarna/phobos.svg?style=shield&circle-token=2289e0fe5bd934074597b32e7f8f0bc98ea0e3c7)
|
2
|
+
|
3
|
+
# Phobos
|
4
|
+
|
5
|
+
Simplifying Kafka for Ruby apps!
|
6
|
+
|
7
|
+
Phobos is a micro framework and library for applications dealing with [Apache Kafka](http://kafka.apache.org/).
|
8
|
+
|
9
|
+
- It wraps common behaviors needed by consumers and producers in an easy and convenient API
|
10
|
+
- It uses [ruby-kafka](https://github.com/zendesk/ruby-kafka) as its Kafka client and core component
|
11
|
+
- It provides a CLI for starting and stopping a standalone application ready to be used for production purposes
|
12
|
+
|
13
|
+
Why Phobos? Why not `ruby-kafka` directly? Well, `ruby-kafka` is just a client. You still need to write a lot of code to manage proper consuming and producing of messages. You need to do proper message routing, error handling, retrying, backing off and maybe logging/instrumenting the message management process. You also need to worry about setting up a platform independent test environment that works on CI as well as any local machine, and even on your deployment pipeline. Finally, you also need to consider how to deploy your app and how to start it.
|
14
|
+
|
15
|
+
With Phobos by your side, all this becomes smooth sailing.
|
16
|
+
|
17
|
+
## Table of Contents
|
18
|
+
|
19
|
+
1. [Installation](#installation)
|
20
|
+
1. [Usage](#usage)
|
21
|
+
1. [Standalone apps](#usage-standalone-apps)
|
22
|
+
1. [Consuming messages from Kafka](#usage-consuming-messages-from-kafka)
|
23
|
+
1. [Producing messages to Kafka](#usage-producing-messages-to-kafka)
|
24
|
+
1. [As library in another app](#usage-as-library)
|
25
|
+
1. [Configuration file](#usage-configuration-file)
|
26
|
+
1. [Instrumentation](#usage-instrumentation)
|
27
|
+
1. [Development](#development)
|
28
|
+
|
29
|
+
## <a name="installation"></a> Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
gem 'phobos'
|
35
|
+
```
|
36
|
+
|
37
|
+
And then execute:
|
38
|
+
|
39
|
+
```sh
|
40
|
+
$ bundle
|
41
|
+
```
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
```sh
|
46
|
+
$ gem install phobos
|
47
|
+
```
|
48
|
+
|
49
|
+
## <a name="usage"></a> Usage
|
50
|
+
|
51
|
+
Phobos can be used in two ways: as a standalone application or to support Kafka features in your existing project - including Rails apps. It provides a CLI tool to run it.
|
52
|
+
|
53
|
+
### <a name="usage-standalone-apps"></a> Standalone apps
|
54
|
+
|
55
|
+
Standalone apps have benefits such as individual deploys and smaller code bases. If consuming from Kafka is your version of microservices, Phobos can be of great help.
|
56
|
+
|
57
|
+
### Setup
|
58
|
+
|
59
|
+
To create an application with Phobos you need two things:
|
60
|
+
* A configuration file (more details in the [Configuration file](#usage-configuration-file) section)
|
61
|
+
* A `phobos_boot.rb` (or the name of your choice) to properly load your code into Phobos executor
|
62
|
+
|
63
|
+
Use the Phobos CLI command __init__ to bootstrap your application. Example:
|
64
|
+
|
65
|
+
```sh
|
66
|
+
# call this command inside your app folder
|
67
|
+
$ phobos init
|
68
|
+
create config/phobos.yml
|
69
|
+
create phobos_boot.rb
|
70
|
+
```
|
71
|
+
|
72
|
+
`phobos.yml` is the configuration file and `phobos_boot.rb` is the place to load your code.
|
73
|
+
|
74
|
+
### Consumers (listeners and handlers)
|
75
|
+
|
76
|
+
In Phobos apps __listeners__ are configured against Kafka - they are our consumers. A listener requires a __handler__ (a ruby class where you should process incoming messages), a __topic__, and a __group_id__. Consumer groups are used to coordinate the listeners across machines. We write the __handlers__ and Phobos makes sure to run them for us. An example of a handler is:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class MyHandler
|
80
|
+
include Phobos::Handler
|
81
|
+
|
82
|
+
def consume(payload, metadata)
|
83
|
+
# payload - This is the content of your Kafka message, Phobos does not attempt to
|
84
|
+
# parse this content, it is delivered raw to you
|
85
|
+
# metadata - A hash with useful information about this event, it contains: The event key,
|
86
|
+
# partition number, offset, retry_count, topic, group_id, and listener_id
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
Writing a handler is all you need to allow Phobos to work - it will take care of execution, retries and concurrency.
|
92
|
+
|
93
|
+
To start Phobos the __start__ command is used, example:
|
94
|
+
|
95
|
+
```sh
|
96
|
+
$ phobos start
|
97
|
+
[2016-08-13T17:29:59:218+0200Z] INFO -- Phobos : <Hash> {:message=>"Phobos configured", :env=>"development"}
|
98
|
+
______ _ _
|
99
|
+
| ___ \ | | |
|
100
|
+
| |_/ / |__ ___ | |__ ___ ___
|
101
|
+
| __/| '_ \ / _ \| '_ \ / _ \/ __|
|
102
|
+
| | | | | | (_) | |_) | (_) \__ \
|
103
|
+
\_| |_| |_|\___/|_.__/ \___/|___/
|
104
|
+
|
105
|
+
phobos_boot.rb - find this file at ~/Projects/example/phobos_boot.rb
|
106
|
+
|
107
|
+
[2016-08-13T17:29:59:272+0200Z] INFO -- Phobos : <Hash> {:message=>"Listener started", :listener_id=>"6d5d2c", :group_id=>"test-1", :topic=>"test"}
|
108
|
+
```
|
109
|
+
|
110
|
+
By default, the __start__ command will look for the configuration file at `config/phobos.yml` and it will load the file `phobos_boot.rb` if it exists. In the example above all example files generated by the __init__ command are used as is. It is possible to change both files, use `-c` for the configuration file and `-b` for the boot file. Example:
|
111
|
+
|
112
|
+
```sh
|
113
|
+
$ phobos start -c /var/configs/my.yml -b /opt/apps/boot.rb
|
114
|
+
```
|
115
|
+
### <a name="usage-consuming-messages-from-kafka"></a> Consuming messages from Kafka
|
116
|
+
|
117
|
+
Messages from Kafka are consumed using __handlers__. You can use Phobos __executors__ or include it in your own projet [as a library](#usage-as-library), but __handlers__ will always be used. To create a handler class, simply include the module `Phobos::Handler`. This module allows Phobos to manage the life cycle of your handler.
|
118
|
+
|
119
|
+
A handler must implement the method `#consume(payload, metadata)`.
|
120
|
+
|
121
|
+
Instances of your handler will be created for every message, so keep a constructor without arguments. If `consume` raises an exception, Phobos will retry the message indefinitely, applying the back off configuration presented in the configuration file. The `metadata` hash will contain a key called `retry_count` with the current number of retries for this message. To skip a message, simply return from `#consume`.
|
122
|
+
|
123
|
+
When the listener starts, the class method `.start` will be called with the `kafka_client` used by the listener. Use this hook as a chance to setup necessary code for your handler. The class method `.stop` will be called during listener shutdown.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
class MyHandler
|
127
|
+
include Phobos::Handler
|
128
|
+
|
129
|
+
def self.start(kafka_client)
|
130
|
+
# setup handler
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.stop
|
134
|
+
# teardown
|
135
|
+
end
|
136
|
+
|
137
|
+
def consume(payload, metadata)
|
138
|
+
# consume or skip message
|
139
|
+
end
|
140
|
+
end
|
141
|
+
```
|
142
|
+
|
143
|
+
It is also possible to control the execution of `#consume` with the class method `.around_consume(payload, metadata)`. This method receives the payload and metadata, and then invokes `#consume` method by means of a block; example:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class MyHandler
|
147
|
+
include Phobos::Handler
|
148
|
+
|
149
|
+
def self.around_consume(payload, metadata)
|
150
|
+
Phobos.logger.info "consuming..."
|
151
|
+
output = yield
|
152
|
+
Phobos.logger.info "done, output: #{output}"
|
153
|
+
end
|
154
|
+
|
155
|
+
def consume(payload, metadata)
|
156
|
+
# consume or skip message
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
Take a look at the examples folder for some ideas.
|
162
|
+
|
163
|
+
The hander life cycle can be illustrated as:
|
164
|
+
|
165
|
+
`.start` -> `#consume` -> `.stop`
|
166
|
+
|
167
|
+
or optionally,
|
168
|
+
|
169
|
+
`.start` -> `.around_consume` [ `#consume` ] -> `.stop`
|
170
|
+
|
171
|
+
### <a name="usage-producing-messages-to-kafka"></a> Producing messages to Kafka
|
172
|
+
|
173
|
+
`ruby-kafka` provides several options for publishing messages, Phobos offers them through the module `Phobos::Producer`. It is possible to turn any ruby class into a producer (including your handlers), just include the producer module, example:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
class MyProducer
|
177
|
+
include Phobos::Producer
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
Phobos is designed for multi threading, thus the producer is always bound to the current thread. It is possible to publish messages from objects and classes, pick the option that suits your code better.
|
182
|
+
The producer module doesn't pollute your classes with a thousand methods, it includes a single method the class and in the instance level: `producer`.
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
my = MyProducer.new
|
186
|
+
my.producer.publish('topic', 'message-payload', 'partition and message key')
|
187
|
+
|
188
|
+
# The code above has the same effect of this code:
|
189
|
+
MyProducer.producer.publish('topic', 'message-payload', 'partition and message key')
|
190
|
+
```
|
191
|
+
|
192
|
+
It is also possible to publish several messages at once:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
MyProducer
|
196
|
+
.producer
|
197
|
+
.publish_list([
|
198
|
+
{ topic: 'A', payload: 'message-1', key: '1' },
|
199
|
+
{ topic: 'B', payload: 'message-2', key: '2' },
|
200
|
+
{ topic: 'B', payload: 'message-3', key: '3' }
|
201
|
+
])
|
202
|
+
```
|
203
|
+
|
204
|
+
There are two flavors of producers: __normal__ producers and __async__ producers.
|
205
|
+
|
206
|
+
Normal producers will deliver the messages synchronously and disconnect, it doesn't matter if you use `publish` or `publish_list` after the messages get delivered the producer will disconnect.
|
207
|
+
|
208
|
+
Async producers will accept your messages without blocking, use the methods `async_publish` and `async_publish_list` to use async producers.
|
209
|
+
|
210
|
+
An example of using handlers to publish messages:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
class MyHandler
|
214
|
+
include Phobos::Handler
|
215
|
+
include Phobos::Producer
|
216
|
+
|
217
|
+
PUBLISH_TO = 'topic2'
|
218
|
+
|
219
|
+
def consume(payload, metadata)
|
220
|
+
producer.async_publish(PUBLISH_TO, {key: 'value'}.to_json)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
#### Note about configuring producers
|
226
|
+
|
227
|
+
Since the handler life cycle is managed by the Listener, it will make sure the producer is properly closed before it stops. When calling the producer outside a handler remember, you need to shutdown them manually before you close the application. Use the class method `async_producer_shutdown` to safely shutdown the producer.
|
228
|
+
|
229
|
+
Without configuring the Kafka client, the producers will create a new one when needed (once per thread). To disconnect from kafka call `kafka_client.close`.
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
# This method will block until everything is safely closed
|
233
|
+
MyProducer
|
234
|
+
.producer
|
235
|
+
.async_producer_shutdown
|
236
|
+
|
237
|
+
MyProducer
|
238
|
+
.producer
|
239
|
+
.kafka_client
|
240
|
+
.close
|
241
|
+
```
|
242
|
+
|
243
|
+
### <a name="usage-as-library"></a> Phobos as a library in an existing project
|
244
|
+
|
245
|
+
When running as a standalone service, Phobos sets up a `Listener` and `Executor` for you. When you use Phobos as a library in your own project, you need to set these components up yourself.
|
246
|
+
|
247
|
+
First, call the method `configure` with the path of your configuration file
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
Phobos.configure('config/phobos.yml')
|
251
|
+
```
|
252
|
+
|
253
|
+
__Listener__ connects to Kafka and acts as your consumer. To create a listener you need a handler class, a topic, and a group id.
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
listener = Phobos::Listener.new(
|
257
|
+
handler: Phobos::EchoHandler,
|
258
|
+
group_id: 'group1',
|
259
|
+
topic: 'test'
|
260
|
+
)
|
261
|
+
|
262
|
+
# start method blocks
|
263
|
+
Thread.new { listener.start }
|
264
|
+
|
265
|
+
listener.id # 6d5d2c (all listeners have an id)
|
266
|
+
listener.stop # stop doesn't block
|
267
|
+
```
|
268
|
+
|
269
|
+
This is all you need to consume from Kafka with back off retries.
|
270
|
+
|
271
|
+
An __executor__ is the supervisor of all listeners. It loads all listeners configured in `phobos.yml`. The executor keeps the listeners running and restarts them when needed.
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
executor = Phobos::Executor.new
|
275
|
+
|
276
|
+
# start doesn't block
|
277
|
+
executor.start
|
278
|
+
|
279
|
+
# stop will block until all listers are properly stopped
|
280
|
+
executor.stop
|
281
|
+
```
|
282
|
+
|
283
|
+
When using Phobos __executors__ you don't care about how listeners are created, just provide the configuration under the `listeners` section in the configuration file and you are good to go.
|
284
|
+
|
285
|
+
### <a name="usage-configuration-file"></a> Configuration file
|
286
|
+
The configuration file is organized in 6 sections. Take a look at the example file, [config/phobos.yml.example](https://github.com/klarna/phobos/blob/master/config/phobos.yml.example).
|
287
|
+
|
288
|
+
__logger__ configures the logger for all Phobos components, it automatically outputs to `STDOUT` and it saves the log in the configured file
|
289
|
+
|
290
|
+
__kafka__ provides configurations for every `Kafka::Client` created over the application. All [options supported by `ruby-kafka`][ruby-kafka-client] can be provided.
|
291
|
+
|
292
|
+
__producer__ provides configurations for all producers created over the application, the options are the same for normal and async producers. All [options supported by `ruby-kafka`][ruby-kafka-producer] can be provided.
|
293
|
+
|
294
|
+
__consumer__ provides configurations for all consumer groups created over the application. All [options supported by `ruby-kafka`][ruby-kafka-consumer] can be provided.
|
295
|
+
|
296
|
+
__backoff__ Phobos provides automatic retries for your handlers, if an exception is raised the listener will retry following the back off configured here
|
297
|
+
|
298
|
+
__listeners__ is the list of listeners configured, each listener represents a consumers group
|
299
|
+
|
300
|
+
[ruby-kafka-client]: http://www.rubydoc.info/gems/ruby-kafka/Kafka%2FClient%3Ainitialize
|
301
|
+
[ruby-kafka-consumer]: http://www.rubydoc.info/gems/ruby-kafka/Kafka%2FClient%3Aconsumer
|
302
|
+
[ruby-kafka-producer]: http://www.rubydoc.info/gems/ruby-kafka/Kafka%2FClient%3Aproducer
|
303
|
+
|
304
|
+
### <a name="usage-instrumentation"></a> Instrumentation
|
305
|
+
|
306
|
+
Some operations are instrumented using [Active Support Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html).
|
307
|
+
|
308
|
+
In order to receive notifications you can use the module `Phobos::Instrumentation`, example:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
Phobos::Instrumentation.subscribe('listener.start') do |event|
|
312
|
+
puts(event.payload)
|
313
|
+
end
|
314
|
+
```
|
315
|
+
|
316
|
+
`Phobos::Instrumentation` is a convenience module around `ActiveSupport::Notifications`, feel free to use it or not. All Phobos events are in the `phobos` namespace. `Phobos::Instrumentation` will always look at `phobos.` events.
|
317
|
+
|
318
|
+
#### Executor notifications
|
319
|
+
* `executor.retry_listener_error` is sent when the listener crashes and the executor wait for a restart. It includes the following payload:
|
320
|
+
* listener_id
|
321
|
+
* retry_count
|
322
|
+
* waiting_time
|
323
|
+
* exception_class
|
324
|
+
* exception_message
|
325
|
+
* backtrace
|
326
|
+
* `executor.stop` is sent when executor stops
|
327
|
+
|
328
|
+
#### Listener notifications
|
329
|
+
* `listener.start_handler` is sent when invoking `handler.start(kafka_client)`. It includes the following payload:
|
330
|
+
* listener_id
|
331
|
+
* group_id
|
332
|
+
* topic
|
333
|
+
* `listener.start` is sent when listener starts. It includes the following payload:
|
334
|
+
* listener_id
|
335
|
+
* group_id
|
336
|
+
* topic
|
337
|
+
* `listener.process_batch` is sent after process a batch. It includes the following payload:
|
338
|
+
* listener_id
|
339
|
+
* group_id
|
340
|
+
* topic
|
341
|
+
* batch_size
|
342
|
+
* partition
|
343
|
+
* offset_lag
|
344
|
+
* highwater_mark_offset
|
345
|
+
* `listener.process_message` is sent after process a message. It includes the following payload:
|
346
|
+
* listener_id
|
347
|
+
* group_id
|
348
|
+
* topic
|
349
|
+
* key
|
350
|
+
* partition
|
351
|
+
* offset
|
352
|
+
* retry_count
|
353
|
+
* `listener.retry_handler_error` is sent after waited for `handler#consume` retry. It includes the following payload:
|
354
|
+
* listener_id
|
355
|
+
* group_id
|
356
|
+
* topic
|
357
|
+
* key
|
358
|
+
* partition
|
359
|
+
* offset
|
360
|
+
* retry_count
|
361
|
+
* waiting_time
|
362
|
+
* exception_class
|
363
|
+
* exception_message
|
364
|
+
* backtrace
|
365
|
+
* `listener.retry_aborted` is sent after waiting for a retry but the listener was stopped before the retry happened. It includes the following payload:
|
366
|
+
* listener_id
|
367
|
+
* group_id
|
368
|
+
* topic
|
369
|
+
* `listener.stopping` is sent when the listener receives signal to stop
|
370
|
+
* listener_id
|
371
|
+
* group_id
|
372
|
+
* topic
|
373
|
+
* `listener.stop_handler` is sent after stopping the handler
|
374
|
+
* listener_id
|
375
|
+
* group_id
|
376
|
+
* topic
|
377
|
+
* `listener.stop` is send after stopping the listener
|
378
|
+
* listener_id
|
379
|
+
* group_id
|
380
|
+
* topic
|
381
|
+
|
382
|
+
## <a name="development"></a> Development
|
383
|
+
|
384
|
+
After checking out the repo:
|
385
|
+
* make sure docker is installed and running
|
386
|
+
* run `bin/setup` to install dependencies
|
387
|
+
* run `sh utils/start-all.sh` to start the required kafka containers in the background
|
388
|
+
* run `rspec` to run the tests
|
389
|
+
|
390
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
391
|
+
|
392
|
+
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).
|
393
|
+
|
394
|
+
The `utils` folder contain some shell scripts to help with the local Kafka cluster. It uses docker to start Kafka and zookeeper.
|
395
|
+
|
396
|
+
```sh
|
397
|
+
sh utils/start-all.sh
|
398
|
+
sh utils/stop-all.sh
|
399
|
+
```
|
400
|
+
|
401
|
+
## Contributing
|
402
|
+
|
403
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/klarna/phobos.
|
404
|
+
|
405
|
+
## License
|
406
|
+
|
407
|
+
Copyright 2016 Klarna
|
408
|
+
|
409
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
410
|
+
|
411
|
+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
412
|
+
|
413
|
+
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.
|