table_sync 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c6441900e1a2c6f9605cff9034b5c393e5aa692f69eac0219ba2870cc780f94
4
- data.tar.gz: cbd3cd0cbc388c08ac8cda0ddc9106e28972dd3750540e8aa345168904b1e795
3
+ metadata.gz: cfa9b3d17de49a2591644329464d45ba1ddfc6b9a56c76db6eb6c061b439f3e4
4
+ data.tar.gz: 7438f913929bd7eb6ae0b6e2d22fcb145b41a62d714aba7e989dcf2868bc7d70
5
5
  SHA512:
6
- metadata.gz: f31202d84b064ea297774c22169ea7c1db9d9048010b3db3fe2de2742e568e2c8fabb3a84c35c124c459eab96b475b4fa30f978bdff9145dcdce15c7fe053a21
7
- data.tar.gz: ba901b5c83cf12406556b5d60e5a5df7c1d2c62ca112c2fcaf5c3e1d2132179b27dcf8d6f5ad241342bc02f45f9c02b9bab034cd7ff60c5ef449ccb675bdae8e
6
+ metadata.gz: ed0babc48e2b1918ba4ae126215083182c6a3eadec3f79136b68322e2d2563ef02871b82aa86d5f310c77bc8303fc6a38042d35dfba95dab71b7bea653bfb658
7
+ data.tar.gz: 11783a0f991fb7d3b39b18e820a82a3075e0f6690ab0bd0d1c5fcb229fa2a0090df673ceacc4e3ec05bc722a42f8657fdd376f18f8f715f27c838efdde5c357b
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [1.8.0] - 2019-07-23
5
+ ### Added
6
+ - `debounce_time` option for publishing
7
+
4
8
  ## [1.7.0] - 2019-07-11
5
9
  ### Added
6
10
  - `on_destroy` return value for manipulation with `after_commit` callback;
@@ -82,8 +82,8 @@ to default Rabbit gem configuration).
82
82
 
83
83
  # Manual publishing
84
84
 
85
- `TableSync::Publisher.new(object_class, original_attributes, confirm: true, state: :updated)` where
86
- state is one of `:created / :updated / :destroyed` and `confirm` is Rabbit's confirm delivery flag
85
+ `TableSync::Publisher.new(object_class, original_attributes, confirm: true, state: :updated, debounce_time: 45)`
86
+ where state is one of `:created / :updated / :destroyed` and `confirm` is Rabbit's confirm delivery flag and optional param `debounce_time` determines debounce time in seconds, 1 minute by default.
87
87
 
88
88
  # Manual publishing with batches
89
89
 
@@ -17,10 +17,13 @@ module TableSync::ORMAdapter
17
17
  end
18
18
 
19
19
  def setup_sync(klass, **opts)
20
+ debounce_time = opts.delete(:debounce_time)
21
+
20
22
  klass.instance_exec do
21
23
  { create: :created, update: :updated, destroy: :destroyed }.each do |event, state|
22
24
  after_commit(on: event, **opts) do
23
- TableSync::Publisher.new(self.class.name, attributes, state: state).publish
25
+ TableSync::Publisher.new(self.class.name, attributes,
26
+ state: state, debounce_time: debounce_time).publish
24
27
  end
25
28
  end
26
29
  end
@@ -19,13 +19,29 @@ module TableSync::ORMAdapter
19
19
  def setup_sync(klass, **opts)
20
20
  if_predicate = to_predicate(opts.delete(:if), true)
21
21
  unless_predicate = to_predicate(opts.delete(:unless), false)
22
- raise "Only :if and :unless options are currently supported for Sequel hooks" if opts.any?
22
+ debounce_time = opts.delete(:debounce_time)
23
23
 
24
+ if opts.any?
25
+ raise "Only :if, :skip_debounce and :unless options are currently " \
26
+ "supported for Sequel hooks"
27
+ end
28
+
29
+ register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
30
+ end
31
+
32
+ def to_predicate(val, default)
33
+ return val.to_proc if val.respond_to?(:to_proc)
34
+
35
+ -> (*) { default }
36
+ end
37
+
38
+ def register_callbacks(klass, if_predicate, unless_predicate, debounce_time)
24
39
  { create: :created, update: :updated }.each do |event, state|
25
40
  klass.send(:define_method, :"after_#{event}") do
26
41
  if instance_eval(&if_predicate) && !instance_eval(&unless_predicate)
27
42
  db.after_commit do
28
- TableSync::Publisher.new(self.class.name, values, state: state).publish
43
+ TableSync::Publisher.new(self.class.name, values,
44
+ state: state, debounce_time: debounce_time).publish
29
45
  end
30
46
  end
31
47
  super()
@@ -40,11 +56,5 @@ module TableSync::ORMAdapter
40
56
  super()
41
57
  end
42
58
  end
43
-
44
- def to_predicate(val, default)
45
- return val.to_proc if val.respond_to?(:to_proc)
46
-
47
- -> (*) { default }
48
- end
49
59
  end
50
60
  end
@@ -4,26 +4,28 @@ class TableSync::Publisher < TableSync::BasePublisher
4
4
  DEBOUNCE_TIME = 1.minute
5
5
 
6
6
  # 'original_attributes' are not published, they are used to resolve the routing key
7
- def initialize(object_class, original_attributes, destroyed: nil, confirm: true, state: :updated)
7
+ def initialize(object_class, original_attributes, **opts)
8
8
  @object_class = object_class.constantize
9
9
  @original_attributes = filter_safe_for_serialization(original_attributes.deep_symbolize_keys)
10
- @confirm = confirm
10
+ @confirm = opts.fetch(:confirm, true)
11
+ @debounce_time = opts[:debounce_time]&.seconds || DEBOUNCE_TIME
11
12
 
12
- if destroyed.nil?
13
- @state = validate_state(state)
13
+ if opts[:destroyed].nil?
14
+ @state = opts.fetch(:state, :updated).to_sym
15
+ validate_state
14
16
  else
15
17
  # TODO Legacy job support, remove
16
- @state = destroyed ? :destroyed : :updated
18
+ @state = opts[:destroyed] ? :destroyed : :updated
17
19
  end
18
20
  end
19
21
 
20
22
  def publish
21
- return enqueue_job if destroyed?
23
+ return enqueue_job if destroyed? || debounce_time.zero?
22
24
 
23
- sync_time = Rails.cache.read(cache_key) || current_time - DEBOUNCE_TIME - 1.second
25
+ sync_time = Rails.cache.read(cache_key) || current_time - debounce_time - 1.second
24
26
  return if sync_time > current_time
25
27
 
26
- next_sync_time = sync_time + DEBOUNCE_TIME
28
+ next_sync_time = sync_time + debounce_time
27
29
  next_sync_time <= current_time ? enqueue_job : enqueue_job(next_sync_time)
28
30
  end
29
31
 
@@ -38,6 +40,7 @@ class TableSync::Publisher < TableSync::BasePublisher
38
40
 
39
41
  attr_reader :original_attributes
40
42
  attr_reader :state
43
+ attr_reader :debounce_time
41
44
 
42
45
  def attrs_for_callables
43
46
  original_attributes
@@ -113,11 +116,7 @@ class TableSync::Publisher < TableSync::BasePublisher
113
116
  state == :created
114
117
  end
115
118
 
116
- def validate_state(state)
117
- if %i[created updated destroyed].include?(state&.to_sym)
118
- state.to_sym
119
- else
120
- raise "Unknown state: #{state.inspect}"
121
- end
119
+ def validate_state
120
+ raise "Unknown state: #{state.inspect}" unless %i[created updated destroyed].include?(state)
122
121
  end
123
122
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableSync
4
- VERSION = "1.7.0"
4
+ VERSION = "1.8.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Umbrellio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-11 00:00:00.000000000 Z
11
+ date: 2019-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: memery