1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-06 14:05:20 +02:00

Partial sync interface

This commit is contained in:
Zach Gollwitzer 2025-05-26 05:39:14 -04:00
parent 6e202bd7ec
commit 7a8ac82823
13 changed files with 34 additions and 94 deletions

View file

@ -9,26 +9,17 @@ module Syncable
raise NotImplementedError, "Subclasses must implement the syncing? method"
end
# Schedules a sync for syncable. If there is an existing sync pending/syncing for this syncable,
# we do not create a new sync, and attempt to expand the sync window if needed.
def sync_later(parent_sync: nil, window_start_date: nil, window_end_date: nil)
def sync_later(parent_sync: nil)
Sync.transaction do
with_lock do
sync = self.syncs.incomplete.first
if sync
Rails.logger.info("There is an existing sync, expanding window if needed (#{sync.id})")
sync.expand_window_if_needed(window_start_date, window_end_date)
else
sync = self.syncs.create!(
parent: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
SyncJob.perform_later(sync)
unless sync
sync = self.syncs.create!(parent: parent_sync)
end
SyncJob.perform_later(sync)
sync
end
end
@ -58,6 +49,10 @@ module Syncable
latest_sync&.created_at
end
def needs_sync?
data_synced_through.nil? || data_synced_through < Date.current
end
private
def latest_sync
syncs.ordered.first

View file

@ -44,8 +44,9 @@ class Entry < ApplicationRecord
end
def sync_account_later
# TODO: <Sync>
sync_start_date = [ date_previously_was, date ].compact.min unless destroyed?
account.sync_later(window_start_date: sync_start_date)
account.sync_later
end
def entryable_name_short

View file

@ -16,7 +16,7 @@ class Family::Syncer
# Schedule child syncs
child_syncables.each do |syncable|
syncable.sync_later(parent_sync: sync, window_start_date: sync.window_start_date, window_end_date: sync.window_end_date)
syncable.sync_later(parent_sync: sync)
end
end

View file

@ -68,13 +68,10 @@ class PlaidItem < ApplicationRecord
end
# Once all the data is fetched, we can schedule account syncs to calculate historical balances
def schedule_account_syncs(parent_sync: nil, window_start_date: nil, window_end_date: nil)
# TODO: <Sync>
def schedule_account_syncs(parent_sync: nil)
accounts.each do |account|
account.sync_later(
parent_sync: parent_sync,
window_start_date: window_start_date,
window_end_date: window_end_date
)
account.sync_later(parent_sync: parent_sync)
end
end

View file

@ -13,11 +13,7 @@ class PlaidItem::Syncer
plaid_item.process_accounts
# All data is synced, so we can now run an account sync to calculate historical balances and more
plaid_item.schedule_account_syncs(
parent_sync: sync,
window_start_date: sync.window_start_date,
window_end_date: sync.window_end_date
)
plaid_item.schedule_account_syncs(parent_sync: sync)
end
def perform_post_sync

View file

@ -101,29 +101,6 @@ class Sync < ApplicationRecord
parent&.finalize_if_all_children_finalized
end
# If a sync is pending, we can adjust the window if new syncs are created with a wider window.
def expand_window_if_needed(new_window_start_date, new_window_end_date)
return unless pending?
return if self.window_start_date.nil? && self.window_end_date.nil? # already as wide as possible
earliest_start_date = if self.window_start_date && new_window_start_date
[ self.window_start_date, new_window_start_date ].min
else
nil
end
latest_end_date = if self.window_end_date && new_window_end_date
[ self.window_end_date, new_window_end_date ].max
else
nil
end
update(
window_start_date: earliest_start_date,
window_end_date: latest_end_date
)
end
private
def log_status_change
Rails.logger.info("changing from #{aasm.from_state} to #{aasm.to_state} (event: #{aasm.current_event})")