diff --git a/app/models/sync.rb b/app/models/sync.rb index 325b5730..0ccd3533 100644 --- a/app/models/sync.rb +++ b/app/models/sync.rb @@ -1,6 +1,8 @@ class Sync < ApplicationRecord include AASM + Error = Class.new(StandardError) + belongs_to :syncable, polymorphic: true belongs_to :parent, class_name: "Sync", optional: true @@ -18,7 +20,7 @@ class Sync < ApplicationRecord state :completed state :failed - event :start do + event :start, after_commit: :report_warnings do transitions from: :pending, to: :syncing end @@ -90,6 +92,17 @@ class Sync < ApplicationRecord end end + def report_warnings + todays_sync_count = syncable.syncs.where(created_at: Date.current.all_day).count + + if todays_sync_count > 10 + Sentry.capture_exception( + Error.new("#{syncable_type} (#{syncable.id}) has exceeded 10 syncs today (count: #{todays_sync_count})"), + level: :warning + ) + end + end + def window_valid if window_start_date && window_end_date && window_start_date > window_end_date errors.add(:window_end_date, "must be greater than window_start_date")