The personal finance app for everyone (by everyone)
Ruby
Enqueue scheduled cron jobs as ActiveJob payloads (#3768)
GFRc83f2b2d4501
diff --git a/app/services/auto_sync_scheduler.rb b/app/services/auto_sync_scheduler.rbindex 44adea3b01..6170e4aa68 100644--- a/app/services/auto_sync_scheduler.rb+++ b/app/services/auto_sync_scheduler.rb@@ -30,6 +30,7 @@ class AutoSyncSchedulername: JOB_NAME,cron: cron,class: "SyncAllJob",+ active_job: true, # see config/schedule.ymlqueue: "scheduled",description: "Syncs all accounts for all families")diff --git a/config/schedule.yml b/config/schedule.ymlindex 126681d8cd..07f6a0f003 100644--- a/config/schedule.yml+++ b/config/schedule.yml@@ -1,6 +1,12 @@+# Every scheduled class is an ActiveJob (ApplicationJob), so each entry sets+# `active_job: true`. Without it sidekiq-cron falls back to a raw Sidekiq::Job+# payload whenever the enqueuing process can't resolve the class constant, and+# the worker then fails with `undefined method 'jid='` (we-promise/sure#1442).+import_market_data:cron: "0 22 * * 1-5" # 5:00 PM EST / 6:00 PM EDT (NY time) Monday through Fridayclass: "ImportMarketDataJob"+ active_job: truequeue: "scheduled"description: "Imports market data daily at 5:00 PM EST (1 hour after market close)"args:@@ -10,77 +16,90 @@ import_market_data:clean_syncs:cron: "0 * * * *" # every hourclass: "SyncCleanerJob"+ active_job: truequeue: "scheduled"description: "Cleans up stale syncs and reaps imports, exports, and provider fetch flags stuck by lost jobs"process_financekit_inbox:cron: "*/5 * * * *"class: "FinancekitInboxJob"+ active_job: truequeue: "high_priority"description: "Applies durable FinanceKit publisher batches and retries downstream work"run_security_health_checks:cron: "0 2 * * 1-5" # 2:00 AM EST / 3:00 AM EDT (NY time) Monday through Fridayclass: "SecurityHealthCheckJob"+ active_job: truequeue: "scheduled"description: "Runs security health checks to detect issues with security data"sync_hourly:cron: "0 * * * *" # every hour at the top of the hourclass: "SyncHourlyJob"+ active_job: truequeue: "scheduled"description: "Syncs provider items that opt-in to hourly syncing"sync_property_valuations:cron: "30 1 1 * *" # monthly on the 1st at 1:30 AMclass: "SyncPropertyValuationsJob"+ active_job: truequeue: "scheduled"description: "Refreshes AVM property valuations once a month (providers enforce tight monthly request caps)"clean_data:cron: "0 3 * * *" # daily at 3:00 AMclass: "DataCleanerJob"+ active_job: truequeue: "scheduled"description: "Cleans up old data (e.g., expired merchant associations, expired archived exports)"clean_debug_log_entries:cron: "30 3 * * *" # daily at 3:30 AMclass: "DebugLogCleanupJob"+ active_job: truequeue: "scheduled"description: "Deletes debug log entries older than 90 days"clean_inactive_families:cron: "0 4 * * *" # daily at 4:00 AMclass: "InactiveFamilyCleanerJob"+ active_job: truequeue: "scheduled"description: "Archives and destroys families that expired their trial without subscribing (managed mode only)"refresh_demo_family:cron: "0 5 * * *" # daily at 5:00 AM UTCclass: "DemoFamilyRefreshJob"+ active_job: truequeue: "scheduled"description: "Refreshes demo family data and emails super admins with daily usage summary"generate_recurring_occurrences:cron: "30 5 * * *" # daily at 5:30 AM UTC, before insights so generators see fresh occurrencesclass: "GenerateRecurringOccurrencesJob"+ active_job: truequeue: "scheduled"description: "Materializes the rolling window of recurring-transaction occurrences for every family"generate_insights:cron: "0 6 * * *" # daily at 6:00 AM UTC, after the nightly cleanup jobsclass: "GenerateInsightsJob"+ active_job: truequeue: "scheduled"description: "Generates proactive financial insights for families with preview features enabled"sweep_expired_goal_pledges:cron: "*/15 * * * *" # every 15 minutesclass: "SweepExpiredGoalPledgesJob"+ active_job: truequeue: "scheduled"description: "Marks goal pledges that passed their 7-day window as expired"refresh_maintained_goal_targets:cron: "0 3 1 * *" # 3:00 AM on the 1st of each monthclass: "RefreshMaintainedGoalTargetsJob"+ active_job: truequeue: "scheduled"description: "Recomputes the floor of reserves expressed in months of expenses"diff --git a/test/jobs/cron_schedule_test.rb b/test/jobs/cron_schedule_test.rbnew file mode 100644index 0000000000..4199c20896--- /dev/null+++ b/test/jobs/cron_schedule_test.rb@@ -0,0 +1,36 @@+require "test_helper"++# Regression coverage for https://github.com/we-promise/sure/issues/1442 - when+# the process firing a cron tick couldn't resolve the job class, sidekiq-cron+# pushed a raw Sidekiq::Job payload and the worker crashed with+# `undefined method 'jid='` because every scheduled class is an ActiveJob.+class CronScheduleTest < ActiveSupport::TestCase+ SCHEDULE = YAML.safe_load_file(Rails.root.join("config/schedule.yml"))+ JOB_WRAPPER = "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"++ test "every scheduled job is an ActiveJob flagged with active_job: true" do+ SCHEDULE.each do |name, config|+ assert config["class"].constantize < ApplicationJob, "#{name}: #{config["class"]} must be an ApplicationJob"+ assert_equal true, config["active_job"], "#{name}: set `active_job: true` so sidekiq-cron always enqueues an ActiveJob payload"+ end+ end++ test "cron ticks enqueue an ActiveJob wrapper even when the job class can't be resolved" do+ Sidekiq::Cron::Support.stubs(:safe_constantize).returns(nil)+ Sidekiq::Cron::Job.any_instance.stubs(:save_last_enqueue_time)+ Sidekiq::Cron::Job.any_instance.stubs(:add_jid_history)++ SCHEDULE.each do |name, config|+ job = Sidekiq::Cron::Job.new(config.merge("name" => name, "status" => "enabled", "last_enqueue_time" => Time.current.to_s))++ Sidekiq::Client.expects(:push).with do |payload|+ payload["class"] == JOB_WRAPPER &&+ payload["wrapped"] == config["class"] &&+ payload["queue"] == config["queue"] &&+ payload["args"].first["job_class"] == config["class"]+ end.returns("jid")++ job.enqueue!+ end+ end+enddiff --git a/test/services/auto_sync_scheduler_test.rb b/test/services/auto_sync_scheduler_test.rbnew file mode 100644index 0000000000..7781b71e9b--- /dev/null+++ b/test/services/auto_sync_scheduler_test.rb@@ -0,0 +1,12 @@+require "test_helper"++class AutoSyncSchedulerTest < ActiveSupport::TestCase+ # https://github.com/we-promise/sure/issues/1442+ test "registers the sync_all_accounts cron job as an ActiveJob" do+ Sidekiq::Cron::Job.expects(:create)+ .with(has_entries(name: AutoSyncScheduler::JOB_NAME, class: "SyncAllJob", active_job: true))+ .returns(stub(valid?: true))++ AutoSyncScheduler.upsert_job+ end+end