pub_sub_model_sync 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +12 -12
- data/lib/pub_sub_model_sync/config.rb +5 -1
- data/lib/pub_sub_model_sync/message_processor.rb +1 -1
- data/lib/pub_sub_model_sync/publisher.rb +1 -1
- data/lib/pub_sub_model_sync/publisher_concern.rb +12 -12
- data/lib/pub_sub_model_sync/service_google.rb +3 -3
- data/lib/pub_sub_model_sync/service_rabbit.rb +6 -5
- data/lib/pub_sub_model_sync/subscriber_concern.rb +8 -8
- 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: 1134a868bdf5d405f2cd05dbca21806c6cc3c8240ae5951f8a26146f2f98917a
|
4
|
+
data.tar.gz: 3d2c96dfd3155827f178ef824074f84a5e1be04408cbaa2948c6536ea6b34e48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb61cce4b0170bb86137a7f51452c703804e2eb4523ec5729eb8a77fabd768e215a9029f200795a2662124621c2b5aa5d77fdfbdecdcafb1d380e3524fa8eb34
|
7
|
+
data.tar.gz: d6a6463def886802bd5a353468adfaa12d1a5d94885d978e85c65f26b03b4db1a05586a8dd051c780782f7dcdb2f57770a6b482c663df880cd40d2f75acd3515
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -57,14 +57,14 @@ And then execute: $ bundle install
|
|
57
57
|
# attributes: name email age
|
58
58
|
class User < ActiveRecord::Base
|
59
59
|
include PubSubModelSync::PublisherConcern
|
60
|
-
|
60
|
+
ps_publish(%i[name email])
|
61
61
|
end
|
62
62
|
|
63
63
|
# App 2
|
64
64
|
class User < ActiveRecord::Base
|
65
65
|
include PubSubModelSync::SubscriberConcern
|
66
|
-
|
67
|
-
|
66
|
+
ps_subscribe(%i[name])
|
67
|
+
ps_class_subscribe(:greeting)
|
68
68
|
|
69
69
|
def self.greeting(data)
|
70
70
|
puts 'Class message called'
|
@@ -73,7 +73,7 @@ end
|
|
73
73
|
|
74
74
|
# Samples
|
75
75
|
User.create(name: 'test user') # Review your App 2 to see the created user (only name will be saved)
|
76
|
-
User.
|
76
|
+
User.ps_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting method (Class method) will be called in App2
|
77
77
|
```
|
78
78
|
|
79
79
|
## Advanced Example
|
@@ -82,9 +82,9 @@ User.ps_msync_class_publish({ msg: 'Hello' }, action: :greeting) # User.greeting
|
|
82
82
|
class User < ActiveRecord::Base
|
83
83
|
self.table_name = 'publisher_users'
|
84
84
|
include PubSubModelSync::PublisherConcern
|
85
|
-
|
85
|
+
ps_publish(%i[name], actions: %i[update], as_klass: 'Client', id: :client_id)
|
86
86
|
|
87
|
-
def
|
87
|
+
def ps_skip_for?(_action)
|
88
88
|
false # here logic with action to skip push message
|
89
89
|
end
|
90
90
|
end
|
@@ -93,8 +93,8 @@ end
|
|
93
93
|
class User < ActiveRecord::Base
|
94
94
|
self.table_name = 'subscriber_users'
|
95
95
|
include PubSubModelSync::SubscriberConcern
|
96
|
-
|
97
|
-
|
96
|
+
ps_subscribe(%i[name], actions: %i[update], as_klass: 'Client', id: :custom_id)
|
97
|
+
ps_class_subscribe(:greeting, as_action: :custom_greeting, as_klass: 'CustomUser')
|
98
98
|
|
99
99
|
def self.greeting(data)
|
100
100
|
puts 'Class message called through custom_greeting'
|
@@ -147,7 +147,7 @@ end
|
|
147
147
|
publisher = PubSubModelSync::Publisher
|
148
148
|
data = { name: 'hello'}
|
149
149
|
action = :create
|
150
|
-
User.
|
150
|
+
User.ps_class_publish(data, action: action)
|
151
151
|
user = User.create(name: 'name', email: 'email')
|
152
152
|
expect_any_instance_of(publisher).to receive(:publish_model).with(user, :create, anything)
|
153
153
|
end
|
@@ -156,16 +156,16 @@ end
|
|
156
156
|
publisher = PubSubModelSync::Publisher
|
157
157
|
data = {msg: 'hello'}
|
158
158
|
action = :greeting
|
159
|
-
User.
|
159
|
+
User.ps_class_publish(data, action: action)
|
160
160
|
expect_any_instance_of(publisher).to receive(:publish_data).with('User', data, action)
|
161
161
|
end
|
162
162
|
```
|
163
163
|
|
164
164
|
There are two special methods to extract crud configuration settings (attrs, id, ...):
|
165
165
|
|
166
|
-
Subscribers: ```User.
|
166
|
+
Subscribers: ```User.ps_subscriber_settings```
|
167
167
|
|
168
|
-
Publishers: ```User.
|
168
|
+
Publishers: ```User.ps_publisher_settings```
|
169
169
|
|
170
170
|
Note: Inspect all configured listeners with:
|
171
171
|
``` PubSubModelSync::Config.listeners ```
|
@@ -14,7 +14,11 @@ module PubSubModelSync
|
|
14
14
|
|
15
15
|
def self.log(msg, kind = :info)
|
16
16
|
msg = "PS_MSYNC ==> #{msg}"
|
17
|
-
|
17
|
+
if logger == :raise_error
|
18
|
+
kind == :error ? raise(msg) : puts(msg)
|
19
|
+
else
|
20
|
+
logger ? logger.send(kind, msg) : puts(msg)
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
@@ -76,7 +76,7 @@ module PubSubModelSync
|
|
76
76
|
|
77
77
|
def listener_add_crud_settings(listener)
|
78
78
|
model_class = listener[:klass].constantize
|
79
|
-
listener[:settings] = model_class.
|
79
|
+
listener[:settings] = model_class.ps_subscriber_settings
|
80
80
|
end
|
81
81
|
|
82
82
|
def log(message, kind = :info)
|
@@ -14,7 +14,7 @@ module PubSubModelSync
|
|
14
14
|
|
15
15
|
# @param settings (Hash): { attrs: [], as_klass: nil, id: nil }
|
16
16
|
def publish_model(model, action, settings = nil)
|
17
|
-
settings ||= model.class.
|
17
|
+
settings ||= model.class.ps_publisher_settings
|
18
18
|
attributes = build_model_attrs(model, action, settings)
|
19
19
|
data = {}
|
20
20
|
if action != 'destroy'
|
@@ -7,39 +7,39 @@ module PubSubModelSync
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# Permit to skip a publish callback
|
10
|
-
def
|
10
|
+
def ps_skip_for?(_action)
|
11
11
|
false
|
12
12
|
end
|
13
13
|
|
14
14
|
module ClassMethods
|
15
15
|
# Permit to publish crud actions (:create, :update, :destroy)
|
16
16
|
# @param settings (Hash): { actions: nil, as_klass: nil, id: nil }
|
17
|
-
def
|
17
|
+
def ps_publish(attrs, settings = {})
|
18
18
|
actions = settings.delete(:actions) || %i[create update destroy]
|
19
|
-
@
|
20
|
-
|
19
|
+
@ps_publisher_settings = settings.merge(attrs: attrs)
|
20
|
+
ps_register_callbacks(actions)
|
21
21
|
end
|
22
22
|
|
23
|
-
def
|
24
|
-
@
|
23
|
+
def ps_publisher_settings
|
24
|
+
@ps_publisher_settings
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def ps_class_publish(data, action:, as_klass: nil)
|
28
28
|
as_klass = (as_klass || name).to_s
|
29
|
-
|
29
|
+
ps_publisher.publish_data(as_klass, data, action.to_sym)
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def ps_publisher
|
33
33
|
PubSubModelSync::Publisher.new
|
34
34
|
end
|
35
35
|
|
36
36
|
private
|
37
37
|
|
38
|
-
def
|
38
|
+
def ps_register_callbacks(actions)
|
39
39
|
actions.each do |action|
|
40
40
|
after_commit(on: action) do |model|
|
41
|
-
unless model.
|
42
|
-
publisher = model.class.
|
41
|
+
unless model.ps_skip_for?(action)
|
42
|
+
publisher = model.class.ps_publisher
|
43
43
|
publisher.publish_model(model, action.to_sym)
|
44
44
|
end
|
45
45
|
end
|
@@ -54,13 +54,13 @@ module PubSubModelSync
|
|
54
54
|
args = [data, attrs[:klass], attrs[:action], attrs]
|
55
55
|
PubSubModelSync::MessageProcessor.new(*args).process
|
56
56
|
rescue => e
|
57
|
-
log("Error processing message: #{[received_message, e.message]}")
|
57
|
+
log("Error processing message: #{[received_message, e.message]}", :error)
|
58
58
|
ensure
|
59
59
|
received_message.acknowledge!
|
60
60
|
end
|
61
61
|
|
62
|
-
def log(msg)
|
63
|
-
config.log("Google Service ==> #{msg}")
|
62
|
+
def log(msg, kind = :info)
|
63
|
+
config.log("Google Service ==> #{msg}", kind)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
@@ -24,7 +24,7 @@ module PubSubModelSync
|
|
24
24
|
rescue PubSubModelSync::Runner::ShutDown
|
25
25
|
raise
|
26
26
|
rescue => e
|
27
|
-
log("Error listening message: #{[e.message, e.backtrace]}")
|
27
|
+
log("Error listening message: #{[e.message, e.backtrace]}", :error)
|
28
28
|
end
|
29
29
|
|
30
30
|
def publish(data, attributes)
|
@@ -33,7 +33,8 @@ module PubSubModelSync
|
|
33
33
|
payload = { data: data, attributes: attributes }
|
34
34
|
topic.publish(payload.to_json, routing_key: queue.name, type: SERVICE_KEY)
|
35
35
|
rescue => e
|
36
|
-
|
36
|
+
info = [data, attributes, e.message, e.backtrace]
|
37
|
+
log("Error publishing: #{info}", :error)
|
37
38
|
end
|
38
39
|
|
39
40
|
def stop
|
@@ -51,7 +52,7 @@ module PubSubModelSync
|
|
51
52
|
PubSubModelSync::MessageProcessor.new(*args).process
|
52
53
|
rescue => e
|
53
54
|
error = [payload, e.message, e.backtrace]
|
54
|
-
log("Error processing message: #{error}")
|
55
|
+
log("Error processing message: #{error}", :error)
|
55
56
|
end
|
56
57
|
|
57
58
|
def parse_message_payload(payload)
|
@@ -74,8 +75,8 @@ module PubSubModelSync
|
|
74
75
|
queue.bind(topic, routing_key: queue.name)
|
75
76
|
end
|
76
77
|
|
77
|
-
def log(msg)
|
78
|
-
config.log("Rabbit Service ==> #{msg}")
|
78
|
+
def log(msg, kind = :info)
|
79
|
+
config.log("Rabbit Service ==> #{msg}", kind)
|
79
80
|
end
|
80
81
|
end
|
81
82
|
end
|
@@ -8,26 +8,26 @@ module PubSubModelSync
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
# @param settings (Hash): { as_klass: nil, actions: nil, id: nil }
|
11
|
-
def
|
11
|
+
def ps_subscribe(attrs, settings = {})
|
12
12
|
settings[:as_klass] = (settings[:as_klass] || name).to_s
|
13
13
|
actions = settings.delete(:actions) || %i[create update destroy]
|
14
|
-
@
|
14
|
+
@ps_subscriber_settings = { attrs: attrs }.merge(settings)
|
15
15
|
actions.each do |action|
|
16
|
-
|
16
|
+
add_ps_subscriber(settings[:as_klass], action, action, false)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
|
20
|
+
def ps_class_subscribe(action, as_action: nil, as_klass: nil)
|
21
|
+
add_ps_subscriber(as_klass, action, as_action, true)
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
25
|
-
@
|
24
|
+
def ps_subscriber_settings
|
25
|
+
@ps_subscriber_settings || {}
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def
|
30
|
+
def add_ps_subscriber(as_klass, action, as_action, direct_mode)
|
31
31
|
listener = {
|
32
32
|
klass: name,
|
33
33
|
as_klass: (as_klass || name).to_s,
|
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.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|