Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Handle duplicate sync jobs

Zach Gollwitzer5125411822a4
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)