pub_sub_model_sync 0.5.8.2 → 0.5.9
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/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +19 -1
- data/lib/pub_sub_model_sync/message_processor.rb +10 -6
- data/lib/pub_sub_model_sync/message_publisher.rb +21 -6
- data/lib/pub_sub_model_sync/payload.rb +4 -6
- data/lib/pub_sub_model_sync/publisher.rb +6 -1
- data/lib/pub_sub_model_sync/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb036497d0e5f977a99a7a4b18b766ae910ed8f72775deac8b5e4d862277085b
|
4
|
+
data.tar.gz: 3eac2629bf814e169199bd95fece47eaf032d10a7a2e311a81e46d50255947b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e34579693469d286ed7fdb6346ac4e4bdf3c000f98d96a4a4b143a0c9c71b3d5df7c441e9069bea4661f5ac7ea8bf2db41f5ad8c55e41a66585602a4bb3d275
|
7
|
+
data.tar.gz: f13e18fe535720ca299dcd65f76a24c3aae44df0dd3e2515adf5c3095f21d19ad5f16246447d7b15f4c03867495c36b18869cde78e6fbaec573493d340890db6
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
# 0.5.9 (February 10, 2021)
|
4
|
+
- feat: reformat :publish and :process methods to include non silence methods
|
5
|
+
- feat: add notification key to payloads (can be used for caching strategies)
|
6
|
+
|
3
7
|
# 0.5.8.2 (February 05, 2021)
|
4
8
|
- fix: restore google pubsub topic settings
|
9
|
+
|
5
10
|
# 0.5.8.1 (February 05, 2021)
|
6
11
|
- fix: keep message ordering with google pubsub
|
7
12
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -50,7 +50,19 @@ And then execute: $ bundle install
|
|
50
50
|
PubSubModelSync::Config.kafka_connection = [["kafka1:9092", "localhost:2121"], { logger: Rails.logger }]
|
51
51
|
PubSubModelSync::Config.topic_name = 'sample-topic'
|
52
52
|
```
|
53
|
-
See details here: https://github.com/zendesk/ruby-kafka
|
53
|
+
See details here: https://github.com/zendesk/ruby-kafka
|
54
|
+
|
55
|
+
Kafka Confluence example:
|
56
|
+
```ruby
|
57
|
+
kafka_settings = {
|
58
|
+
sasl_plain_username: '...',
|
59
|
+
sasl_plain_password: '...',
|
60
|
+
ssl_ca_certs_from_system: true,
|
61
|
+
logger: Rails.logger, client_id: 'my-app-name'
|
62
|
+
}
|
63
|
+
PubSubModelSync::Config.kafka_connection = [['...confluent.cloud:9092'], kafka_settings]
|
64
|
+
```
|
65
|
+
Note: You need to create the topic manually on Kafka Confluence
|
54
66
|
|
55
67
|
- Add publishers/subscribers to your models (See examples below)
|
56
68
|
|
@@ -315,6 +327,12 @@ config.debug = true
|
|
315
327
|
PubSubModelSync::MessagePublisher.batch_publish({ same_keys: :use_last_as_first|:use_last|:use_first_as_last|:keep*, same_data: :use_last_as_first*|:use_last|:use_first_as_last|:keep })
|
316
328
|
- Add DB table to use as a shield to skip publishing similar notifications or publish partial notifications (similar idea when processing notif)
|
317
329
|
- add callback: on_message_received(payload)
|
330
|
+
- Sometimes the listener service stops listening, debug idea:
|
331
|
+
```subscriber.on_error do
|
332
|
+
log('error gogooogle')
|
333
|
+
end
|
334
|
+
loop{ log(['loooooop:', {l: subscriber.last_error, stopped: subscriber.stopped?, started: subscriber.started?}]); sleep 2 }
|
335
|
+
```
|
318
336
|
|
319
337
|
## Q&A
|
320
338
|
- Error "could not obtain a connection from the pool within 5.000 seconds"
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module PubSubModelSync
|
4
4
|
class MessageProcessor < PubSubModelSync::Base
|
5
|
-
attr_accessor :payload
|
5
|
+
attr_accessor :payload
|
6
6
|
|
7
7
|
# @param payload (Payload): payload to be delivered
|
8
8
|
# @Deprecated: def initialize(data, klass, action)
|
@@ -15,10 +15,16 @@ module PubSubModelSync
|
|
15
15
|
@payload = PubSubModelSync::Payload.new(payload, { klass: klass, action: action })
|
16
16
|
end
|
17
17
|
|
18
|
-
def process
|
18
|
+
def process!
|
19
19
|
filter_subscribers.each(&method(:run_subscriber))
|
20
20
|
end
|
21
21
|
|
22
|
+
def process
|
23
|
+
process!
|
24
|
+
rescue => e
|
25
|
+
notify_error(e)
|
26
|
+
end
|
27
|
+
|
22
28
|
private
|
23
29
|
|
24
30
|
def run_subscriber(subscriber)
|
@@ -29,8 +35,6 @@ module PubSubModelSync
|
|
29
35
|
res = config.on_success_processing.call(payload, { subscriber: subscriber })
|
30
36
|
log "processed message with: #{payload.inspect}" if res != :skip_log
|
31
37
|
end
|
32
|
-
rescue => e
|
33
|
-
raise_error ? raise : print_subscriber_error(e, subscriber)
|
34
38
|
end
|
35
39
|
|
36
40
|
def processable?(subscriber)
|
@@ -40,9 +44,9 @@ module PubSubModelSync
|
|
40
44
|
end
|
41
45
|
|
42
46
|
# @param error (Error)
|
43
|
-
def
|
47
|
+
def notify_error(error)
|
44
48
|
info = [payload, error.message, error.backtrace]
|
45
|
-
res = config.on_error_processing.call(error, { payload: payload
|
49
|
+
res = config.on_error_processing.call(error, { payload: payload })
|
46
50
|
log("Error processing message: #{info}", :error) if res != :skip_log
|
47
51
|
end
|
48
52
|
|
@@ -7,14 +7,20 @@ module PubSubModelSync
|
|
7
7
|
@connector ||= PubSubModelSync::Connector.new
|
8
8
|
end
|
9
9
|
|
10
|
+
# Publishes any value to pubsub
|
11
|
+
# @param klass (String): Class name
|
12
|
+
# @param data (Hash): Data to be delivered
|
13
|
+
# @param action (:symbol): action name
|
10
14
|
def publish_data(klass, data, action)
|
11
|
-
|
15
|
+
attrs = { klass: klass.to_s, action: action.to_sym, key: [klass.to_s, action].join('/') }
|
16
|
+
payload = PubSubModelSync::Payload.new(data, attrs)
|
12
17
|
publish(payload)
|
13
18
|
end
|
14
19
|
|
15
|
-
#
|
16
|
-
# @param
|
17
|
-
# @param
|
20
|
+
# Publishes model info to pubsub
|
21
|
+
# @param model (ActiveRecord model)
|
22
|
+
# @param action (Sym): Action name
|
23
|
+
# @param publisher (Publisher, optional): Publisher to be used
|
18
24
|
def publish_model(model, action, publisher = nil)
|
19
25
|
return if model.ps_skip_sync?(action)
|
20
26
|
|
@@ -27,7 +33,10 @@ module PubSubModelSync
|
|
27
33
|
model.ps_after_sync(action, payload.data)
|
28
34
|
end
|
29
35
|
|
30
|
-
|
36
|
+
# Publishes payload to pubsub
|
37
|
+
# @attr payload (PubSubModelSync::Payload)
|
38
|
+
# Raises error if exist
|
39
|
+
def publish!(payload)
|
31
40
|
if config.on_before_publish.call(payload) == :cancel
|
32
41
|
log("Publish message cancelled: #{payload}") if config.debug
|
33
42
|
return
|
@@ -36,8 +45,14 @@ module PubSubModelSync
|
|
36
45
|
log("Publishing message: #{[payload]}")
|
37
46
|
connector.publish(payload)
|
38
47
|
config.on_after_publish.call(payload)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Similar to :publish! method
|
51
|
+
# Notifies error via :on_error_publish instead of raising error
|
52
|
+
def publish(payload)
|
53
|
+
publish!(payload)
|
39
54
|
rescue => e
|
40
|
-
|
55
|
+
notify_error(e, payload)
|
41
56
|
end
|
42
57
|
|
43
58
|
private
|
@@ -6,7 +6,7 @@ module PubSubModelSync
|
|
6
6
|
attr_reader :data, :attributes, :headers
|
7
7
|
|
8
8
|
# @param data (Hash: { any value }):
|
9
|
-
# @param attributes (Hash: { klass
|
9
|
+
# @param attributes (Hash: { klass*: string, action*: :sym, key?: string }):
|
10
10
|
def initialize(data, attributes, headers = {})
|
11
11
|
@data = data
|
12
12
|
@attributes = attributes
|
@@ -31,16 +31,14 @@ module PubSubModelSync
|
|
31
31
|
# Process payload data
|
32
32
|
# (If error will raise exception and wont call on_error_processing callback)
|
33
33
|
def process!
|
34
|
-
|
35
|
-
|
36
|
-
end
|
34
|
+
publisher = PubSubModelSync::MessageProcessor.new(self)
|
35
|
+
publisher.process!
|
37
36
|
end
|
38
37
|
|
39
38
|
# Process payload data
|
40
39
|
# (If error will call on_error_processing callback)
|
41
40
|
def process
|
42
41
|
publisher = PubSubModelSync::MessageProcessor.new(self)
|
43
|
-
yield(publisher) if block_given?
|
44
42
|
publisher.process
|
45
43
|
end
|
46
44
|
|
@@ -48,7 +46,7 @@ module PubSubModelSync
|
|
48
46
|
# (If error will raise exception and wont call on_error_publish callback)
|
49
47
|
def publish!
|
50
48
|
klass = PubSubModelSync::MessagePublisher
|
51
|
-
klass.publish(self
|
49
|
+
klass.publish!(self)
|
52
50
|
end
|
53
51
|
|
54
52
|
# Publish payload to pubsub
|
@@ -11,6 +11,7 @@ module PubSubModelSync
|
|
11
11
|
@as_klass = as_klass || klass
|
12
12
|
end
|
13
13
|
|
14
|
+
# Builds the payload with model information defined for :action (:create|:update|:destroy)
|
14
15
|
def payload(model, action)
|
15
16
|
PubSubModelSync::Payload.new(payload_data(model), payload_attrs(model, action))
|
16
17
|
end
|
@@ -29,7 +30,11 @@ module PubSubModelSync
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def payload_attrs(model, action)
|
32
|
-
{
|
33
|
+
{
|
34
|
+
klass: (as_klass || model.class.name).to_s,
|
35
|
+
action: action.to_sym,
|
36
|
+
key: [model.class.name, action, model.id].join('/')
|
37
|
+
}
|
33
38
|
end
|
34
39
|
end
|
35
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pub_sub_model_sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-02-
|
11
|
+
date: 2021-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|