1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 21:29:38 +02:00
Maybe/app/models/sync.rb

94 lines
2.4 KiB
Ruby
Raw Normal View History

class Sync < ApplicationRecord
2025-04-18 09:46:49 -04:00
Error = Class.new(StandardError)
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
2025-05-08 12:52:40 -04:00
syncable.sync_data(self, start_date: start_date)
2025-05-07 16:51:11 -04:00
unless has_pending_child_syncs?
2025-05-07 18:12:08 -04:00
complete!
2025-05-07 16:51:11 -04:00
Rails.logger.info("Sync completed, starting post-sync")
syncable.post_sync(self)
Rails.logger.info("Post-sync completed")
end
Transaction rules engine V1 (#1900) * Domain model sketch * Scaffold out rules domain * Migrations * Remove existing data enrichment for clean slate * Sketch out business logic and basic tests * Simplify rule scope building and action executions * Get generator working again * Basic implementation + tests * Remove manual merchant management (rules will replace) * Revert "Remove manual merchant management (rules will replace)" This reverts commit 83dcbd9ff0aa7bbee211796b71aa48b71df5e57e. * Family and Provider merchants model * Fix brakeman warnings * Fix notification loader * Update notification position * Add Rule action and condition registries * Rule form with compound conditions and tests * Split out notification types, add CTA type * Rules form builder and Stimulus controller * Clean up rule registry domain * Clean up rules stimulus controller * CTA message for rule when user changes transaction category * Fix tests * Lint updates * Centralize notifications in Notifiable concern * Implement category rule prompts with auto backoff and option to disable * Fix layout bug caused by merge conflict * Initialize rule with correct action for category CTA * Add rule deletions, get rules working * Complete dynamic rule form, split Stimulus controllers by resource * Fix failing tests * Change test password to avoid chromium conflicts * Update integration tests * Centralize all test password references * Add re-apply rule action * Rule confirm modal * Run migrations * Trigger rule notification after inline category updates * Clean up rule styles * Basic attribute locking for rules * Apply attribute locks on user edits * Log data enrichments, only apply rules to unlocked attributes * Fix merge errors * Additional merge conflict fixes * Form UI improvements, ignore attribute locks on manual rule application * Batch AI auto-categorization of transactions * Auto merchant detection, ai enrichment in batches * Fix Plaid merchant assignments * Plaid category matching * Cleanup 1 * Test cleanup * Remove stale route * Fix desktop chat UI issues * Fix mobile nav styling issues
2025-04-18 11:39:58 -04:00
rescue StandardError => error
fail! error
raise error if Rails.env.development?
2025-05-07 16:51:11 -04:00
ensure
2025-05-07 18:12:08 -04:00
notify_parent_of_completion! if has_parent?
end
2024-11-20 11:01:52 -05:00
end
end
def handle_child_completion_event
Sync.transaction do
# We need this to ensure 2 child syncs don't update the parent at the exact same time with different results
# and cause the sync to hang in "syncing" status indefinitely
self.lock!
unless has_pending_child_syncs?
complete!
2025-05-07 18:12:08 -04:00
# 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?
syncable.post_sync(self)
end
end
end
private
def has_pending_child_syncs?
children.where(status: [ :pending, :syncing ]).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
end
def complete!
Rails.logger.info("Sync completed")
update! status: :completed, last_ran_at: Time.current
end
def fail!(error)
Rails.logger.error("Sync failed: #{error.message}")
2024-12-02 12:04:54 -05:00
Sentry.capture_exception(error) do |scope|
scope.set_context("sync", { id: id, syncable_type: syncable_type, syncable_id: syncable_id })
2025-04-28 15:54:12 -04:00
scope.set_tags(sync_id: id)
2024-12-02 12:04:54 -05:00
end
update!(
status: :failed,
error: error.message,
last_ran_at: Time.current
)
end
end