mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-04 21:15:19 +02:00
Sync hierarchy updates (#2087)
* Add parent sync orchestration * Pass sync object to dependents
This commit is contained in:
parent
9fa3698823
commit
8648f11413
11 changed files with 111 additions and 30 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue