table_sync 4.2.1 → 6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +52 -0
- data/CHANGELOG.md +75 -9
- data/Gemfile.lock +159 -152
- data/README.md +4 -1
- data/docs/message_protocol.md +3 -3
- data/docs/publishing/configuration.md +143 -0
- data/docs/publishing/manual.md +155 -0
- data/docs/publishing/publishers.md +162 -0
- data/docs/publishing.md +35 -119
- data/docs/receiving.md +11 -6
- data/lib/table_sync/errors.rb +31 -22
- data/lib/table_sync/event.rb +35 -0
- data/lib/table_sync/orm_adapter/active_record.rb +25 -0
- data/lib/table_sync/orm_adapter/base.rb +92 -0
- data/lib/table_sync/orm_adapter/sequel.rb +29 -0
- data/lib/table_sync/publishing/batch.rb +53 -0
- data/lib/table_sync/publishing/data/objects.rb +50 -0
- data/lib/table_sync/publishing/data/raw.rb +27 -0
- data/lib/table_sync/publishing/helpers/attributes.rb +63 -0
- data/lib/table_sync/publishing/helpers/debounce.rb +134 -0
- data/lib/table_sync/publishing/helpers/objects.rb +39 -0
- data/lib/table_sync/publishing/message/base.rb +73 -0
- data/lib/table_sync/publishing/message/batch.rb +14 -0
- data/lib/table_sync/publishing/message/raw.rb +54 -0
- data/lib/table_sync/publishing/message/single.rb +13 -0
- data/lib/table_sync/publishing/params/base.rb +66 -0
- data/lib/table_sync/publishing/params/batch.rb +23 -0
- data/lib/table_sync/publishing/params/raw.rb +7 -0
- data/lib/table_sync/publishing/params/single.rb +31 -0
- data/lib/table_sync/publishing/raw.rb +21 -0
- data/lib/table_sync/publishing/single.rb +65 -0
- data/lib/table_sync/publishing.rb +20 -5
- data/lib/table_sync/receiving/config.rb +6 -4
- data/lib/table_sync/receiving/handler.rb +25 -9
- data/lib/table_sync/receiving/model/active_record.rb +1 -1
- data/lib/table_sync/receiving.rb +0 -2
- data/lib/table_sync/setup/active_record.rb +22 -0
- data/lib/table_sync/setup/base.rb +67 -0
- data/lib/table_sync/setup/sequel.rb +26 -0
- data/lib/table_sync/utils/interface_checker.rb +2 -2
- data/lib/table_sync/version.rb +1 -1
- data/lib/table_sync.rb +31 -8
- data/table_sync.gemspec +7 -7
- metadata +58 -37
- data/.travis.yml +0 -34
- data/lib/table_sync/publishing/base_publisher.rb +0 -114
- data/lib/table_sync/publishing/batch_publisher.rb +0 -109
- data/lib/table_sync/publishing/orm_adapter/active_record.rb +0 -32
- data/lib/table_sync/publishing/orm_adapter/sequel.rb +0 -57
- data/lib/table_sync/publishing/publisher.rb +0 -129
@@ -1,129 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class TableSync::Publishing::Publisher < TableSync::Publishing::BasePublisher
|
4
|
-
DEBOUNCE_TIME = 1.minute
|
5
|
-
|
6
|
-
# 'original_attributes' are not published, they are used to resolve the routing key
|
7
|
-
def initialize(object_class, original_attributes, **opts)
|
8
|
-
@object_class = object_class.constantize
|
9
|
-
@original_attributes = filter_safe_for_serialization(original_attributes.deep_symbolize_keys)
|
10
|
-
@confirm = opts.fetch(:confirm, true)
|
11
|
-
@debounce_time = opts[:debounce_time]&.seconds || DEBOUNCE_TIME
|
12
|
-
|
13
|
-
if opts[:destroyed].nil?
|
14
|
-
@state = opts.fetch(:state, :updated).to_sym
|
15
|
-
validate_state
|
16
|
-
else
|
17
|
-
# TODO Legacy job support, remove
|
18
|
-
@state = opts[:destroyed] ? :destroyed : :updated
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def publish
|
23
|
-
return enqueue_job if destroyed? || debounce_time.zero?
|
24
|
-
|
25
|
-
sync_time = Rails.cache.read(cache_key) || current_time - debounce_time - 1.second
|
26
|
-
return if sync_time > current_time
|
27
|
-
|
28
|
-
next_sync_time = sync_time + debounce_time
|
29
|
-
next_sync_time <= current_time ? enqueue_job : enqueue_job(next_sync_time)
|
30
|
-
end
|
31
|
-
|
32
|
-
def publish_now
|
33
|
-
# Update request and object does not exist -> skip publishing
|
34
|
-
return if !object && !destroyed?
|
35
|
-
|
36
|
-
Rabbit.publish(params)
|
37
|
-
model_naming = TableSync.publishing_adapter.model_naming(object_class)
|
38
|
-
TableSync::Instrument.notify table: model_naming.table, schema: model_naming.schema,
|
39
|
-
event: event, direction: :publish
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
attr_reader :original_attributes
|
45
|
-
attr_reader :state
|
46
|
-
attr_reader :debounce_time
|
47
|
-
|
48
|
-
def attrs_for_callables
|
49
|
-
original_attributes
|
50
|
-
end
|
51
|
-
|
52
|
-
def attrs_for_routing_key
|
53
|
-
return object.attrs_for_routing_key if attrs_for_routing_key_defined?
|
54
|
-
attrs_for_callables
|
55
|
-
end
|
56
|
-
|
57
|
-
def attrs_for_metadata
|
58
|
-
return object.attrs_for_metadata if attrs_for_metadata_defined?
|
59
|
-
attrs_for_callables
|
60
|
-
end
|
61
|
-
|
62
|
-
def job_callable
|
63
|
-
TableSync.publishing_job_class_callable
|
64
|
-
end
|
65
|
-
|
66
|
-
def job_callable_error_message
|
67
|
-
"Can't publish, set TableSync.publishing_job_class_callable"
|
68
|
-
end
|
69
|
-
|
70
|
-
def enqueue_job(perform_at = current_time)
|
71
|
-
job = job_class.set(wait_until: perform_at)
|
72
|
-
job.perform_later(object_class.name, original_attributes, state: state.to_s, confirm: confirm?)
|
73
|
-
Rails.cache.write(cache_key, perform_at)
|
74
|
-
end
|
75
|
-
|
76
|
-
def routing_key
|
77
|
-
resolve_routing_key
|
78
|
-
end
|
79
|
-
|
80
|
-
def publishing_data
|
81
|
-
{
|
82
|
-
**super,
|
83
|
-
event: event,
|
84
|
-
metadata: { created: created? },
|
85
|
-
}
|
86
|
-
end
|
87
|
-
|
88
|
-
def attributes_for_sync
|
89
|
-
if destroyed?
|
90
|
-
if object_class.respond_to?(:table_sync_destroy_attributes)
|
91
|
-
object_class.table_sync_destroy_attributes(original_attributes)
|
92
|
-
else
|
93
|
-
needle
|
94
|
-
end
|
95
|
-
elsif attributes_for_sync_defined?
|
96
|
-
object.attributes_for_sync
|
97
|
-
else
|
98
|
-
TableSync.publishing_adapter.attributes(object)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
memoize def object
|
103
|
-
TableSync.publishing_adapter.find(object_class, needle)
|
104
|
-
end
|
105
|
-
|
106
|
-
def event
|
107
|
-
destroyed? ? :destroy : :update
|
108
|
-
end
|
109
|
-
|
110
|
-
def needle
|
111
|
-
original_attributes.slice(*primary_keys)
|
112
|
-
end
|
113
|
-
|
114
|
-
def cache_key
|
115
|
-
"#{object_class}/#{needle}_table_sync_time".delete(" ")
|
116
|
-
end
|
117
|
-
|
118
|
-
def destroyed?
|
119
|
-
state == :destroyed
|
120
|
-
end
|
121
|
-
|
122
|
-
def created?
|
123
|
-
state == :created
|
124
|
-
end
|
125
|
-
|
126
|
-
def validate_state
|
127
|
-
raise "Unknown state: #{state.inspect}" unless %i[created updated destroyed].include?(state)
|
128
|
-
end
|
129
|
-
end
|