1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00
Maybe/app/models/concerns/syncable.rb
Zach Gollwitzer 9f13b5bb83
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions
Handle stale syncs (#2257)
* Handle stale syncs

* Use `visible` sync logic in sidebar groups
2025-05-17 18:28:21 -04:00

64 lines
1.2 KiB
Ruby

module Syncable
extend ActiveSupport::Concern
included do
has_many :syncs, as: :syncable, dependent: :destroy
end
def syncing?
raise NotImplementedError, "Subclasses must implement the syncing? method"
end
def sync_later(parent_sync: nil, window_start_date: nil, window_end_date: nil)
Sync.transaction do
# Expire any previous in-flight syncs for this record that exceeded the
# global staleness window.
syncs.stale_candidates.find_each(&:mark_stale!)
new_sync = syncs.create!(
parent: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
SyncJob.perform_later(new_sync)
end
end
def perform_sync(sync)
syncer.perform_sync(sync)
end
def perform_post_sync
syncer.perform_post_sync
end
def broadcast_sync_complete
sync_broadcaster.broadcast
end
def sync_error
latest_sync&.error
end
def last_synced_at
latest_sync&.completed_at
end
def last_sync_created_at
latest_sync&.created_at
end
private
def latest_sync
syncs.ordered.first
end
def syncer
self.class::Syncer.new(self)
end
def sync_broadcaster
self.class::SyncCompleteEvent.new(self)
end
end