From 8574153b07cf0a3d4a9e17c0206c972af0276747 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Sat, 22 Aug 2020 18:36:22 -0500 Subject: [PATCH] [deploy] Refactor:Move Remove Old HTML Variant Data task to Sidekiq (#9925) --- .../html_variants/remove_old_data_worker.rb | 15 ++++++++++++ config/schedule.yml | 3 +++ lib/tasks/fetch.rake | 8 ------- spec/factories/html_variant_successes.rb | 6 +++++ spec/factories/html_variant_trials.rb | 6 +++++ .../remove_old_data_worker_spec.rb | 24 +++++++++++++++++++ 6 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 app/workers/html_variants/remove_old_data_worker.rb create mode 100644 spec/factories/html_variant_successes.rb create mode 100644 spec/factories/html_variant_trials.rb create mode 100644 spec/workers/html_variants/remove_old_data_worker_spec.rb 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