class Syncinator

Constants

FIXNUM_MAX

Public Instance Methods

changesets() click to toggle source
# File lib/trogdir/syncinator.rb, line 30
def changesets
  Changeset.where('change_syncs.syncinator_id' => id).order_by(created_at: :asc)
end
error!(sync_log, message) click to toggle source
# File lib/trogdir/syncinator.rb, line 74
def error!(sync_log, message)
  # Because we have to save the change_sync instead of the sync_log (see below)
  # we need to make sure we grab the sync_log through the change_sync, other wise
  # the save on change_sync doesn't catch the changes to sync_log.
  change_sync = sync_log.change_sync
  sync_log = change_sync.sync_logs.find_by(id: sync_log.id)

  sync_log.errored_at = Time.now
  sync_log.message = message
  # There seems to be a bug in mongoid 4.0.2 that saves two records if you call just
  # sync_log.save!. Calling save on the ChangeSync seems to be the best work-around for now.
  # Thanks to Michael for finding the least-stupid workaround.
  # TODO: do a simple sync_log.save! when this issue gets fixed.
  sync_log.change_sync.save!

  sync_log
end
errored_changesets() click to toggle source
# File lib/trogdir/syncinator.rb, line 51
def errored_changesets
  Changeset.where(
    :change_syncs.elem_match => {
      syncinator_id: id, :run_after.ne => nil, :'sync_logs.errored_at'.exists => true
    }
  ).order_by(created_at: :asc)
end
finish!(sync_log, action, message = nil) click to toggle source
# File lib/trogdir/syncinator.rb, line 92
def finish!(sync_log, action, message = nil)
  # Because we have to save the change_sync instead of the sync_log (see below)
  # we need to make sure we grab the sync_log through the change_sync, other wise
  # the save on change_sync doesn't catch the changes to sync_log.
  change_sync = sync_log.change_sync
  sync_log = change_sync.sync_logs.find_by(id: sync_log.id)

  sync_log.succeeded_at = Time.now
  sync_log.action = action
  sync_log.message = message
  # There seems to be a bug in mongoid 4.0.2 that saves two records if you call just
  # sync_log.save!. Calling save on the ChangeSync seems to be the best work-around for now.
  # Thanks to Michael for finding the least-stupid workaround.
  # TODO: do a simple sync_log.save! when this issue gets fixed.
  sync_log.change_sync.save!

  sync_log
end
pending_changesets() click to toggle source

have started but haven't errored or succeeded

# File lib/trogdir/syncinator.rb, line 41
def pending_changesets
  Changeset.where(
    :change_syncs.elem_match => {
      syncinator_id: id, :run_after.ne => nil, :sync_logs.elem_match => {
        :started_at.ne => nil, errored_at: nil, succeeded_at: nil
      }
    }
  ).order_by(created_at: :asc)
end
start!(changeset) click to toggle source
# File lib/trogdir/syncinator.rb, line 65
def start!(changeset)
  return false unless change_sync = change_sync_for(changeset)

  # delete old pending sync_logs before creating a new one
  change_sync.sync_logs.where(succeeded_at: nil, errored_at: nil).delete

  change_sync.sync_logs.create! started_at: Time.now
end
startable_changesets() click to toggle source
# File lib/trogdir/syncinator.rb, line 59
def startable_changesets
  Changeset.where(
    :change_syncs.elem_match => {syncinator_id: id, :run_after.lt => Time.now }
  ).order_by(created_at: :asc)
end
to_s() click to toggle source
# File lib/trogdir/syncinator.rb, line 26
def to_s
  name
end
unfinished_changesets() click to toggle source
# File lib/trogdir/syncinator.rb, line 34
def unfinished_changesets
  Changeset.where(
    :change_syncs.elem_match => {syncinator_id: id, :run_after.ne => nil}
  ).order_by(created_at: :asc)
end

Private Instance Methods

change_sync_for(changeset) click to toggle source
# File lib/trogdir/syncinator.rb, line 113
def change_sync_for(changeset)
  changeset.change_syncs.find_by(syncinator: self)
end
set_access_id() click to toggle source
# File lib/trogdir/syncinator.rb, line 121
def set_access_id
  self.access_id ||= rand(FIXNUM_MAX)
end
set_default_slug() click to toggle source
# File lib/trogdir/syncinator.rb, line 117
def set_default_slug
  self.slug ||= name.parameterize
end
set_secret_key() click to toggle source
# File lib/trogdir/syncinator.rb, line 125
def set_secret_key
  self.secret_key ||= ApiAuth.generate_secret_key
end