2024-11-15 13:49:37 -05:00
|
|
|
class Sync < ApplicationRecord
|
2025-04-18 09:46:49 -04:00
|
|
|
Error = Class.new(StandardError)
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
belongs_to :syncable, polymorphic: true
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
belongs_to :parent, class_name: "Sync", optional: true
|
|
|
|
has_many :children, class_name: "Sync", foreign_key: :parent_id, dependent: :destroy
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
enum :status, { pending: "pending", syncing: "syncing", completed: "completed", failed: "failed" }
|
|
|
|
|
|
|
|
scope :ordered, -> { order(created_at: :desc) }
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def child?
|
|
|
|
parent_id.present?
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def perform
|
2025-03-05 15:38:31 -05:00
|
|
|
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-04-11 12:13:46 -04:00
|
|
|
|
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")
|
2025-04-11 12:13:46 -04:00
|
|
|
end
|
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?
|
2025-03-05 15:38:31 -05:00
|
|
|
end
|
2024-11-20 11:01:52 -05:00
|
|
|
end
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
def handle_child_completion_event
|
2025-05-07 16:28:58 -04:00
|
|
|
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?
|
2025-04-11 12:13:46 -04:00
|
|
|
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?
|
|
|
|
|
2025-04-11 12:13:46 -04:00
|
|
|
syncable.post_sync(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
private
|
2025-04-11 12:13:46 -04:00
|
|
|
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
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def start!
|
2025-03-05 15:38:31 -05:00
|
|
|
Rails.logger.info("Starting sync")
|
2024-11-15 13:49:37 -05:00
|
|
|
update! status: :syncing
|
|
|
|
end
|
|
|
|
|
|
|
|
def complete!
|
2025-03-05 15:38:31 -05:00
|
|
|
Rails.logger.info("Sync completed")
|
2024-11-15 13:49:37 -05:00
|
|
|
update! status: :completed, last_ran_at: Time.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail!(error)
|
2025-03-05 15:38:31 -05:00
|
|
|
Rails.logger.error("Sync failed: #{error.message}")
|
|
|
|
|
2024-12-02 12:04:54 -05:00
|
|
|
Sentry.capture_exception(error) do |scope|
|
2025-03-05 15:38:31 -05:00
|
|
|
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
|
|
|
|
|
2024-12-30 16:04:05 +01:00
|
|
|
update!(
|
|
|
|
status: :failed,
|
|
|
|
error: error.message,
|
|
|
|
last_ran_at: Time.current
|
|
|
|
)
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
end
|