table_sync 4.2.1 → 6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +52 -0
  3. data/CHANGELOG.md +75 -9
  4. data/Gemfile.lock +159 -152
  5. data/README.md +4 -1
  6. data/docs/message_protocol.md +3 -3
  7. data/docs/publishing/configuration.md +143 -0
  8. data/docs/publishing/manual.md +155 -0
  9. data/docs/publishing/publishers.md +162 -0
  10. data/docs/publishing.md +35 -119
  11. data/docs/receiving.md +11 -6
  12. data/lib/table_sync/errors.rb +31 -22
  13. data/lib/table_sync/event.rb +35 -0
  14. data/lib/table_sync/orm_adapter/active_record.rb +25 -0
  15. data/lib/table_sync/orm_adapter/base.rb +92 -0
  16. data/lib/table_sync/orm_adapter/sequel.rb +29 -0
  17. data/lib/table_sync/publishing/batch.rb +53 -0
  18. data/lib/table_sync/publishing/data/objects.rb +50 -0
  19. data/lib/table_sync/publishing/data/raw.rb +27 -0
  20. data/lib/table_sync/publishing/helpers/attributes.rb +63 -0
  21. data/lib/table_sync/publishing/helpers/debounce.rb +134 -0
  22. data/lib/table_sync/publishing/helpers/objects.rb +39 -0
  23. data/lib/table_sync/publishing/message/base.rb +73 -0
  24. data/lib/table_sync/publishing/message/batch.rb +14 -0
  25. data/lib/table_sync/publishing/message/raw.rb +54 -0
  26. data/lib/table_sync/publishing/message/single.rb +13 -0
  27. data/lib/table_sync/publishing/params/base.rb +66 -0
  28. data/lib/table_sync/publishing/params/batch.rb +23 -0
  29. data/lib/table_sync/publishing/params/raw.rb +7 -0
  30. data/lib/table_sync/publishing/params/single.rb +31 -0
  31. data/lib/table_sync/publishing/raw.rb +21 -0
  32. data/lib/table_sync/publishing/single.rb +65 -0
  33. data/lib/table_sync/publishing.rb +20 -5
  34. data/lib/table_sync/receiving/config.rb +6 -4
  35. data/lib/table_sync/receiving/handler.rb +25 -9
  36. data/lib/table_sync/receiving/model/active_record.rb +1 -1
  37. data/lib/table_sync/receiving.rb +0 -2
  38. data/lib/table_sync/setup/active_record.rb +22 -0
  39. data/lib/table_sync/setup/base.rb +67 -0
  40. data/lib/table_sync/setup/sequel.rb +26 -0
  41. data/lib/table_sync/utils/interface_checker.rb +2 -2
  42. data/lib/table_sync/version.rb +1 -1
  43. data/lib/table_sync.rb +31 -8
  44. data/table_sync.gemspec +7 -7
  45. metadata +58 -37
  46. data/.travis.yml +0 -34
  47. data/lib/table_sync/publishing/base_publisher.rb +0 -114
  48. data/lib/table_sync/publishing/batch_publisher.rb +0 -109
  49. data/lib/table_sync/publishing/orm_adapter/active_record.rb +0 -32
  50. data/lib/table_sync/publishing/orm_adapter/sequel.rb +0 -57
  51. 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