Module: Stagehand::Staging::Synchronizer

Extended by:
Synchronizer
Included in:
Synchronizer
Defined in:
lib/stagehand/staging/synchronizer.rb

Constant Summary collapse

BATCH_SIZE =
1000
SESSION_BATCH_SIZE =
30
ENTRY_SYNC_ORDER =
[:delete, :update, :insert].freeze
ENTRY_SYNC_ORDER_SQL =
ActiveRecord::Base.send(:sanitize_sql_for_order, ['FIELD(operation, ?), id DESC', ENTRY_SYNC_ORDER]).freeze

Instance Method Summary collapse

Instance Method Details

#auto_sync(polling_delay = 5.seconds) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/stagehand/staging/synchronizer.rb', line 24

def auto_sync(polling_delay = 5.seconds)
  loop do
    Rails.logger.info "Autosyncing"
    sync(BATCH_SIZE)
    sleep(polling_delay) if polling_delay
  end
end

#sync(limit = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stagehand/staging/synchronizer.rb', line 32

def sync(limit = nil)
  synced_count = 0
  deleted_count = 0
  in_progress = nil

  Rails.logger.info "Syncing"

  iterate_autosyncable_entries do |entry|
    sync_entry(entry, :callbacks => :sync)
    synced_count += 1
    in_progress ||= CommitEntry.in_progress.pluck(:id)
    deleted_count += CommitEntry.matching(entry).no_newer_than(entry).where.not(:id => in_progress).delete_all
    break if synced_count == limit
  end

  Rails.logger.info "Synced #{synced_count} entries"
  Rails.logger.info "Removed #{deleted_count} stale entries"

  return synced_count
end

#sync_allObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stagehand/staging/synchronizer.rb', line 53

def sync_all
  loop do
    entries = CommitEntry.order(ENTRY_SYNC_ORDER_SQL).limit(BATCH_SIZE).to_a
    break unless entries.present?

    latest_entries = entries.uniq(&:key)
    latest_entries.each {|entry| sync_entry(entry, :callbacks => :sync) }
    Rails.logger.info "Synced #{latest_entries.count} entries"

    deleted_count = CommitEntry.matching(latest_entries).delete_all
    Rails.logger.info "Removed #{deleted_count - latest_entries.count} stale entries"
  end
end

#sync_checklist(checklist) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stagehand/staging/synchronizer.rb', line 72

def sync_checklist(checklist)
  Database.transaction do
    checklist.syncing_entries.each do |entry|
      if checklist.subject_entries.include?(entry)
        sync_entry(entry, :callbacks => [:sync, :sync_as_subject])
      else
        sync_entry(entry, :callbacks => [:sync, :sync_as_affected])
      end
    end

    CommitEntry.delete(checklist.affected_entries)
  end
end

#sync_now(subject_record = nil, &block) ⇒ Object

Immediately attempt to sync the changes from the block if possible The block is wrapped in a transaction to prevent changes to records while being synced

Raises:



14
15
16
17
18
19
20
21
22
# File 'lib/stagehand/staging/synchronizer.rb', line 14

def sync_now(subject_record = nil, &block)
  raise SyncBlockRequired unless block_given?

  Rails.logger.info "Syncing Now"
  Database.transaction do
    checklist = Checklist.new(Commit.capture(subject_record, &block).entries)
    sync_checklist(checklist) unless checklist.requires_confirmation?
  end
end

#sync_record(record) ⇒ Object

Copies all the affected records from the staging database to the production database



68
69
70
# File 'lib/stagehand/staging/synchronizer.rb', line 68

def sync_record(record)
  sync_checklist(Checklist.new(record))
end