[deploy] Refactor:Move Remove Old HTML Variant Data task to Sidekiq (#9925)
This commit is contained in:
parent
cbb85c430c
commit
8574153b07
6 changed files with 54 additions and 8 deletions
15
app/workers/html_variants/remove_old_data_worker.rb
Normal file
15
app/workers/html_variants/remove_old_data_worker.rb
Normal 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
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
6
spec/factories/html_variant_successes.rb
Normal file
6
spec/factories/html_variant_successes.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
FactoryBot.define do
|
||||
factory :html_variant_success do
|
||||
article
|
||||
html_variant
|
||||
end
|
||||
end
|
||||
6
spec/factories/html_variant_trials.rb
Normal file
6
spec/factories/html_variant_trials.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
FactoryBot.define do
|
||||
factory :html_variant_trial do
|
||||
article
|
||||
html_variant
|
||||
end
|
||||
end
|
||||
24
spec/workers/html_variants/remove_old_data_worker_spec.rb
Normal file
24
spec/workers/html_variants/remove_old_data_worker_spec.rb
Normal 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
|
||||
Loading…
Add table
Reference in a new issue