diff --git a/app/workers/html_variants/remove_old_data_worker.rb b/app/workers/html_variants/remove_old_data_worker.rb
new file mode 100644
index 000000000..462005d89
--- /dev/null
+++ b/app/workers/html_variants/remove_old_data_worker.rb
@@ -0,0 +1,15 @@
+module HtmlVariants
+ class RemoveOldDataWorker
+ include Sidekiq::Worker
+
+ sidekiq_options queue: :low_priority, retry: 15
+
+ def perform
+ HtmlVariantTrial.destroy_by("created_at < ?", 2.weeks.ago)
+ HtmlVariantSuccess.destroy_by("created_at < ?", 2.weeks.ago)
+ HtmlVariant.find_each do |html_variant|
+ html_variant.calculate_success_rate! if html_variant.html_variant_successes.any?
+ end
+ end
+ end
+end
diff --git a/config/schedule.yml b/config/schedule.yml
index 86b9cf09a..b07eb7859 100644
--- a/config/schedule.yml
+++ b/config/schedule.yml
@@ -62,6 +62,9 @@ award_contributor_badges_from_github:
- ""
- award_contributor_badges_from_github
- ""
+remove_old_html_variant_data:
+ cron: "10 * * * *" # every hour, 10 min after the hour
+ class: "HtmlVariants::RemoveOldDataWorker"
resave_supported_tags:
cron: "25 0 * * *" # daily at 12:25 am UTC
class: "Tags::ResaveSupportedTagsWorker"
diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake
index 7582cc1ee..9269e998a 100644
--- a/lib/tasks/fetch.rake
+++ b/lib/tasks/fetch.rake
@@ -20,14 +20,6 @@ task github_repo_fetch_all: :environment do
GithubRepo.update_to_latest
end
-task remove_old_html_variant_data: :environment do
- HtmlVariantTrial.destroy_by("created_at < ?", 2.weeks.ago)
- HtmlVariantSuccess.destroy_by("created_at < ?", 2.weeks.ago)
- HtmlVariant.find_each do |html_variant|
- html_variant.calculate_success_rate! if html_variant.html_variant_successes.any?
- end
-end
-
task fix_credits_count_cache: :environment do
Credit.counter_culture_fix_counts only: %i[user organization]
end
diff --git a/spec/factories/html_variant_successes.rb b/spec/factories/html_variant_successes.rb
new file mode 100644
index 000000000..258956db9
--- /dev/null
+++ b/spec/factories/html_variant_successes.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :html_variant_success do
+ article
+ html_variant
+ end
+end
diff --git a/spec/factories/html_variant_trials.rb b/spec/factories/html_variant_trials.rb
new file mode 100644
index 000000000..2807ecff0
--- /dev/null
+++ b/spec/factories/html_variant_trials.rb
@@ -0,0 +1,6 @@
+FactoryBot.define do
+ factory :html_variant_trial do
+ article
+ html_variant
+ end
+end
diff --git a/spec/workers/html_variants/remove_old_data_worker_spec.rb b/spec/workers/html_variants/remove_old_data_worker_spec.rb
new file mode 100644
index 000000000..4b5a84d17
--- /dev/null
+++ b/spec/workers/html_variants/remove_old_data_worker_spec.rb
@@ -0,0 +1,24 @@
+require "rails_helper"
+
+RSpec.describe HtmlVariants::RemoveOldDataWorker, type: :worker do
+ let(:worker) { subject }
+
+ include_examples "#enqueues_on_correct_queue", "low_priority"
+
+ describe "#perform" do
+ it "removes old html variants" do
+ Timecop.freeze do
+ old_trial_id = create(:html_variant_trial, created_at: 1.month.ago).id
+ old_success_id = create(:html_variant_success, created_at: 1.month.ago).id
+ new_trial = create(:html_variant_trial, created_at: Time.current)
+ new_trial.html_variant.update(success_rate: 0)
+
+ worker.perform
+
+ expect(HtmlVariantTrial.find_by(id: old_trial_id)).to be_nil
+ expect(HtmlVariantSuccess.find_by(id: old_success_id)).to be_nil
+ expect(HtmlVariantTrial.find_by(id: new_trial.id)).not_to be_nil
+ end
+ end
+ end
+end