tobox 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/CHANGELOG.md +10 -0
- data/LICENSE.txt +191 -0
- data/README.md +384 -0
- data/exe/tobox +14 -0
- data/lib/tobox/application.rb +32 -0
- data/lib/tobox/cli.rb +145 -0
- data/lib/tobox/configuration.rb +122 -0
- data/lib/tobox/fetcher.rb +139 -0
- data/lib/tobox/plugins/datadog/configuration.rb +34 -0
- data/lib/tobox/plugins/datadog/integration.rb +39 -0
- data/lib/tobox/plugins/datadog/patcher.rb +26 -0
- data/lib/tobox/plugins/datadog.rb +102 -0
- data/lib/tobox/plugins/sentry.rb +143 -0
- data/lib/tobox/plugins/zeitwerk.rb +52 -0
- data/lib/tobox/pool/fiber_pool.rb +49 -0
- data/lib/tobox/pool/threaded_pool.rb +55 -0
- data/lib/tobox/pool.rb +19 -0
- data/lib/tobox/version.rb +5 -0
- data/lib/tobox/worker.rb +49 -0
- data/lib/tobox.rb +41 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 20f2b2ccbeacc67bb55403fa085c7d02bbe38e7f49263a5c006958bb35d987f2
|
4
|
+
data.tar.gz: 0e69f560de1959cf87acc8b245ae02dfdf7b8beff693023a6a2651a29cb9b43d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3588443eaf61c20ce1eebcf8b3a291ff959bc9861a5ffb157210b9790106e7ed5fe9818362539c5943d1531afbe93d8e9c84f8dd545b8d3a22d9fcede7d0a43
|
7
|
+
data.tar.gz: 1d19aa8d047754f107043fcab8c6ea2835007fb1dfe206bedb2831561f5cf4616e03befe46310ac4463cbd8adf7abe532d736acc7e6790dde792c7a2ccb80972
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
https://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
Copyright 2013-2017 Docker, Inc.
|
180
|
+
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
182
|
+
you may not use this file except in compliance with the License.
|
183
|
+
You may obtain a copy of the License at
|
184
|
+
|
185
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
186
|
+
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
190
|
+
See the License for the specific language governing permissions and
|
191
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,384 @@
|
|
1
|
+
# Tobox: Transactional outbox pattern implementation in ruby
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/tobox.svg)](http://rubygems.org/gems/tobox)
|
4
|
+
[![pipeline status](https://gitlab.com/honeyryderchuck/tobox/badges/master/pipeline.svg)](https://gitlab.com/honeyryderchuck/tobox/pipelines?page=1&scope=all&ref=master)
|
5
|
+
[![coverage report](https://gitlab.com/honeyryderchuck/tobox/badges/master/coverage.svg?job=coverage)](https://honeyryderchuck.gitlab.io/tobox/#_AllFiles)
|
6
|
+
|
7
|
+
Simple, data-first events processing framework based on the [transactional outbox pattern](https://microservices.io/patterns/data/transactional-outbox.html).
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
`tobox` requires integration with RDBMS which supports `SKIP LOCKED` functionality. As of today, that's:
|
12
|
+
|
13
|
+
* PostgreSQL 9.5+
|
14
|
+
* MySQL 8+
|
15
|
+
* Oracle
|
16
|
+
* Microsoft SQL Server
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "tobox"
|
24
|
+
|
25
|
+
# You'll also need to aadd the right database client gem for the target RDBMS
|
26
|
+
# ex, for postgresql:
|
27
|
+
#
|
28
|
+
# gem "pg"
|
29
|
+
# see more: http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle install
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install tobox
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
1. create the `outbox` table in your application's database:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# example migration using sequel
|
46
|
+
Sequel.migration do
|
47
|
+
up do
|
48
|
+
create_table(:outbox) do
|
49
|
+
primary_key :id
|
50
|
+
column :type, :varchar, null: false
|
51
|
+
column :data_before, :json, null: true
|
52
|
+
column :data_after, :json, null: true
|
53
|
+
column :created_at, "timestamp without time zone", null: false, default: Sequel::CURRENT_TIMESTAMP
|
54
|
+
column :attempts, :integer, null: false, default: 0
|
55
|
+
column :run_at, "timestamp without time zone", null: true
|
56
|
+
column :last_error, :text, null: true
|
57
|
+
column :metadata, :json, null: true
|
58
|
+
|
59
|
+
index Sequel.desc(:run_at)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
down do
|
64
|
+
drop_table(:outbox)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
2. create a `tobox.rb` config file in your project directory tree:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
# tobox
|
72
|
+
database Sequel.connect("postgres://user:pass@dbhost/database")
|
73
|
+
# table :outbox
|
74
|
+
# concurrency 8
|
75
|
+
on("user_created") do |event|
|
76
|
+
puts "created user #{event[:after]["id"]}"
|
77
|
+
DataLakeService.user_created(user_data_hash)
|
78
|
+
BillingService.bill_user_account(user_data_hash)
|
79
|
+
end
|
80
|
+
on("user_updated") do |event|
|
81
|
+
# ...
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
3. Start the `tobox` process
|
86
|
+
|
87
|
+
```
|
88
|
+
> bundle exec tobox -C path/to/tobox.rb -r path/to/file_requiring_application_code.rb
|
89
|
+
```
|
90
|
+
|
91
|
+
There is no API for event production yet (still TODO). It's recommended you write directly into the "outbox" table via database triggers (i.e. *insert into users table -> add user_created event"). Alternatively you can use `sequel` directly (`DB[:outbox].insert(...)`).
|
92
|
+
|
93
|
+
4. Emit outbox events
|
94
|
+
|
95
|
+
Currently, `tobox` only deals with outbox events consumption. When it comes to producing, you can do it yourself. There essentially two alternatives:
|
96
|
+
|
97
|
+
4.1 Emit from application code
|
98
|
+
|
99
|
+
If you're using `sequel` as your ORM, you can use the dataset API:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# Assuming DB points to your `Sequel::Database`, and defaults are used:
|
103
|
+
order = Order.new(
|
104
|
+
item_id: item.id,
|
105
|
+
price: 20_20,
|
106
|
+
currency: "EUR"
|
107
|
+
)
|
108
|
+
DB.transaction do
|
109
|
+
order.save
|
110
|
+
DB[:outbox].insert(event_type: "order_created", data_after: order.to_hash)
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
4.2 Emit from database trigger
|
115
|
+
|
116
|
+
This is how it could be done in PostgreSQL using trigger functions:
|
117
|
+
|
118
|
+
```sql
|
119
|
+
CREATE OR REPLACE FUNCTION order_created_outbox_event()
|
120
|
+
RETURNS TRIGGER
|
121
|
+
LANGUAGE PLPGSQL
|
122
|
+
AS
|
123
|
+
$$
|
124
|
+
BEGIN
|
125
|
+
INSERT INTO outbox(event_type, data_after)
|
126
|
+
VALUES('order_created', row_to_json(NEW.*));
|
127
|
+
RETURN NEW;
|
128
|
+
END;
|
129
|
+
$$
|
130
|
+
|
131
|
+
CREATE TRIGGER order_created_outbox_event
|
132
|
+
AFTER INSERT
|
133
|
+
ON orders
|
134
|
+
FOR EACH ROW
|
135
|
+
EXECUTE PROCEDURE order_created_outbox_event();
|
136
|
+
```
|
137
|
+
|
138
|
+
## Configuration
|
139
|
+
|
140
|
+
As mentioned above, configuration can be set in a particular file. The following options are configurable:
|
141
|
+
|
142
|
+
### `environment``
|
143
|
+
|
144
|
+
Sets the application environment (either "development" or "production"). Can be set directly, or via `APP_ENV` environment variable (defaults to "development").
|
145
|
+
|
146
|
+
### `database_uri`
|
147
|
+
|
148
|
+
Accepts a URI pointing to a database, [where scheme identifies the database adapter to be used](https://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html):
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
database_uri `"postgres://user:password@localhost/blog"`.
|
152
|
+
```
|
153
|
+
|
154
|
+
### `table`
|
155
|
+
|
156
|
+
the name of the database table where outbox events are stored (`:outbox` by default).
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
table :outbox
|
160
|
+
```
|
161
|
+
|
162
|
+
### `max_attempts`
|
163
|
+
|
164
|
+
Maximum number of times a failed attempt to process an event will be retried (`10` by default).
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
concurrency 4
|
168
|
+
```
|
169
|
+
|
170
|
+
**Note**: the new attempt will be retried in `n ** 4`, where `n` is the number of past attempts for that event.
|
171
|
+
|
172
|
+
### `concurrency`
|
173
|
+
|
174
|
+
Number of workers processing events.
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
concurrency 4
|
178
|
+
```
|
179
|
+
|
180
|
+
**Note**: the default concurrency is adapted and different for each worker pool type, so make sure you understand how this tweak may affect you.
|
181
|
+
|
182
|
+
### `worker`
|
183
|
+
|
184
|
+
Type of the worker used to process events. Can be `:thread` (default), `:fiber`, or a class implementing the `Tobox::Pool` protocol (TBD: define what this protocol is).
|
185
|
+
|
186
|
+
### `wait_for_events_delay`
|
187
|
+
|
188
|
+
Time (in seconds) to wait before checking again for events in the outbox.
|
189
|
+
|
190
|
+
### `shutdown_timeout`
|
191
|
+
|
192
|
+
Time (in seconds) to wait for events to finishing processing, before hard-killing the process.
|
193
|
+
|
194
|
+
### `on(event_type) { |before, after| }`
|
195
|
+
|
196
|
+
callback executed when processing an event of the given type. By default, it'll yield the state of data before and after the event (unless `message_to_arguments` is set).
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
on("order_created") { |event| puts "order created: #{event[:after]}" }
|
200
|
+
on("order_updated") { |event| puts "order created: was #{event[:before]}, now is #{event[:after]}" }
|
201
|
+
# ...
|
202
|
+
```
|
203
|
+
|
204
|
+
### `on_before_event { |event| }`
|
205
|
+
|
206
|
+
callback executed right before proocessing an event.
|
207
|
+
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
on_before_event { |event| start_trace(event[:id]) }
|
211
|
+
```
|
212
|
+
|
213
|
+
### `on_after_event { |event| }`
|
214
|
+
|
215
|
+
callback executed right after proocessing an event.
|
216
|
+
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
on_before_event { |event| finish_trace(event[:id]) }
|
220
|
+
```
|
221
|
+
|
222
|
+
### `on_error_event { |event, error| }`
|
223
|
+
|
224
|
+
callback executed when an exception was raised while processing an event.
|
225
|
+
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
on_error_event { |event, exception| Sentry.capture_exception(exception) }
|
229
|
+
```
|
230
|
+
|
231
|
+
### `message_to_arguments { |event| }`
|
232
|
+
|
233
|
+
if exposing raw data to the `on` handlers is not what you'd want, you can always override the behaviour by providing an alternative "before/after fetcher" implementation.
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
# if you'd like to yield the ORM object only
|
237
|
+
message_to_arguments do |event|
|
238
|
+
case event_type
|
239
|
+
when "order_created", "order_updated"
|
240
|
+
Order.get(after[:id])
|
241
|
+
when "payment_created", "payment_processed", "payment_reconciled"
|
242
|
+
Payment.get(after[:id])
|
243
|
+
else
|
244
|
+
super(event)
|
245
|
+
end
|
246
|
+
on("order_created") { |order| puts "order created: #{order}" }
|
247
|
+
# ...
|
248
|
+
on("payment_created") { |payment| puts "payment created: #{payment}" }
|
249
|
+
# ...
|
250
|
+
```
|
251
|
+
|
252
|
+
### logger
|
253
|
+
|
254
|
+
Overrides the internal logger (an instance of `Logger`).
|
255
|
+
|
256
|
+
### log_level
|
257
|
+
|
258
|
+
Overrides the default log level ("info" when in "production" environment, "debug" otherwise).
|
259
|
+
|
260
|
+
## Event
|
261
|
+
|
262
|
+
The event is composed of the following properties:
|
263
|
+
|
264
|
+
* `:id`: unique event identifier
|
265
|
+
* `:type`: label identifying the event (i.e. `"order_created"`)
|
266
|
+
* `:before`: hash of the associated event data before event is emitted (can be `nil`)
|
267
|
+
* `:after`: hash of the associated event data after event is emitted (can be `nil`)
|
268
|
+
* `:created_at`: timestamp of when the event is emitted
|
269
|
+
|
270
|
+
(*NOTE*: The event is also composed of other properties which are only relevant for `tobox`.)
|
271
|
+
|
272
|
+
## Rails support
|
273
|
+
|
274
|
+
Rails is supported out of the box by adding the [sequel-activerecord_connection](https://github.com/janko/sequel-activerecord_connection) gem into your Gemfile, and requiring the rails application in the `tobox` cli call:
|
275
|
+
|
276
|
+
```bash
|
277
|
+
> bundle exec tobox -C path/to/tobox.rb -r path/to/rails_app/config/environment.rb
|
278
|
+
```
|
279
|
+
|
280
|
+
In the `tobox` config, you can set the environment:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
environment Rails.env
|
284
|
+
```
|
285
|
+
|
286
|
+
## Plugins
|
287
|
+
|
288
|
+
`tobox` ships with a very simple plugin system. (TODO: add docs).
|
289
|
+
|
290
|
+
Plugins can be loaded in the config via `plugin`:
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
# tobox.rb
|
294
|
+
plugin(:plugin_name)
|
295
|
+
```
|
296
|
+
|
297
|
+
It ships with the following integrations.
|
298
|
+
|
299
|
+
### Zeitwerk
|
300
|
+
|
301
|
+
(requires the `zeitwerk` gem.)
|
302
|
+
|
303
|
+
Plugin for the [zeitwerk](https://github.com/fxn/zeitwerk) auto-loader. It allows to set the autoload dirs, and seamlessly integrates code reloading in "development", and eagerloading in "production":
|
304
|
+
|
305
|
+
```ruby
|
306
|
+
# tobox.rb
|
307
|
+
plugin(:zeitwerk)
|
308
|
+
zeitwerk_loader do |loader|
|
309
|
+
loader.push_dir("path/to/handlers")
|
310
|
+
end
|
311
|
+
```
|
312
|
+
|
313
|
+
### Sentry
|
314
|
+
|
315
|
+
(requires the `sentry-ruby` gem.)
|
316
|
+
|
317
|
+
Plugin for the [sentry](https://github.com/getsentry/sentry-ruby) ruby SDK for error tracking. It'll send all errors happening while processing events to Sentry.
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
# tobox.rb
|
321
|
+
plugin(:sentry)
|
322
|
+
```
|
323
|
+
|
324
|
+
### Datadog
|
325
|
+
|
326
|
+
(requires the `ddtrace` gem.)
|
327
|
+
|
328
|
+
Plugin for [datadog](https://github.com/DataDog/dd-trace-rb) ruby SDK. It'll generate traces for event handling.
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
# you can init the datadog config in another file to load:
|
332
|
+
Datadog.configure do |c|
|
333
|
+
c.tracing.instrument :tobox
|
334
|
+
end
|
335
|
+
|
336
|
+
# tobox.rb
|
337
|
+
plugin(:datadog)
|
338
|
+
```
|
339
|
+
|
340
|
+
## Supported Rubies
|
341
|
+
|
342
|
+
All Rubies greater or equal to 2.6, and always latest JRuby and Truffleruby.
|
343
|
+
|
344
|
+
## Why?
|
345
|
+
|
346
|
+
### Simple and lightweight, framework (and programming language) agnostic
|
347
|
+
|
348
|
+
`tobox` event callbacks yield the data in ruby primitive types, rather than heavy ORM instances. This is by design, as callbacks may not rely on application code being loaded.
|
349
|
+
|
350
|
+
This allows `tobox` to process events dispatched from an application done in another programmming language, as an example.
|
351
|
+
|
352
|
+
|
353
|
+
### No second storage system
|
354
|
+
|
355
|
+
While `tobox` does not advertise itself as a background job framework, it can be used as such.
|
356
|
+
|
357
|
+
Most tiered applications already have an RDBMS. Popular background job solutions, like `"sidekiq"` and `"shoryuken"`, usually require integrating with a separate message broker (Redis, SQS, RabbitMQ...). This increases the overhead in deployment and operations, as these brokers need to be provisioned, monitored, scaled separately, and billed differently.
|
358
|
+
|
359
|
+
`tobox` only requires the database you usually need to account for anyway, allowing you to delay buying into more complicated setups until you have to and have budget for.
|
360
|
+
|
361
|
+
However, it can work well in tandem with such solutions:
|
362
|
+
|
363
|
+
```ruby
|
364
|
+
# process event by scheduling an active job
|
365
|
+
on("order_created") { |event| SendOrderMailJob.perform_later(event[:after]["id"]) }
|
366
|
+
```
|
367
|
+
|
368
|
+
### Atomic processing via database transactions
|
369
|
+
|
370
|
+
When scheduling work, one needs to ensure that data is committed to the database before scheduling. This is a very frequent bug when using non-RDBMS background job frameworks, such as [Sidekiq, which has a FAQ entry for this](https://github.com/mperham/sidekiq/wiki/FAQ#why-am-i-seeing-a-lot-of-cant-find-modelname-with-id12345-errors-with-sidekiq) .
|
371
|
+
|
372
|
+
But even if you do that, the system can go down **after** the data is committed in the database and **before** the job is enqueued to the broker. Failing to address this behaviour makes the [job delivery guarantee "at most once"](https://brandur.org/job-drain). This may or may not be a problem depending on what your job does (if it bills a customer, it probably is).
|
373
|
+
|
374
|
+
By using the database as the message broker, `tobox` can rely on good old transactions to ensure that data committed to the database has a corresponding event. This makes the delivery guarantee "exactly once".
|
375
|
+
|
376
|
+
(The actual processing may change this to "at least once", as issues may happen before the event is successfully deleted from the outbox. Still, "at least once" is acceptable and solvable using idempotency mechanisms).
|
377
|
+
|
378
|
+
## Development
|
379
|
+
|
380
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
381
|
+
|
382
|
+
## Contributing
|
383
|
+
|
384
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/honeyryderchuck/tobox.
|
data/exe/tobox
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tobox
|
4
|
+
class Application
|
5
|
+
def initialize(configuration)
|
6
|
+
@configuration = configuration
|
7
|
+
@running = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def start
|
11
|
+
return if @running
|
12
|
+
|
13
|
+
worker = @configuration[:worker]
|
14
|
+
|
15
|
+
@pool = case worker
|
16
|
+
when :thread then ThreadedPool
|
17
|
+
when :fiber then FiberPool
|
18
|
+
else worker
|
19
|
+
end.new(@configuration)
|
20
|
+
|
21
|
+
@running = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def stop
|
25
|
+
return unless @running
|
26
|
+
|
27
|
+
@pool.stop
|
28
|
+
|
29
|
+
@running = false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|