1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00

Propagate child sync errors up to parent, fix sync status (#2232)

* Propagate child sync errors up to parent, fix sync status

* Remove testing error
This commit is contained in:
Zach Gollwitzer 2025-05-09 14:56:49 -04:00 committed by GitHub
parent 03e3899541
commit ab2cec55e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 29 additions and 22 deletions

View file

@ -7,7 +7,6 @@ module AutoSync
private private
def sync_family def sync_family
Current.family.update!(last_synced_at: Time.current)
Current.family.sync_later Current.family.sync_later
end end

View file

@ -82,8 +82,6 @@ class Account < ApplicationRecord
end end
def sync_data(sync, start_date: nil) def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})") Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})")
sync_balances sync_balances
end end

View file

@ -27,7 +27,11 @@ module Syncable
end end
def sync_error def sync_error
latest_sync.error latest_sync&.error
end
def last_synced_at
latest_sync&.last_ran_at
end end
private private

View file

@ -66,8 +66,6 @@ class Family < ApplicationRecord
end end
def sync_data(sync, start_date: nil) def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
# We don't rely on this value to guard the app, but keep it eventually consistent # We don't rely on this value to guard the app, but keep it eventually consistent
sync_trial_status! sync_trial_status!

View file

@ -38,8 +38,6 @@ class PlaidItem < ApplicationRecord
end end
def sync_data(sync, start_date: nil) def sync_data(sync, start_date: nil)
update!(last_synced_at: Time.current)
begin begin
Rails.logger.info("Fetching and loading Plaid data") Rails.logger.info("Fetching and loading Plaid data")
fetch_and_load_plaid_data(sync) fetch_and_load_plaid_data(sync)

View file

@ -28,8 +28,7 @@ class Sync < ApplicationRecord
Rails.logger.info("Post-sync completed") Rails.logger.info("Post-sync completed")
end end
rescue StandardError => error rescue StandardError => error
fail! error fail! error, report_error: true
raise error if Rails.env.development?
ensure ensure
notify_parent_of_completion! if has_parent? notify_parent_of_completion! if has_parent?
end end
@ -43,7 +42,11 @@ class Sync < ApplicationRecord
self.lock! self.lock!
unless has_pending_child_syncs? unless has_pending_child_syncs?
complete! if has_failed_child_syncs?
fail!(Error.new("One or more child syncs failed"))
else
complete!
end
# If this sync is both a child and a parent, we need to notify the parent of completion # If this sync is both a child and a parent, we need to notify the parent of completion
notify_parent_of_completion! if has_parent? notify_parent_of_completion! if has_parent?
@ -58,6 +61,10 @@ class Sync < ApplicationRecord
children.where(status: [ :pending, :syncing ]).any? children.where(status: [ :pending, :syncing ]).any?
end end
def has_failed_child_syncs?
children.where(status: :failed).any?
end
def has_parent? def has_parent?
parent_id.present? parent_id.present?
end end
@ -76,12 +83,14 @@ class Sync < ApplicationRecord
update! status: :completed, last_ran_at: Time.current update! status: :completed, last_ran_at: Time.current
end end
def fail!(error) def fail!(error, report_error: false)
Rails.logger.error("Sync failed: #{error.message}") Rails.logger.error("Sync failed: #{error.message}")
Sentry.capture_exception(error) do |scope| if report_error
scope.set_context("sync", { id: id, syncable_type: syncable_type, syncable_id: syncable_id }) Sentry.capture_exception(error) do |scope|
scope.set_tags(sync_id: id) scope.set_context("sync", { id: id, syncable_type: syncable_type, syncable_id: syncable_id })
scope.set_tags(sync_id: id)
end
end end
update!( update!(

View file

@ -0,0 +1,7 @@
class DynamicLastSynced < ActiveRecord::Migration[7.2]
def change
remove_column :plaid_items, :last_synced_at, :datetime
remove_column :accounts, :last_synced_at, :datetime
remove_column :families, :last_synced_at, :datetime
end
end

5
db/schema.rb generated
View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_05_02_164951) do ActiveRecord::Schema[7.2].define(version: 2025_05_09_182903) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto" enable_extension "pgcrypto"
enable_extension "plpgsql" enable_extension "plpgsql"
@ -34,7 +34,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_02_164951) do
t.uuid "import_id" t.uuid "import_id"
t.uuid "plaid_account_id" t.uuid "plaid_account_id"
t.boolean "scheduled_for_deletion", default: false t.boolean "scheduled_for_deletion", default: false
t.datetime "last_synced_at"
t.decimal "cash_balance", precision: 19, scale: 4, default: "0.0" t.decimal "cash_balance", precision: 19, scale: 4, default: "0.0"
t.jsonb "locked_attributes", default: {} t.jsonb "locked_attributes", default: {}
t.index ["accountable_id", "accountable_type"], name: "index_accounts_on_accountable_id_and_accountable_type" t.index ["accountable_id", "accountable_type"], name: "index_accounts_on_accountable_id_and_accountable_type"
@ -225,7 +224,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_02_164951) do
t.string "stripe_customer_id" t.string "stripe_customer_id"
t.string "date_format", default: "%m-%d-%Y" t.string "date_format", default: "%m-%d-%Y"
t.string "country", default: "US" t.string "country", default: "US"
t.datetime "last_synced_at"
t.string "timezone" t.string "timezone"
t.boolean "data_enrichment_enabled", default: false t.boolean "data_enrichment_enabled", default: false
t.boolean "early_access", default: false t.boolean "early_access", default: false
@ -445,7 +443,6 @@ ActiveRecord::Schema[7.2].define(version: 2025_05_02_164951) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "available_products", default: [], array: true t.string "available_products", default: [], array: true
t.string "billed_products", default: [], array: true t.string "billed_products", default: [], array: true
t.datetime "last_synced_at"
t.string "plaid_region", default: "us", null: false t.string "plaid_region", default: "us", null: false
t.string "institution_url" t.string "institution_url"
t.string "institution_id" t.string "institution_id"

View file

@ -1,8 +1,5 @@
empty: empty:
name: Family name: Family
last_synced_at: <%= Time.now %>
dylan_family: dylan_family:
name: The Dylan Family name: The Dylan Family
last_synced_at: <%= Time.now %>