mirror of
https://github.com/we-promise/sure.git
synced 2026-08-12 11:10:20 +00:00
Add post-sync hook (#1479)
This commit is contained in:
@@ -22,6 +22,10 @@ module Syncable
|
|||||||
raise NotImplementedError, "Subclasses must implement the `sync_data` method"
|
raise NotImplementedError, "Subclasses must implement the `sync_data` method"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def post_sync
|
||||||
|
# no-op, syncable can optionally provide implementation
|
||||||
|
end
|
||||||
|
|
||||||
def sync_error
|
def sync_error
|
||||||
latest_sync.error
|
latest_sync.error
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ class Family < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def post_sync
|
||||||
|
broadcast_refresh
|
||||||
|
end
|
||||||
|
|
||||||
def syncing?
|
def syncing?
|
||||||
super || accounts.manual.any?(&:syncing?) || plaid_items.any?(&:syncing?)
|
super || accounts.manual.any?(&:syncing?) || plaid_items.any?(&:syncing?)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ class PlaidItem < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def post_sync
|
||||||
|
family.broadcast_refresh
|
||||||
|
end
|
||||||
|
|
||||||
def destroy_later
|
def destroy_later
|
||||||
update!(scheduled_for_deletion: true)
|
update!(scheduled_for_deletion: true)
|
||||||
DestroyJob.perform_later(self)
|
DestroyJob.perform_later(self)
|
||||||
|
|||||||
+9
-14
@@ -8,32 +8,27 @@ class Sync < ApplicationRecord
|
|||||||
def perform
|
def perform
|
||||||
start!
|
start!
|
||||||
|
|
||||||
syncable.sync_data(start_date: start_date)
|
begin
|
||||||
|
syncable.sync_data(start_date: start_date)
|
||||||
complete!
|
complete!
|
||||||
rescue StandardError => error
|
rescue StandardError => error
|
||||||
fail! error
|
fail! error
|
||||||
raise error if Rails.env.development?
|
raise error if Rails.env.development?
|
||||||
|
ensure
|
||||||
|
syncable.post_sync
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def family
|
|
||||||
syncable.is_a?(Family) ? syncable : syncable.family
|
|
||||||
end
|
|
||||||
|
|
||||||
def start!
|
def start!
|
||||||
update! status: :syncing
|
update! status: :syncing
|
||||||
end
|
end
|
||||||
|
|
||||||
def complete!
|
def complete!
|
||||||
update! status: :completed, last_ran_at: Time.current
|
update! status: :completed, last_ran_at: Time.current
|
||||||
|
|
||||||
family.broadcast_refresh
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fail!(error)
|
def fail!(error)
|
||||||
update! status: :failed, error: error.message, last_ran_at: Time.current
|
update! status: :failed, error: error.message, last_ran_at: Time.current
|
||||||
|
|
||||||
family.broadcast_refresh
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user