pheromone 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -1
- data/lib/pheromone/config.rb +1 -0
- data/lib/pheromone/frameworks/rspec.rb +11 -0
- data/lib/pheromone/messaging/message_dispatcher.rb +5 -0
- data/lib/pheromone/publishable.rb +8 -0
- data/lib/pheromone/version.rb +1 -1
- data/lib/pheromone.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63271f5d1308a9f022f2fa76e31ddf0f51e2bbaa
|
4
|
+
data.tar.gz: 8637f120bba37d51e1ce5ab52279f92b3f776a13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22adbaa1bf556272bf5ec71a10429a2f6e496ea051a2e64e659aa687f1c44706bd42668f4eaf965597e153ef79dfd07dc0c811383d0c46dadd9b30aaeafbed90
|
7
|
+
data.tar.gz: 089891f1c6bf466e5f6aa1c1f21697f0fe6fd2276886f8217656762fda2d50d6bc676fe6c232ce67323b2b992e1a3a1d131842217147cd1bdb46e4219376e817
|
data/README.md
CHANGED
@@ -52,6 +52,7 @@ Edit this file to modify the default config. The following configuration options
|
|
52
52
|
| background_processor.klass | String | Background processor class name that sends messages to kafka |
|
53
53
|
| timezone_format | String | Valid timezone name for timestamps sent to kafka |
|
54
54
|
| message_format | Symbol | Only supports :json format currently |
|
55
|
+
| enabled | Boolean | Defaults to true. When this is set to false, no messages will be sent to Kafka |
|
55
56
|
|
56
57
|
The timezone setting will transform any timestamp attributes in the message to the specified format.
|
57
58
|
|
@@ -91,6 +92,7 @@ Create a new class and add the name under `Pheromone.config.background_processor
|
|
91
92
|
def self.perform(message_object)
|
92
93
|
Pheromone::Messaging::Message.new(
|
93
94
|
topic: message_object['topic'],
|
95
|
+
metadata: { source: 'application1' },
|
94
96
|
blob: message_object['blob'],
|
95
97
|
metadata: message_object['metadata'],
|
96
98
|
options: message_object['options']
|
@@ -126,6 +128,7 @@ class PublishableModel < ActiveRecord::Base
|
|
126
128
|
{
|
127
129
|
event_types: [:create],
|
128
130
|
topic: :topic1,
|
131
|
+
metadata: { source: 'application1' },
|
129
132
|
message: ->(obj) { { name: obj.name } }
|
130
133
|
}
|
131
134
|
]
|
@@ -141,6 +144,7 @@ class PublishableModel < ActiveRecord::Base
|
|
141
144
|
{
|
142
145
|
event_types: [:update],
|
143
146
|
topic: :topic1,
|
147
|
+
metadata: { source: 'application1' },
|
144
148
|
message: ->(obj) { { name: obj.name } }
|
145
149
|
}
|
146
150
|
]
|
@@ -160,6 +164,7 @@ class PublishableModel < ActiveRecord::Base
|
|
160
164
|
{
|
161
165
|
event_types: [:create],
|
162
166
|
topic: :topic1,
|
167
|
+
metadata: { source: 'application1' },
|
163
168
|
message: ->(obj) { { name: obj.name } }
|
164
169
|
}
|
165
170
|
]
|
@@ -175,6 +180,7 @@ class PublishableModel < ActiveRecord::Base
|
|
175
180
|
{
|
176
181
|
event_types: [:update],
|
177
182
|
topic: :topic1,
|
183
|
+
metadata: { source: 'application1' },
|
178
184
|
message: message
|
179
185
|
}
|
180
186
|
]
|
@@ -194,6 +200,7 @@ class PublishableModel < ActiveRecord::Base
|
|
194
200
|
{
|
195
201
|
event_types: [:create],
|
196
202
|
topic: :topic1,
|
203
|
+
metadata: { source: 'application1' },
|
197
204
|
serializer: Class.new(BaseSerializer) { attributes :name, :type }
|
198
205
|
}
|
199
206
|
]
|
@@ -288,7 +295,30 @@ class PublishableModel < ActiveRecord::Base
|
|
288
295
|
end
|
289
296
|
```
|
290
297
|
|
291
|
-
### 7.
|
298
|
+
### 7. Specifying message metadata
|
299
|
+
|
300
|
+
These can be sent in by specifying `metadata` to the `publish` method:
|
301
|
+
|
302
|
+
```
|
303
|
+
class PublishableModel < ActiveRecord::Base
|
304
|
+
include Pheromone::Publishable
|
305
|
+
publish [
|
306
|
+
{
|
307
|
+
event_types: [:create],
|
308
|
+
topic: :topic_test,
|
309
|
+
message: ->(obj) { { name: obj.name } },
|
310
|
+
metadata: { source: 'application1' },
|
311
|
+
producer_options: {
|
312
|
+
key: 'key',
|
313
|
+
partition: 1,
|
314
|
+
partition_key: 'key'
|
315
|
+
}
|
316
|
+
}
|
317
|
+
]
|
318
|
+
end
|
319
|
+
```
|
320
|
+
|
321
|
+
### 8. Sending messages to Kafka directly
|
292
322
|
|
293
323
|
`pheromone` provides a custom message object that sends messages to Kafka in a predefined format, to maintain consistency in the message fields.
|
294
324
|
|
@@ -325,6 +355,38 @@ end
|
|
325
355
|
|
326
356
|
As seen above `timestamp` will be added automatically to the main attributes along with the message metadata. The actual message will be encapsulated inside a key called `blob`.
|
327
357
|
|
358
|
+
## Testing
|
359
|
+
|
360
|
+
#### RSpec
|
361
|
+
|
362
|
+
`pheromone` makes it easy to control when it is enabled during testing with `pheromone/frameworks/rspec`
|
363
|
+
|
364
|
+
```
|
365
|
+
# spec/rails_helper.rb
|
366
|
+
require 'rspec/rails'
|
367
|
+
# ...
|
368
|
+
require 'pheromone/frameworks/rspec'
|
369
|
+
|
370
|
+
```
|
371
|
+
|
372
|
+
With this helper, `publishable` is disabled by default. To turn it on, pass `publishable: true` to the spec block like this:
|
373
|
+
|
374
|
+
```
|
375
|
+
describe 'PublishableModel' do
|
376
|
+
before do
|
377
|
+
@invocation_count = 0
|
378
|
+
allow(WaterDrop::SyncProducer).to receive(:call) do
|
379
|
+
@invocation_count += 1
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'sends messages to kafka', publishable: true do
|
383
|
+
PublishableModel.create
|
384
|
+
expect(@invocation_count).to eq(1)
|
385
|
+
end
|
386
|
+
end
|
387
|
+
```
|
388
|
+
|
389
|
+
|
328
390
|
## Development
|
329
391
|
|
330
392
|
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.
|
data/lib/pheromone/config.rb
CHANGED
@@ -6,6 +6,7 @@ module Pheromone
|
|
6
6
|
extend Dry::Configurable
|
7
7
|
# accepts message format. Currently only accepts :json as the permitted value
|
8
8
|
setting :message_format, :json
|
9
|
+
setting :enabled, true
|
9
10
|
setting :background_processor do
|
10
11
|
# accepts :sidekiq or :resque as a value
|
11
12
|
setting :name
|
@@ -15,6 +15,7 @@ module Pheromone
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def dispatch
|
18
|
+
return unless enabled?
|
18
19
|
if @dispatch_method == :sync
|
19
20
|
message.send!
|
20
21
|
elsif @dispatch_method == :async
|
@@ -39,6 +40,10 @@ module Pheromone
|
|
39
40
|
Message.new(message_body)
|
40
41
|
end
|
41
42
|
|
43
|
+
def enabled?
|
44
|
+
Pheromone.config.enabled
|
45
|
+
end
|
46
|
+
|
42
47
|
def message_body
|
43
48
|
{
|
44
49
|
topic: @message_parameters[:topic],
|
@@ -11,6 +11,7 @@
|
|
11
11
|
# compression_threshold: 10,
|
12
12
|
# required_acks: 1
|
13
13
|
# }
|
14
|
+
# metadata: { source: 'application1' }
|
14
15
|
# event_types: [:create, :update],
|
15
16
|
# message: { a: 1, b: 2 }
|
16
17
|
# dispatch_method: :async
|
@@ -30,6 +31,8 @@ require 'pheromone/validators/options_validator'
|
|
30
31
|
require 'pheromone/exceptions/invalid_publish_options'
|
31
32
|
require 'pheromone/method_invoker'
|
32
33
|
require 'pheromone/messaging/message_dispatcher'
|
34
|
+
require 'pry'
|
35
|
+
|
33
36
|
module Pheromone
|
34
37
|
module Publishable
|
35
38
|
# class methods for the model including Publishable
|
@@ -56,6 +59,7 @@ module Pheromone
|
|
56
59
|
include Pheromone::MethodInvoker
|
57
60
|
|
58
61
|
def dispatch_messages(message_options:, current_event:)
|
62
|
+
return unless enabled?
|
59
63
|
message_options.each do |options|
|
60
64
|
next unless check_conditions(options, current_event)
|
61
65
|
send_message(options, current_event)
|
@@ -64,6 +68,10 @@ module Pheromone
|
|
64
68
|
|
65
69
|
private
|
66
70
|
|
71
|
+
def enabled?
|
72
|
+
Pheromone.config.enabled
|
73
|
+
end
|
74
|
+
|
67
75
|
def check_conditions(options, current_event)
|
68
76
|
condition_callback = options[:if]
|
69
77
|
result = check_event(options, current_event)
|
data/lib/pheromone/version.rb
CHANGED
data/lib/pheromone.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pheromone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ankita Gupta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|
@@ -216,6 +216,7 @@ files:
|
|
216
216
|
- lib/pheromone/config.rb
|
217
217
|
- lib/pheromone/exceptions/invalid_publish_options.rb
|
218
218
|
- lib/pheromone/exceptions/unsupported_message_format.rb
|
219
|
+
- lib/pheromone/frameworks/rspec.rb
|
219
220
|
- lib/pheromone/messaging/message.rb
|
220
221
|
- lib/pheromone/messaging/message_dispatcher.rb
|
221
222
|
- lib/pheromone/messaging/message_formatter.rb
|