Class: Harmonia::Sync

Inherits:
ApplicationRecord show all
Defined in:
lib/generators/harmonia/templates/harmonia_sync.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#to_fm

Class Method Details

.last_sync_for(table_name, direction, server_id = nil) ⇒ Object



21
22
23
24
25
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 21

def self.last_sync_for(table_name, direction, server_id = nil)
  scope = completed.for_direction(direction).for_table(table_name)
  scope = server_id ? scope.for_server(server_id) : scope.where(server_id: nil)
  scope.recent.first
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 33

def complete?
  status == 'completed' && records_synced == records_required
end

#completion_percentageObject



27
28
29
30
31
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 27

def completion_percentage
  return 0 if records_required.to_i.zero?

  ((records_synced.to_f / records_required.to_f) * 100).round(2)
end

#fail!(error_message, failed_fm_ids: {}, failed_pg_ids: {}) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 52

def fail!(error_message, failed_fm_ids: {}, failed_pg_ids: {})
  update!(
    status: 'failed',
    error_message: error_message,
    failed_fm_ids: failed_fm_ids,
    failed_pg_ids: failed_pg_ids
  )
end

#finish!(records_synced:, records_required:, failed_fm_ids: {}, failed_pg_ids: {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 41

def finish!(records_synced:, records_required:, failed_fm_ids: {}, failed_pg_ids: {})
  status = records_synced == records_required ? 'completed' : 'failed'
  update!(
    status: status,
    records_synced: records_synced,
    records_required: records_required,
    failed_fm_ids: failed_fm_ids,
    failed_pg_ids: failed_pg_ids
  )
end

#start!Object



37
38
39
# File 'lib/generators/harmonia/templates/harmonia_sync.rb', line 37

def start!
  update!(status: 'in_progress')
end