From 80a12db1913c8ce568a6d6464bec75bcfd4e232b Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 31 Mar 2026 11:20:23 +1100 Subject: [PATCH] Move database clean from cron to Sidekiq scheduler After moving the remaining tasks from schedule.rb to sidekiq.yml, we can remove whenever and won't rely on cron any more. That will simplify the setup and migration to a new server. --- app/jobs/rake_job.rb | 13 +++++++++++++ config/schedule.rb | 4 ---- config/sidekiq.yml | 3 +++ spec/jobs/rake_job_spec.rb | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 app/jobs/rake_job.rb create mode 100644 spec/jobs/rake_job_spec.rb diff --git a/app/jobs/rake_job.rb b/app/jobs/rake_job.rb new file mode 100644 index 0000000000..f522abd0a0 --- /dev/null +++ b/app/jobs/rake_job.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# Executes a rake task +class RakeJob < ApplicationJob + def perform(task_string) + Rails.application.load_tasks if Rake::Task.tasks.empty? + + Rake.application.invoke_task(task_string) + ensure + name, _args = Rake.application.parse_task_string(task_string) + Rake::Task[name].reenable + end +end diff --git a/config/schedule.rb b/config/schedule.rb index 20c240f5ff..a07f4d9972 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -11,10 +11,6 @@ env "MAILTO", ENV["SCHEDULE_NOTIFICATIONS"] if ENV["SCHEDULE_NOTIFICATIONS"] # If we use -e with a file containing specs, rspec interprets it and filters out our examples job_type :run_file, "cd :path; :environment_variable=:environment bundle exec script/rails runner :task :output" -every 1.month, at: '4:30am' do - rake 'ofn:data:remove_transient_data' -end - every 1.day, at: '2:45am' do rake 'db2fog:clean' if ENV['S3_BACKUPS_BUCKET'] end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index bcd4e31909..a2de4a4f97 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -19,3 +19,6 @@ every: "5m" OrderCycleClosingJob: every: "5m" + RakeJob: + args: ["ofn:data:remove_transient_data"] + cron: "30 4 1 * *" diff --git a/spec/jobs/rake_job_spec.rb b/spec/jobs/rake_job_spec.rb new file mode 100644 index 0000000000..9430c116d4 --- /dev/null +++ b/spec/jobs/rake_job_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "tasks/data/remove_transient_data" + +RSpec.describe RakeJob do + let(:task_string) { "ofn:data:remove_transient_data" } + + it "calls the removal service" do + expect(RemoveTransientData).to receive(:new).and_call_original + RakeJob.perform_now(task_string) + end + + it "can be called several times" do + expect(RemoveTransientData).to receive(:new).twice.and_call_original + RakeJob.perform_now(task_string) + RakeJob.perform_now(task_string) + end +end