From 5125411822a47e5543ffbb4a7485880990c91ae2 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Sat, 24 May 2025 17:58:17 -0400 Subject: [PATCH] Handle duplicate sync jobs --- app/models/sync.rb | 6 ++++++ test/models/sync_test.rb | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/models/sync.rb b/app/models/sync.rb index be4bb8c3..37c05dfa 100644 --- a/app/models/sync.rb +++ b/app/models/sync.rb @@ -57,6 +57,12 @@ class Sync < ApplicationRecord def perform Rails.logger.tagged("Sync", id, syncable_type, syncable_id) do + # This can happen on server restarts or if Sidekiq enqueues a duplicate job + unless may_start? + Rails.logger.warn("Sync #{id} is not in a valid state (#{aasm.from_state}) to start. Skipping sync.") + return + end + start! begin diff --git a/test/models/sync_test.rb b/test/models/sync_test.rb index 05765ea0..d1182fd5 100644 --- a/test/models/sync_test.rb +++ b/test/models/sync_test.rb @@ -3,6 +3,17 @@ require "test_helper" class SyncTest < ActiveSupport::TestCase include ActiveJob::TestHelper + test "does not run if not in a valid state" do + syncable = accounts(:depository) + sync = Sync.create!(syncable: syncable, status: :completed) + + syncable.expects(:perform_sync).never + + sync.perform + + assert_equal "completed", sync.status + end + test "runs successful sync" do syncable = accounts(:depository) sync = Sync.create!(syncable: syncable)