[deploy] Refactor:Move Remove Old HTML Variant Data task to Sidekiq (#9925)

This commit is contained in:
Molly Struve 2020-08-22 18:36:22 -05:00 committed by GitHub
parent cbb85c430c
commit 8574153b07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 8 deletions

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -0,0 +1,6 @@
FactoryBot.define do
factory :html_variant_success do
article
html_variant
end
end

View file

@ -0,0 +1,6 @@
FactoryBot.define do
factory :html_variant_trial do
article
html_variant
end
end

View file

@ -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