pheromone 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +37 -0
- data/lib/pheromone/messaging/message.rb +19 -5
- data/lib/pheromone/messaging/message_dispatcher.rb +1 -0
- data/lib/pheromone/publishable.rb +5 -3
- 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: f73a9670e5afd4662b7b3c951ce47d44e6f4ef0d
|
4
|
+
data.tar.gz: 338faa66fa2406bd8def99618f3438d7ddff0da3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f7a867cd34de612895f1252983c9739cafb7ec07e52a0de7d62f3201c8b3210c6e34cd53195466a92207866ac86e512b14bdcafa84fe8eb5107b34f397206ad
|
7
|
+
data.tar.gz: e45e68c57a2853742e8013fc188a88034fe829c1355a4306509086614950cfc762d44f46f0a963c91c35f36a00c22ee1c55d528d2dfab986ae8d49a1aca5924a
|
data/README.md
CHANGED
@@ -299,6 +299,43 @@ class PublishableModel < ActiveRecord::Base
|
|
299
299
|
end
|
300
300
|
```
|
301
301
|
|
302
|
+
### 7. Sending messages to Kafka directly
|
303
|
+
|
304
|
+
`pheromone` provides a custom message object that sends messages to Kafka in a predefined format, to maintain consistency in the message fields.
|
305
|
+
|
306
|
+
`Pheromone::Messaging::Message` can be initialized with the following arguments:
|
307
|
+
- `topic`: name of the topic to which the message is produced
|
308
|
+
- `message`: the actual message itself
|
309
|
+
- `metadata`: any additional fields that must be sent along with the message
|
310
|
+
- `options`: producer options as described in Section 6
|
311
|
+
|
312
|
+
Of these fields, only `topic` and `message` are compulsory and the remaining two are optional.
|
313
|
+
|
314
|
+
Example usage:
|
315
|
+
|
316
|
+
```
|
317
|
+
Pheromone::Messaging::Message.new(
|
318
|
+
topic: 'test_topic',
|
319
|
+
message: { message_text: 'test' },
|
320
|
+
metadata: { event_type: 'create' },
|
321
|
+
producer_options: { max_retries: 5 }
|
322
|
+
)
|
323
|
+
```
|
324
|
+
|
325
|
+
This will send a message to `test_topic` in Kafka in the following format:
|
326
|
+
|
327
|
+
```
|
328
|
+
{
|
329
|
+
'event_type' => 'create',
|
330
|
+
'timestamp' => '2015-07-14T10:10:00.000+08:00',
|
331
|
+
'blob' => {
|
332
|
+
'message_text' => 'test'
|
333
|
+
}
|
334
|
+
}.to_json
|
335
|
+
```
|
336
|
+
|
337
|
+
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`.
|
338
|
+
|
302
339
|
## Development
|
303
340
|
|
304
341
|
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.
|
@@ -4,16 +4,30 @@ require 'waterdrop'
|
|
4
4
|
module Pheromone
|
5
5
|
module Messaging
|
6
6
|
class Message
|
7
|
-
def initialize(topic:, message:, options: {})
|
7
|
+
def initialize(topic:, message:, metadata: {}, options: {})
|
8
8
|
@topic = topic
|
9
|
-
@message =
|
10
|
-
@options = options
|
9
|
+
@message = message
|
10
|
+
@options = options || {}
|
11
|
+
@metadata = metadata || {}
|
11
12
|
end
|
12
13
|
|
13
|
-
attr_reader :topic, :message, :options
|
14
|
+
attr_reader :topic, :message, :options, :metadata
|
14
15
|
|
15
16
|
def send!
|
16
|
-
::WaterDrop::Message.new(
|
17
|
+
::WaterDrop::Message.new(
|
18
|
+
topic,
|
19
|
+
MessageFormatter.new(full_message).format,
|
20
|
+
options
|
21
|
+
).send!
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def full_message
|
27
|
+
@metadata.merge!(
|
28
|
+
timestamp: Time.now,
|
29
|
+
blob: @message
|
30
|
+
)
|
17
31
|
end
|
18
32
|
end
|
19
33
|
end
|
@@ -66,11 +66,14 @@ module Pheromone
|
|
66
66
|
options[:event_types].any? { |event| event == current_event }
|
67
67
|
end
|
68
68
|
|
69
|
+
# Manages the actual formatting and sending of messages. By default messages are sent in
|
70
|
+
# sync mode. To override this, provide dispatch_method as :async
|
69
71
|
def send_message(options)
|
70
72
|
Pheromone::Messaging::MessageDispatcher.new(
|
71
73
|
message_parameters: {
|
72
74
|
topic: options[:topic],
|
73
|
-
message:
|
75
|
+
message: message_blob(options),
|
76
|
+
metadata: message_meta_data,
|
74
77
|
producer_options: options[:producer_options]
|
75
78
|
},
|
76
79
|
dispatch_method: options[:dispatch_method] || :sync
|
@@ -80,8 +83,7 @@ module Pheromone
|
|
80
83
|
def message_meta_data
|
81
84
|
{
|
82
85
|
event: current_event,
|
83
|
-
entity: self.class.name
|
84
|
-
timestamp: Time.now
|
86
|
+
entity: self.class.name
|
85
87
|
}
|
86
88
|
end
|
87
89
|
|
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.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ankita Gupta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|