class Consyncful::Sync

A mongoid model that stores the state of a syncronisation feed. Stores the next URL provided by Contentfuls Sync API.

Sync's are affectivly singletons, there should only ever be one in the database

Is also the entrypoint of a Syncronization run

Public Class Methods

fresh() click to toggle source

Delete the previous sync chains from database and create a fresh one. Used to completely resync all items from Contentful.

# File lib/consyncful/sync.rb, line 37
def self.fresh
  destroy_all
  latest
end
latest() click to toggle source
# File lib/consyncful/sync.rb, line 30
def self.latest
  last || new
end

Public Instance Methods

drop_stale() click to toggle source

Makes sure that the database contains only records that have been provided during this chain of syncronisation.

# File lib/consyncful/sync.rb, line 45
def drop_stale
  stale = Base.where(:sync_id.ne => id, :sync_id.exists => true)
  puts Rainbow("Dropping #{stale.count} records that haven't been touched in this sync").red
  stale.destroy
end
run() click to toggle source

Entry point to a syncronization run. Is responsible for updating Sync state

# File lib/consyncful/sync.rb, line 53
def run
  run_hook :before_run

  stats = Consyncful::Stats.new
  load_all_models

  sync = start_sync

  changed_ids = sync_items(sync, stats)

  drop_stale

  update_run(sync.next_sync_url)
  stats.print_stats

  run_hook :after_run, changed_ids
end

Private Instance Methods

load_all_models() click to toggle source
# File lib/consyncful/sync.rb, line 73
def load_all_models
  return unless defined? Rails

  Rails.application.eager_load!
end
start_sync() click to toggle source
# File lib/consyncful/sync.rb, line 85
def start_sync
  if next_url.present?
    puts Rainbow("Starting update, last update: #{last_run_at} (#{(Time.current - last_run_at).round(3)}s ago)").blue
    Consyncful.client.sync(next_url)
  else
    puts Rainbow('Starting full refresh').blue
    Consyncful.client.sync(initial: true)
  end
end
sync_item(item, stats) click to toggle source
# File lib/consyncful/sync.rb, line 105
def sync_item(item, stats)
  puts Rainbow("syncing: #{item.id}").yellow
  PersistedItem.new(item, id, stats).persist
  item.id
end
sync_items(sync, stats) click to toggle source
# File lib/consyncful/sync.rb, line 95
def sync_items(sync, stats)
  ids = []
  sync.each_page do |page|
    page.items.each do |item|
      ids << sync_item(ItemMapper.new(item), stats)
    end
  end
  ids
end
update_run(next_url) click to toggle source
# File lib/consyncful/sync.rb, line 79
def update_run(next_url)
  self.next_url = next_url
  self.last_run_at = Time.current
  save
end