The personal finance app for everyone (by everyone)
Ruby
Fix scheduled-entry fallback sync across time zones and deletion (#4852)
Saim Shafique74066e4d3a7f
diff --git a/app/jobs/entry_scheduled_sync_job.rb b/app/jobs/entry_scheduled_sync_job.rbindex 85fba64018..dbba1e9040 100644--- a/app/jobs/entry_scheduled_sync_job.rb+++ b/app/jobs/entry_scheduled_sync_job.rb@@ -6,29 +6,40 @@ class EntryScheduledSyncJob < ApplicationJob# disabled, nothing would otherwise trigger a sync on that date. This job# is the fallback: it fires once, on the entry's date, and simply asks the# account to sync -- a cheap no-op if a sync already happened by then.- def perform(entry_id)+ def perform(entry_id, account_id = nil)entry = Entry.find_by(id: entry_id)- return unless entry+ return entry.account.sync_later if entry- entry.account.sync_later+ # The representative entry was deleted after the job was scheduled; the+ # other entries on that account and date still need their sync.+ Account.find_by(id: account_id)&.sync_later if account_idenddef self.schedule_for(entry)- return unless entry.scheduled?+ # "Future" is judged in the family's zone: entries are scheduled from jobs+ # running in the app's zone, where Date.current may already be the+ # entry's date even though it is still tomorrow for the family.+ Time.use_zone(entry.account.family.resolved_time_zone) do+ return unless entry.scheduled?- # A few minutes past midnight on the entry's date, so Date.current has- # definitively rolled over (avoids clock-skew edge cases) by run time.- # Midnight in the family's zone, as Sync#perform judges the date there --- # imports and bulk updates call this from jobs running in the app's zone.- run_at = entry.date.in_time_zone(entry.account.family.resolved_time_zone) + 5.minutes- set(wait_until: run_at).perform_later(entry.id)+ # A few minutes past midnight on the entry's date, so Date.current has+ # definitively rolled over (avoids clock-skew edge cases) by run time.+ # Midnight in the family's zone, as Sync#perform judges the date there.+ run_at = entry.date.in_time_zone(entry.account.family.resolved_time_zone) + 5.minutes+ set(wait_until: run_at).perform_later(entry.id, entry.account_id)+ endend# Batch variant for paths that write many entries without going through# Entry#sync_account_later (imports, bulk updates). Enqueues one job per# (account, date) -- they'd all sync the same account on the same day.+ # A loose lower bound only: whether a date is "future" depends on the+ # family's time zone, so the exact check lives in schedule_for. Time-zone+ # skew is at most a day, so entries older than yesterday can never be+ # scheduled -- filtering them in SQL avoids loading account + family for+ # every distinct date in a large historical import.def self.schedule_for_entries(entries)- entries.where("entries.date > ?", Date.current)+ entries.where("entries.date >= ?", Date.current - 1.day).select("DISTINCT ON (entries.account_id, entries.date) entries.*").order("entries.account_id, entries.date").each { |entry| schedule_for(entry) }diff --git a/app/models/entry.rb b/app/models/entry.rbindex db0ca98712..b6600bb965 100644--- a/app/models/entry.rb+++ b/app/models/entry.rb@@ -512,7 +512,10 @@ class Entry < ApplicationRecord@earliest_saved_date = nilaccount.sync_later(window_start_date: sync_start_date)- EntryScheduledSyncJob.schedule_for(self) if !destroyed? && scheduled?+ # No scheduled? gate here: schedule_for judges "future" in the family's+ # time zone, and the app-zone check would skip entries that are still+ # tomorrow for the family.+ EntryScheduledSyncJob.schedule_for(self) unless destroyed?end# Whether the last save changed anything a sync depends on. Call it rightdiff --git a/test/jobs/entry_scheduled_sync_job_test.rb b/test/jobs/entry_scheduled_sync_job_test.rbindex a3c69bfb24..c64aba1745 100644--- a/test/jobs/entry_scheduled_sync_job_test.rb+++ b/test/jobs/entry_scheduled_sync_job_test.rb@@ -6,11 +6,26 @@ class EntryScheduledSyncJobTest < ActiveJob::TestCasetest "schedule_for enqueues a job for the entry's date when scheduled" doentry = create_transaction(account: accounts(:depository), date: 3.days.from_now.to_date)- assert_enqueued_with(job: EntryScheduledSyncJob, args: [ entry.id ]) do+ assert_enqueued_with(job: EntryScheduledSyncJob, args: [ entry.id, entry.account_id ]) doEntryScheduledSyncJob.schedule_for(entry)endend+ test "schedule_for enqueues when the entry is dated tomorrow in the family's time zone" do+ families(:dylan_family).update!(timezone: "America/Los_Angeles")++ # 04:00 UTC is 20:00 the previous day in Los Angeles: the app already+ # calls it tomorrow, the family still calls it today.+ travel_to Time.utc(2026, 11, 11, 4, 0, 0) do+ entry = create_transaction(account: accounts(:depository), date: Date.new(2026, 11, 11))+ expected_run_at = entry.date.in_time_zone("America/Los_Angeles") + 5.minutes++ assert_enqueued_with(job: EntryScheduledSyncJob, args: [ entry.id, entry.account_id ], at: expected_run_at) do+ EntryScheduledSyncJob.schedule_for(entry)+ end+ end+ end+test "schedule_for runs just after midnight in the family's timezone" dofamilies(:dylan_family).update!(timezone: "America/New_York")entry = create_transaction(account: accounts(:depository), date: 3.days.from_now.to_date)@@ -42,6 +57,58 @@ class EntryScheduledSyncJobTest < ActiveJob::TestCaseendend+ test "schedule_for_entries includes entries dated tomorrow in the family's time zone" do+ families(:dylan_family).update!(timezone: "America/Los_Angeles")++ travel_to Time.utc(2026, 11, 11, 4, 0, 0) do+ entry = create_transaction(account: accounts(:depository), date: Date.new(2026, 11, 11))++ assert_enqueued_jobs 1, only: EntryScheduledSyncJob do+ EntryScheduledSyncJob.schedule_for_entries(Entry.where(id: entry.id))+ end+ end+ end++ test "schedule_for_entries does not touch entries dated before yesterday" do+ account = accounts(:depository)+ recent = create_transaction(account: account, date: 3.days.from_now.to_date)+ old = create_transaction(account: account, date: 1.month.ago.to_date)+ older = create_transaction(account: account, date: 2.years.ago.to_date)++ scheduled = []+ EntryScheduledSyncJob.stubs(:schedule_for).with { |entry| scheduled << entry.id; true }++ EntryScheduledSyncJob.schedule_for_entries(Entry.where(id: [ recent.id, old.id, older.id ]))++ assert_equal [ recent.id ], scheduled+ end++ test "sync_account_later schedules the fallback job when the entry is tomorrow in the family's time zone" do+ families(:dylan_family).update!(timezone: "America/Los_Angeles")++ # 04:00 UTC is 20:00 the previous day in Los Angeles: the app already+ # calls it tomorrow, the family still calls it today.+ travel_to Time.utc(2026, 11, 11, 4, 0, 0) do+ entry = create_transaction(account: accounts(:depository), date: Date.new(2026, 11, 11))+ expected_run_at = entry.date.in_time_zone("America/Los_Angeles") + 5.minutes+ Account.any_instance.stubs(:sync_later)++ assert_enqueued_with(job: EntryScheduledSyncJob, args: [ entry.id, entry.account_id ], at: expected_run_at) do+ entry.sync_account_later+ end+ end+ end++ test "perform still syncs the account when the representative entry was deleted" do+ entry = create_transaction(account: accounts(:depository), date: 3.days.from_now.to_date)+ account = entry.account+ entry.destroy!++ Account.any_instance.expects(:sync_later).once++ EntryScheduledSyncJob.perform_now(entry.id, account.id)+ end+test "perform triggers a sync for the entry's account" doentry = create_transaction(account: accounts(:depository), date: 3.days.from_now.to_date)