Module: SyncRetryable

Defined in:
lib/generators/harmonia/templates/concerns/sync_retryable.rb

Overview

Adds retry capability for FM IDs that failed in the previous sync run.

Usage in a syncer:

include SyncRetryable

def run
  ...
  @database_connector.open_database do
    sync_record.start!
    sync_records(sync_record)
    retry_failed_records(FileMaker::MyModel, table: 'my_models', direction: 'FileMaker to ActiveRecord')
  end
end

The concern reads failed_fm_ids from the previous completed/failed sync, fetches those records from FileMaker, then calls retry_create / retry_update. Syncers can override either hook for custom logic (e.g. photo handling).

Instance Method Summary collapse

Instance Method Details

#retry_failed_records(fm_model_class, table:, direction:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/generators/harmonia/templates/concerns/sync_retryable.rb', line 22

def retry_failed_records(fm_model_class, table:, direction:)
  previous_sync = Harmonia::Sync
                  .where(table:, direction:, server_id: @server_id)
                  .where(status: %w[completed failed])
                  .order(ran_on: :desc)
                  .offset(1) # skip the sync that just completed
                  .first

  return unless previous_sync

  failed_fm_ids = previous_sync.failed_fm_ids.keys
  return if failed_fm_ids.empty?

  log_sync_retry_start(table:, direction:, count: failed_fm_ids.size, ids: failed_fm_ids)

  fm_records = failed_fm_ids.filter_map do |fm_id|
    fm_model_class.where(id: fm_id).first
  rescue StandardError => e
    Rails.logger.warn("[SYNC RETRY] Could not fetch FM record #{fm_id}: #{e.message}")
    nil
  end

  pg_model = retryable_pg_model
  to_create = fm_records.reject { |r| pg_model.exists?(filemaker_id: r.id) }
  to_update = fm_records.select { |r| pg_model.exists?(filemaker_id: r.id) }

  created = retry_create(to_create, fm_model_class)
  updated = retry_update(to_update, fm_model_class)

  log_sync_retry_finish(table:, direction:, created:, updated:)
end