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

Sync hierarchy updates (#2087)

* Add parent sync orchestration

* Pass sync object to dependents
This commit is contained in:
Zach Gollwitzer 2025-04-11 12:13:46 -04:00 committed by GitHub
parent 9fa3698823
commit 8648f11413
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 111 additions and 30 deletions

View file

@ -2,7 +2,6 @@ class SyncJob < ApplicationJob
queue_as :high_priority
def perform(sync)
sleep 1 # simulate work for faster jobs
sync.perform
end
end

View file

@ -70,24 +70,26 @@ class Account < ApplicationRecord
DestroyJob.perform_later(self)
end
def sync_data(start_date: nil)
def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
Rails.logger.info("Auto-matching transfers")
family.auto_match_transfers!
Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})")
sync_balances
end
def post_sync(sync)
family.remove_syncing_notice!
accountable.post_sync(sync)
if enrichable?
Rails.logger.info("Enriching transaction data")
enrich_data
end
end
def post_sync
broadcast_remove_to(family, target: "syncing-notice")
accountable.post_sync
unless sync.child?
family.auto_match_transfers!
end
end
def original_balance

View file

@ -45,7 +45,7 @@ module Accountable
end
end
def post_sync
def post_sync(sync)
broadcast_replace_to(
account,
target: "chart_account_#{account.id}",

View file

@ -9,8 +9,8 @@ module Syncable
syncs.where(status: [ :syncing, :pending ]).any?
end
def sync_later(start_date: nil)
new_sync = syncs.create!(start_date: start_date)
def sync_later(start_date: nil, parent_sync: nil)
new_sync = syncs.create!(start_date: start_date, parent: parent_sync)
SyncJob.perform_later(new_sync)
end
@ -18,11 +18,11 @@ module Syncable
syncs.create!(start_date: start_date).perform
end
def sync_data(start_date: nil)
def sync_data(sync, start_date: nil)
raise NotImplementedError, "Subclasses must implement the `sync_data` method"
end
def post_sync
def post_sync(sync)
# no-op, syncable can optionally provide implementation
end

View file

@ -43,29 +43,36 @@ class Family < ApplicationRecord
@income_statement ||= IncomeStatement.new(self)
end
def sync_data(start_date: nil)
def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
accounts.manual.each do |account|
account.sync_later(start_date: start_date)
account.sync_later(start_date: start_date, parent_sync: sync)
end
plaid_items.each do |plaid_item|
plaid_item.sync_later(start_date: start_date)
plaid_item.sync_later(start_date: start_date, parent_sync: sync)
end
end
def post_sync
def remove_syncing_notice!
broadcast_remove target: "syncing-notice"
end
def post_sync(sync)
auto_match_transfers!
broadcast_refresh
end
# If family has any syncs pending/syncing within the last hour, we show a persistent "syncing" notice.
# Ignore syncs older than 1 hour as they are considered "stale"
def syncing?
Sync.where(
"(syncable_type = 'Family' AND syncable_id = ?) OR
(syncable_type = 'Account' AND syncable_id IN (SELECT id FROM accounts WHERE family_id = ? AND plaid_account_id IS NULL)) OR
(syncable_type = 'PlaidItem' AND syncable_id IN (SELECT id FROM plaid_items WHERE family_id = ?))",
id, id, id
).where(status: [ "pending", "syncing" ]).exists?
).where(status: [ "pending", "syncing" ], created_at: 1.hour.ago..).exists?
end
def eu?

View file

@ -37,7 +37,7 @@ class PlaidItem < ApplicationRecord
end
end
def sync_data(start_date: nil)
def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
begin
@ -79,7 +79,7 @@ class PlaidItem < ApplicationRecord
end
end
def post_sync
def post_sync(sync)
family.broadcast_refresh
end

View file

@ -1,32 +1,71 @@
class Sync < ApplicationRecord
belongs_to :syncable, polymorphic: true
belongs_to :parent, class_name: "Sync", optional: true
has_many :children, class_name: "Sync", foreign_key: :parent_id, dependent: :destroy
enum :status, { pending: "pending", syncing: "syncing", completed: "completed", failed: "failed" }
scope :ordered, -> { order(created_at: :desc) }
def child?
parent_id.present?
end
def perform
Rails.logger.tagged("Sync", id, syncable_type, syncable_id) do
start!
begin
data = syncable.sync_data(start_date: start_date)
data = syncable.sync_data(self, start_date: start_date)
update!(data: data) if data
complete!
complete! unless has_pending_child_syncs?
rescue StandardError => error
fail! error
raise error if Rails.env.development?
ensure
Rails.logger.info("Sync completed, starting post-sync")
syncable.post_sync
if has_parent?
notify_parent_of_completion!
else
syncable.post_sync(self)
end
Rails.logger.info("Post-sync completed")
end
end
end
def handle_child_completion_event
unless has_pending_child_syncs?
if has_failed_child_syncs?
fail!("One or more child syncs failed")
else
complete!
syncable.post_sync(self)
end
end
end
private
def has_pending_child_syncs?
children.where(status: [ :pending, :syncing ]).any?
end
def has_failed_child_syncs?
children.where(status: :failed).any?
end
def has_parent?
parent_id.present?
end
def notify_parent_of_completion!
parent.handle_child_completion_event
end
def start!
Rails.logger.info("Starting sync")
update! status: :syncing