lib/wcc/contentful/sync_engine.rb in wcc-contentful-1.2.1 vs lib/wcc/contentful/sync_engine.rb in wcc-contentful-1.3.0
- old
+ new
@@ -74,25 +74,27 @@
id_found = up_to_id.nil?
all_events = []
@mutex.synchronize do
@state ||= read_state || token_wrapper_factory(nil)
- next_sync_token = @state['token']
+ sync_token = @state['token']
- sync_resp = client.sync(sync_token: next_sync_token)
- sync_resp.items.each do |item|
- id = item.dig('sys', 'id')
- id_found ||= id == up_to_id
+ next_sync_token =
+ client.sync(sync_token: sync_token) do |item|
+ id = item.dig('sys', 'id')
+ id_found ||= id == up_to_id
- store.index(item) if store&.index?
- event = WCC::Contentful::Event.from_raw(item, source: self)
- yield(event) if block_given?
- emit_event(event)
- all_events << event
- end
+ store.index(item) if store&.index?
+ event = WCC::Contentful::Event.from_raw(item, source: self)
+ yield(event) if block_given?
+ emit_event(event)
- @state = @state.merge('token' => sync_resp.next_sync_token)
+ # Only keep the "sys" not the content in case we have a large space
+ all_events << WCC::Contentful::Event.from_raw(item.slice('sys'), source: self)
+ end
+
+ @state = @state.merge('token' => next_sync_token)
write_state
end
emit_sync_complete(all_events)
@@ -134,17 +136,23 @@
# Define the job only if rails is loaded
if defined?(ActiveJob)
# This job uses the Contentful Sync API to update the configured store with
# the latest data from Contentful.
class Job < ActiveJob::Base
- include WCC::Contentful::ServiceAccessors
-
self.queue_adapter = :async
queue_as :default
+ def configuration
+ @configuration ||= WCC::Contentful.configuration
+ end
+
+ def services
+ @services ||= WCC::Contentful::Services.instance
+ end
+
def perform(event = nil)
- return unless sync_engine&.should_sync?
+ return unless services.sync_engine&.should_sync?
up_to_id = nil
retry_count = 0
if event
up_to_id = event[:up_to_id] || event.dig('sys', 'id')
@@ -160,12 +168,12 @@
# An ID that we know has changed and should come back from the sync.
# If we don't find this ID in the sync data, then drop a job to try
# the sync again after a few minutes.
#
def sync!(up_to_id: nil, retry_count: 0)
- id_found, count = sync_engine.next(up_to_id: up_to_id)
+ id_found, count = services.sync_engine.next(up_to_id: up_to_id)
- next_sync_token = sync_engine.state['token']
+ next_sync_token = services.sync_engine.state['token']
logger.info "Synced #{count} entries. Next sync token:\n #{next_sync_token}"
unless id_found
if retry_count >= configuration.sync_retry_limit
logger.error "Unable to find item with id '#{up_to_id}' on the Sync API. " \