Class: Syncinator

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Userstamp::User
Defined in:
lib/trogdir/syncinator.rb

Constant Summary collapse

FIXNUM_MAX =
(2**(0.size * 8 -2) -1)

Instance Method Summary collapse

Instance Method Details

#changesetsObject



30
31
32
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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_changesetsObject



51
52
53
54
55
56
57
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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_changesetsObject

have started but haven’t errored or succeeded



41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



65
66
67
68
69
70
71
72
# 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_changesetsObject



59
60
61
62
63
# 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_sObject



26
27
28
# File 'lib/trogdir/syncinator.rb', line 26

def to_s
  name
end

#unfinished_changesetsObject



34
35
36
37
38
# 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