From 197fa54ea46a66d2cccba8d25223b4e872be9a04 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 27 Jul 2022 10:32:54 +0200 Subject: [PATCH] fix(archives): purge stuck pending archives Cf 0cb096b3d6440a4da05ca2902ed6fa72a93e6579 --- app/jobs/cron/purge_stale_archives_job.rb | 1 + app/models/archive.rb | 1 + spec/models/archive_spec.rb | 28 +++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/jobs/cron/purge_stale_archives_job.rb b/app/jobs/cron/purge_stale_archives_job.rb index 124c7935e..196002d69 100644 --- a/app/jobs/cron/purge_stale_archives_job.rb +++ b/app/jobs/cron/purge_stale_archives_job.rb @@ -3,5 +3,6 @@ class Cron::PurgeStaleArchivesJob < Cron::CronJob def perform Archive.stale(Archive::RETENTION_DURATION).destroy_all + Archive.stuck(Archive::MAX_DUREE_GENERATION).destroy_all end end diff --git a/app/models/archive.rb b/app/models/archive.rb index d4081d8f4..e6230b8b7 100644 --- a/app/models/archive.rb +++ b/app/models/archive.rb @@ -14,6 +14,7 @@ class Archive < ApplicationRecord include TransientModelsWithPurgeableJobConcern RETENTION_DURATION = 4.days + MAX_DUREE_GENERATION = 24.hours MAX_SIZE = 100.gigabytes has_and_belongs_to_many :groupe_instructeurs diff --git a/spec/models/archive_spec.rb b/spec/models/archive_spec.rb index c69ecdc5b..ee5cc6dd7 100644 --- a/spec/models/archive_spec.rb +++ b/spec/models/archive_spec.rb @@ -4,26 +4,34 @@ describe Archive do before { Timecop.freeze(Time.zone.now) } after { Timecop.return } - let(:archive) { create(:archive, job_status: :pending) } + let!(:archive) { create(:archive, job_status: :pending) } describe 'scopes' do describe 'staled' do - let(:recent_archive) { create(:archive, job_status: :pending) } - let(:staled_archive_still_pending) { create(:archive, job_status: :pending, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } - let(:staled_archive_still_failed) { create(:archive, job_status: :failed, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } - let(:staled_archive_still_generated) { create(:archive, job_status: :generated, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } + let!(:recent_archive) { create(:archive, job_status: :pending) } + let!(:staled_archive_still_pending) { create(:archive, job_status: :pending, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } + let!(:staled_archive_still_failed) { create(:archive, job_status: :failed, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } + let!(:staled_archive_still_generated) { create(:archive, job_status: :generated, updated_at: (Archive::RETENTION_DURATION + 2).days.ago) } subject do - archive - recent_archive - staled_archive_still_pending - staled_archive_still_failed - staled_archive_still_generated Archive.stale(Archive::RETENTION_DURATION) end it { is_expected.to match_array([staled_archive_still_failed, staled_archive_still_generated]) } end + + describe 'stuck' do + let!(:recent_archive) { create(:archive, job_status: :pending) } + let!(:staled_archive_still_pending) { create(:archive, job_status: :pending, updated_at: (Archive::MAX_DUREE_GENERATION + 2).days.ago) } + let!(:staled_archive_still_failed) { create(:archive, job_status: :failed, updated_at: (Archive::MAX_DUREE_GENERATION + 2).days.ago) } + let!(:staled_archive_still_generated) { create(:archive, job_status: :generated, updated_at: (Archive::MAX_DUREE_GENERATION + 2).days.ago) } + + subject do + Archive.stuck(Archive::MAX_DUREE_GENERATION) + end + + it { is_expected.to match_array([staled_archive_still_pending]) } + end end describe '.job_status' do