pheromone 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -24
- data/lib/pheromone/config.rb +6 -1
- data/lib/pheromone/messaging/message_dispatcher.rb +7 -1
- data/lib/pheromone/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdb8d44235a3ccf8f853460d9b49ea652a0bd3ce
|
4
|
+
data.tar.gz: 1c4694ebbc1e013a37159b88733ad56bcbfb5af9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c5e66513aa2078d9f94aa62f0e0c3f3b9a73fb571d3b8ae87584141e4dcb0a09b012a8a745f60dfe30c6952b857b1e7d36b76e0a717f3b607d9f0bde963b55d
|
7
|
+
data.tar.gz: 0313d08e66fed868d7aaa3781a9482e3b6102f1f02f3bcc735723030200d790147eb213bf2be74bdbac3ba35d6a57fca09652f6a0935b57a688c92fce214ff48
|
data/README.md
CHANGED
@@ -36,7 +36,7 @@ Pheromone.setup do |pheromone_config|
|
|
36
36
|
# pheromone_config.background_processor.klass = 'BackgroundWorker'
|
37
37
|
# pheromone_config.timezone = 'UTC'
|
38
38
|
pheromone_config.message_format = :json
|
39
|
-
WaterDrop.setup do |waterdrop_config|
|
39
|
+
WaterDrop.setup do |waterdrop_config|
|
40
40
|
waterdrop_config.deliver = Rails.env.production?
|
41
41
|
waterdrop_config.kafka.seed_brokers = [Rails.env.production? ? ENV['KAFKA_HOST'] : 'localhost:9092']
|
42
42
|
end
|
@@ -46,10 +46,11 @@ end
|
|
46
46
|
Edit this file to modify the default config. The following configuration options are available:
|
47
47
|
|
48
48
|
|
49
|
-
| Option | Value type | Description |
|
49
|
+
| Option | Value type | Description |
|
50
50
|
|-------------------------------|---------------|----------------------------------|
|
51
51
|
| background_processor.name | Symbol | Choose :sidekiq or :resque as the background processor only if messages need to be sent to kafka asynchronously |
|
52
52
|
| background_processor.klass | String | Background processor class name that sends messages to kafka |
|
53
|
+
| background_processor.custom_processor |Proc| Custom processor (only works when you specify name as :custom) |
|
53
54
|
| timezone_format | String | Valid timezone name for timestamps sent to kafka |
|
54
55
|
| message_format | Symbol | Only supports :json format currently |
|
55
56
|
| enabled | Boolean | Defaults to true. When this is set to false, no messages will be sent to Kafka |
|
@@ -66,7 +67,7 @@ The underlying Kafka client used by `pheromone` is `ruby-kafka`. This client pro
|
|
66
67
|
It is advisable to use the normal producer in production systems because async producer provides no guarantees that the messages will be delivered. To read more on this, refer the `ruby-kafka` [documentation](https://github.com/zendesk/ruby-kafka#asynchronously-producing-messages)
|
67
68
|
|
68
69
|
Even while using a synchronous producer, sometimes there might be a need to run send messages to Kafka in a background task. This is especially true for batch processing tasks that send a high message volume to Kafka. To allow for this, `pheromone` provides an `async` mode that can be specified as an option to `publish` by specifying `dispatch_method` as `:async`. By default, `dispatch_method` will be `:sync`. Specifying `:async` will still use the normal producer and NOT the async_producer.
|
69
|
-
|
70
|
+
|
70
71
|
```
|
71
72
|
class PublishableModel < ActiveRecord::Base
|
72
73
|
include Pheromone::Publishable
|
@@ -117,6 +118,18 @@ Create a new class and add the name under `Pheromone.config.background_processor
|
|
117
118
|
end
|
118
119
|
end
|
119
120
|
```
|
121
|
+
#### 1.c. Implement your own processor
|
122
|
+
You can also implement your own processor in addition to resque and sidekiq.
|
123
|
+
```ruby
|
124
|
+
Pheromone.setup do |config|
|
125
|
+
config.background_processor.name = :custom
|
126
|
+
config.background_processor.klass = 'CustomJob'
|
127
|
+
config.background_processor.custom_processor = -> (klass, msg) do
|
128
|
+
klass.perform(msg)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
120
133
|
`pheromone` will invoke the class name specified in the config with the message object. This mode can be used if you don't want to block a request that ends up sending messages to Kafka.
|
121
134
|
|
122
135
|
### 2. Supported events
|
@@ -300,7 +313,7 @@ end
|
|
300
313
|
|
301
314
|
Pheromone makes it easier to standardise the format of messages sent to Kafka while affording the flexibility to add other custom fields using the metadata option.
|
302
315
|
|
303
|
-
By default, the messages sent to Kafka have an event_type and timestamp attached to it as shown below:
|
316
|
+
By default, the messages sent to Kafka have an event_type and timestamp attached to it as shown below:
|
304
317
|
```
|
305
318
|
{
|
306
319
|
'event_type' => 'create',
|
@@ -333,7 +346,7 @@ end
|
|
333
346
|
```
|
334
347
|
|
335
348
|
The Kafka message will have the original metadata in addition to the new fields:
|
336
|
-
|
349
|
+
|
337
350
|
|
338
351
|
```
|
339
352
|
{
|
@@ -351,15 +364,15 @@ The Kafka message will have the original metadata in addition to the new fields:
|
|
351
364
|
`pheromone` provides a custom message object that sends messages to Kafka in a predefined format, to maintain consistency in the message fields.
|
352
365
|
|
353
366
|
`Pheromone::Messaging::Message` can be initialized with the following arguments:
|
354
|
-
- `topic`: name of the topic to which the message is produced
|
367
|
+
- `topic`: name of the topic to which the message is produced
|
355
368
|
- `blob`: the actual message itself
|
356
369
|
- `metadata`: any additional fields that must be sent along with the message
|
357
370
|
- `options`: producer options as described in Section 6
|
358
|
-
|
371
|
+
|
359
372
|
Of these fields, only `topic` and `message` are compulsory and the remaining two are optional.
|
360
|
-
|
373
|
+
|
361
374
|
Example usage:
|
362
|
-
|
375
|
+
|
363
376
|
```
|
364
377
|
Pheromone::Messaging::Message.new(
|
365
378
|
topic: 'test_topic',
|
@@ -368,9 +381,9 @@ The Kafka message will have the original metadata in addition to the new fields:
|
|
368
381
|
producer_options: { max_retries: 5 }
|
369
382
|
).send!
|
370
383
|
```
|
371
|
-
|
372
|
-
This will send a message to `test_topic` in Kafka in the following format:
|
373
|
-
|
384
|
+
|
385
|
+
This will send a message to `test_topic` in Kafka in the following format:
|
386
|
+
|
374
387
|
```
|
375
388
|
{
|
376
389
|
'event_type' => 'create',
|
@@ -389,9 +402,9 @@ As seen above `timestamp` will be added automatically to the main attributes alo
|
|
389
402
|
|
390
403
|
`publish` takes the `encoder` and `message_format` as options for encoding to be specified. Encoder is called with the entire message object and performs encoding on this message.
|
391
404
|
|
392
|
-
```
|
405
|
+
```
|
393
406
|
publish(
|
394
|
-
[
|
407
|
+
[
|
395
408
|
{
|
396
409
|
topic: :test_topic,
|
397
410
|
event_types: [:update, :create],
|
@@ -419,7 +432,7 @@ The message published to Kafka looks like this:
|
|
419
432
|
}
|
420
433
|
}
|
421
434
|
```
|
422
|
-
From `0.5` version onwards, the message format looks like this:
|
435
|
+
From `0.5` version onwards, the message format looks like this:
|
423
436
|
```
|
424
437
|
{
|
425
438
|
metadata: {
|
@@ -435,10 +448,10 @@ The message published to Kafka looks like this:
|
|
435
448
|
`event`, `entity`, and `timestamp` are determined and added by `pheromone`. `timestamp` will be in UTC by default and will use `timezone_format` value specified in `config/initializers/pheromone.rb`. Contents of `metadata` can be modified by using these [guidelines](https://github.com/ankitagupta12/pheromone#7-specifying-message-metadata). Message contents are placed under the key `blob`.
|
436
449
|
|
437
450
|
In order to use the format with `blob` embedded inside `metadata`, specify option `embed_blob` as `true` inside `publish` options like this:
|
438
|
-
|
439
|
-
```
|
451
|
+
|
452
|
+
```
|
440
453
|
publish(
|
441
|
-
[
|
454
|
+
[
|
442
455
|
{
|
443
456
|
topic: :test_topic,
|
444
457
|
event_types: [:update, :create],
|
@@ -456,17 +469,17 @@ The message published to Kafka looks like this:
|
|
456
469
|
#### RSpec
|
457
470
|
|
458
471
|
`pheromone` makes it easy to control when it is enabled during testing with `pheromone/frameworks/rspec`
|
459
|
-
|
472
|
+
|
460
473
|
```
|
461
474
|
# spec/rails_helper.rb
|
462
475
|
require 'rspec/rails'
|
463
476
|
# ...
|
464
477
|
require 'pheromone/frameworks/rspec'
|
465
|
-
|
478
|
+
|
466
479
|
```
|
467
|
-
|
480
|
+
|
468
481
|
With this helper, `publishable` is disabled by default. To turn it on, pass `publishable: true` to the spec block like this:
|
469
|
-
|
482
|
+
|
470
483
|
```
|
471
484
|
describe 'PublishableModel' do
|
472
485
|
before do
|
@@ -474,7 +487,7 @@ The message published to Kafka looks like this:
|
|
474
487
|
allow(WaterDrop::SyncProducer).to receive(:call) do
|
475
488
|
@invocation_count += 1
|
476
489
|
end
|
477
|
-
|
490
|
+
|
478
491
|
it 'sends messages to kafka', publishable: true do
|
479
492
|
PublishableModel.create
|
480
493
|
expect(@invocation_count).to eq(1)
|
@@ -482,7 +495,7 @@ The message published to Kafka looks like this:
|
|
482
495
|
end
|
483
496
|
```
|
484
497
|
Alternatively, `Pheromone.enabled?` can be used to check if `pheromone` is enabled
|
485
|
-
|
498
|
+
|
486
499
|
## Development
|
487
500
|
|
488
501
|
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
@@ -8,10 +8,15 @@ module Pheromone
|
|
8
8
|
setting :message_format, :json
|
9
9
|
setting :enabled, true
|
10
10
|
setting :background_processor do
|
11
|
+
# type: symbol
|
11
12
|
# accepts :sidekiq or :resque as a value
|
12
13
|
setting :name
|
14
|
+
# type: string
|
13
15
|
# specify the background job handling message send to kafka
|
14
16
|
setting :klass
|
17
|
+
# type: proc/lambda
|
18
|
+
# specify custom background job
|
19
|
+
setting :custom_processor
|
15
20
|
end
|
16
21
|
# timezone names should match a valid timezone defined here:
|
17
22
|
# http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
|
@@ -25,4 +30,4 @@ module Pheromone
|
|
25
30
|
end
|
26
31
|
end
|
27
32
|
end
|
28
|
-
end
|
33
|
+
end
|
@@ -9,6 +9,8 @@ require 'pheromone/messaging/message'
|
|
9
9
|
module Pheromone
|
10
10
|
module Messaging
|
11
11
|
class MessageDispatcher
|
12
|
+
class NoSpecifiedProcessor < StandardError; end
|
13
|
+
|
12
14
|
def initialize(message_parameters:, dispatch_method:)
|
13
15
|
@message_parameters = message_parameters
|
14
16
|
@dispatch_method = dispatch_method
|
@@ -38,6 +40,10 @@ module Pheromone
|
|
38
40
|
Resque.enqueue(background_processor_klass, message_body)
|
39
41
|
elsif background_processor.name == :sidekiq
|
40
42
|
background_processor_klass.perform_async(message_body)
|
43
|
+
elsif background_processor.name == :custom
|
44
|
+
background_processor.custom_processor.call(background_processor_klass, message_body)
|
45
|
+
else
|
46
|
+
raise NoSpecifiedProcessor
|
41
47
|
end
|
42
48
|
end
|
43
49
|
|
@@ -60,4 +66,4 @@ module Pheromone
|
|
60
66
|
end
|
61
67
|
end
|
62
68
|
end
|
63
|
-
end
|
69
|
+
end
|
data/lib/pheromone/version.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.5.
|
4
|
+
version: 0.5.1
|
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-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|