synced 1.6.1 → 1.7.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
  SHA1:
3
- metadata.gz: 1ea40c30211956a680d7ef2a3030fbf97cfe45c2
4
- data.tar.gz: 3cbc1b4d38f7aabc32e54eac1ddf73667de1f13c
3
+ metadata.gz: 9d07dc9160fbcc981a1b08e986c8b732768b3912
4
+ data.tar.gz: 6ad287a51fd7465e84120d34c230d0359ef7afe1
5
5
  SHA512:
6
- metadata.gz: 8dbad6127fda57a74cb2d4c8fd443fbd8b4d6e7da07a2bb8203d07175f64bc4d146b43da406cfe28175d5a345542373beb5edcb3b5539e9e1a338be6bdb476a9
7
- data.tar.gz: b9a5699877146d56c1ac2ae2a2da4e94a29308c36bd09a89bf51b1178628ffb5ef3359806394a53c5cc7b19b6d3b9b2938ad78b73a15c95cb1b0f313737551ac
6
+ metadata.gz: 0381da8af658da381a11eb0fcc3b2f764a0e90e20c6fb5df03e6fc33d3755c1f39bdd4451c94461779ecb66140e6e60940e71781714ed1cf3431a038344a6c3b
7
+ data.tar.gz: 8c30d25d30aebb37edd4f1eb3b331795bce23efbcdda85331468f9c01b220b08ae3c97bac7e25fd9e157329e9816d8c4a1327cf8df2eb66533b7139d39245980
data/README.md CHANGED
@@ -275,12 +275,13 @@ and copy the body from here:
275
275
  class CreateSyncedTimestamps < ActiveRecord::Migration
276
276
  def change
277
277
  create_table :synced_timestamps do |t|
278
- t.belongs_to :parent_scope, polymorphic: true
278
+ t.string :parent_scope_type
279
+ t.integer :parent_scope_id
279
280
  t.string :model_class, null: false
280
281
  t.datetime :synced_at, null: false
281
282
  end
282
283
 
283
- add_index :synced_timestamps, [:parent_scope_id, :parent_scope_type, :model_class, :synced_at], name: 'synced_timestamps_max_index', order: { synced_at: 'DESC' }
284
+ add_index :synced_timestamps, [:parent_scope_id, :parent_scope_type, :model_class, :synced_at], name: "synced_timestamps_max_index", order: { synced_at: "DESC" }
284
285
  end
285
286
  end
286
287
  ```
@@ -289,6 +290,27 @@ end
289
290
  This strategy is added to fix the problems with massive updates on `synced_all_at`. Proper cleanup of timestamp records
290
291
  is needed once in a while with `Synced::Timestamp.cleanup` (cleans records older than 1 week).
291
292
 
293
+ ### Syncing with offset
294
+
295
+ Sometimes (mostly for pricing related endpoints like LOS Records or Rates) sync request
296
+ is made during transaction. That can make your local database out of sync, usually due to
297
+ older records not being removed. For such cases there is `tolerance` option which will
298
+ reduce `updated_since` value by specified amount of seconds. That way, if your last request has
299
+ been made during transaction, everything will heal itself during next sync.
300
+
301
+ For example:
302
+
303
+ ```ruby
304
+ class Rental < ActiveRecord::Base
305
+ synced tolerance: 60
306
+ end
307
+ ```
308
+
309
+ Will always reduce `updated_since` param by 60 seconds.
310
+ Setting this value too high can cause re-fetching same changes multiple times, which
311
+ may exhaust rate limit of your application much faster and increase overall sync time.
312
+
313
+
292
314
  ### Forcing local objects to be re-synced with the API
293
315
 
294
316
  When you add a new column or change something in the synced attributes and you
@@ -495,6 +517,7 @@ Option name | Default value | Description
495
517
  `:auto_paginate` | `true` | [Whether data should be fetched in batches or as one response](#fetching-methods) | YES | YES |
496
518
  `:transaction_per_page` | `false` | [Whether transaction should be per page of fetched objects or for all the pages - note that setting this value to `true` will mean always fetching data in batches, even when specifying `auto_paginate` as true](#persisting-fetched-objects) | YES | YES |
497
519
  `:handle_processed_objects_proc` | `nil` | [Custom proc taking persisted remote objects, called after persisting batch of data](#persisted-objects) | YES | NO |
520
+ `:tolerance` | 0 | [How many seconds updated_since param should be reduced during request](#syncing-with-offset) | YES | NO |
498
521
 
499
522
  ## Documentation
500
523
 
data/lib/synced/model.rb CHANGED
@@ -43,16 +43,20 @@ module Synced
43
43
  # @option options [Proc] handle_processed_objects_proc: Proc taking one argument (persisted remote objects).
44
44
  # Called after persisting remote objects (once in case of auto_paginate, after each batch
45
45
  # when paginating with block).
46
+ # @option options [Integer] tolerance: amount of seconds subtracted from last_request timestamp.
47
+ # Used to ensure records are up to date in case request has been made in the middle of transaction.
48
+ # Defaults to 0.
46
49
  def synced(strategy: :updated_since, **options)
47
- options.assert_valid_keys(:associations, :data_key, :fields,
48
- :globalized_attributes, :id_key, :include, :initial_sync_since,
49
- :local_attributes, :mapper, :only_updated, :remove, :auto_paginate, :transaction_per_page,
50
- :delegate_attributes, :query_params, :timestamp_strategy, :handle_processed_objects_proc)
50
+ options.assert_valid_keys(:associations, :data_key, :fields, :globalized_attributes,
51
+ :id_key, :include, :initial_sync_since, :local_attributes, :mapper, :only_updated,
52
+ :remove, :auto_paginate, :transaction_per_page, :delegate_attributes, :query_params,
53
+ :timestamp_strategy, :handle_processed_objects_proc, :tolerance)
51
54
  class_attribute :synced_id_key, :synced_data_key,
52
55
  :synced_local_attributes, :synced_associations, :synced_only_updated,
53
56
  :synced_mapper, :synced_remove, :synced_include, :synced_fields, :synced_auto_paginate, :synced_transaction_per_page,
54
57
  :synced_globalized_attributes, :synced_initial_sync_since, :synced_delegate_attributes,
55
- :synced_query_params, :synced_timestamp_strategy, :synced_strategy, :synced_handle_processed_objects_proc
58
+ :synced_query_params, :synced_timestamp_strategy, :synced_strategy, :synced_handle_processed_objects_proc,
59
+ :synced_tolerance
56
60
  self.synced_strategy = strategy
57
61
  self.synced_id_key = options.fetch(:id_key, :synced_id)
58
62
  self.synced_data_key = options.fetch(:data_key) { synced_column_presence(:synced_data) }
@@ -73,6 +77,7 @@ module Synced
73
77
  self.synced_auto_paginate = options.fetch(:auto_paginate, true)
74
78
  self.synced_transaction_per_page = options.fetch(:transaction_per_page, false)
75
79
  self.synced_handle_processed_objects_proc = options.fetch(:handle_processed_objects_proc, nil)
80
+ self.synced_tolerance = options.fetch(:tolerance, 0).to_i.abs
76
81
  include Synced::DelegateAttributes
77
82
  include Synced::HasSyncedData
78
83
  end
@@ -132,7 +137,8 @@ module Synced
132
137
  globalized_attributes: synced_globalized_attributes,
133
138
  initial_sync_since: synced_initial_sync_since,
134
139
  timestamp_strategy: synced_timestamp_strategy,
135
- handle_processed_objects_proc: synced_handle_processed_objects_proc
140
+ handle_processed_objects_proc: synced_handle_processed_objects_proc,
141
+ tolerance: synced_tolerance
136
142
  })
137
143
  Synced::Synchronizer.new(self, options).perform
138
144
  end
@@ -11,6 +11,7 @@ module Synced
11
11
  def initialize(model_class, options = {})
12
12
  super
13
13
  @initial_sync_since = options[:initial_sync_since]
14
+ @tolerance = options[:tolerance]
14
15
  timestampt_strategy_class = options[:timestamp_strategy] || Synced::Strategies::SyncedAllAtTimestampStrategy
15
16
  @timestamp_strategy = timestampt_strategy_class.new(relation_scope: relation_scope, scope: @scope, model_class: model_class)
16
17
  end
@@ -44,10 +45,15 @@ module Synced
44
45
 
45
46
  def updated_since
46
47
  instrument("updated_since.synced") do
47
- [@timestamp_strategy.last_synced_at, initial_sync_since].compact.max
48
+ [last_synced_at_with_offset, initial_sync_since].compact.max
48
49
  end
49
50
  end
50
51
 
52
+ def last_synced_at_with_offset
53
+ return if @timestamp_strategy.last_synced_at.blank?
54
+ @timestamp_strategy.last_synced_at - @tolerance
55
+ end
56
+
51
57
  def deleted_remote_objects_ids
52
58
  meta && meta[:deleted_ids] or raise CannotDeleteDueToNoDeletedIdsError.new(@model_class)
53
59
  end
@@ -1,3 +1,3 @@
1
1
  module Synced
2
- VERSION = "1.6.1"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synced
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Grosjean
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-10-24 00:00:00.000000000 Z
12
+ date: 2018-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails