poc(batch_operation.purge): nettoie les batch terminé ou inutiles
This commit is contained in:
parent
6556fda218
commit
ea8bd13bcf
4 changed files with 39 additions and 0 deletions
8
app/jobs/cron/purge_stale_batch_operation_job.rb
Normal file
8
app/jobs/cron/purge_stale_batch_operation_job.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class Cron::PurgeStaleBatchOperationJob < Cron::CronJob
|
||||||
|
self.schedule_expression = "every 5 minutes"
|
||||||
|
|
||||||
|
def perform
|
||||||
|
BatchOperation.stale.destroy_all
|
||||||
|
BatchOperation.stuck.destroy_all
|
||||||
|
end
|
||||||
|
end
|
|
@ -26,6 +26,19 @@ class BatchOperation < ApplicationRecord
|
||||||
|
|
||||||
validates :operation, presence: true
|
validates :operation, presence: true
|
||||||
|
|
||||||
|
RETENTION_DURATION = 4.hours
|
||||||
|
MAX_DUREE_GENERATION = 24.hours
|
||||||
|
|
||||||
|
scope :stale, lambda {
|
||||||
|
where.not(finished_at: nil)
|
||||||
|
.where('updated_at < ?', (Time.zone.now - RETENTION_DURATION))
|
||||||
|
}
|
||||||
|
|
||||||
|
scope :stuck, lambda {
|
||||||
|
where(finished_at: nil)
|
||||||
|
.where('updated_at < ?', (Time.zone.now - MAX_DUREE_GENERATION))
|
||||||
|
}
|
||||||
|
|
||||||
def dossiers_safe_scope(dossier_ids = self.dossier_ids)
|
def dossiers_safe_scope(dossier_ids = self.dossier_ids)
|
||||||
query = Dossier.joins(:procedure)
|
query = Dossier.joins(:procedure)
|
||||||
.where(procedure: { id: instructeur.procedures.ids })
|
.where(procedure: { id: instructeur.procedures.ids })
|
||||||
|
@ -60,6 +73,7 @@ class BatchOperation < ApplicationRecord
|
||||||
values = []
|
values = []
|
||||||
values.push([arel_table[:run_at], Time.zone.now]) if called_for_first_time?
|
values.push([arel_table[:run_at], Time.zone.now]) if called_for_first_time?
|
||||||
values.push([arel_table[:finished_at], Time.zone.now]) if called_for_last_time?
|
values.push([arel_table[:finished_at], Time.zone.now]) if called_for_last_time?
|
||||||
|
values.push([arel_table[:updated_at], Time.zone.now])
|
||||||
if success
|
if success
|
||||||
values.push([arel_table[:success_dossier_ids], Arel::Nodes::NamedFunction.new('array_append', [arel_table[:success_dossier_ids], dossier.id])])
|
values.push([arel_table[:success_dossier_ids], Arel::Nodes::NamedFunction.new('array_append', [arel_table[:success_dossier_ids], dossier.id])])
|
||||||
values.push([arel_table[:failed_dossier_ids], Arel::Nodes::NamedFunction.new('array_remove', [arel_table[:failed_dossier_ids], dossier.id])])
|
values.push([arel_table[:failed_dossier_ids], Arel::Nodes::NamedFunction.new('array_remove', [arel_table[:failed_dossier_ids], dossier.id])])
|
||||||
|
|
|
@ -4,6 +4,8 @@ FactoryBot.define do
|
||||||
invalid_instructeur { nil }
|
invalid_instructeur { nil }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
association :instructeur
|
||||||
|
|
||||||
trait :archiver do
|
trait :archiver do
|
||||||
operation { BatchOperation.operations.fetch(:archiver) }
|
operation { BatchOperation.operations.fetch(:archiver) }
|
||||||
after(:build) do |batch_operation, evaluator|
|
after(:build) do |batch_operation, evaluator|
|
||||||
|
|
|
@ -170,4 +170,19 @@ describe BatchOperation, type: :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'stale' do
|
||||||
|
let(:finished_at) { 6.hours.ago }
|
||||||
|
let(:staled_batch_operation) { create(:batch_operation, operation: :archiver, finished_at: 2.days.ago, updated_at: 2.days.ago) }
|
||||||
|
it 'finds stale jobs' do
|
||||||
|
expect(BatchOperation.stale).to match_array(staled_batch_operation)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'stuck' do
|
||||||
|
let(:stuck_batch_operation) { create(:batch_operation, operation: :archiver, finished_at: nil, updated_at: 2.days.ago) }
|
||||||
|
it 'finds stale jobs' do
|
||||||
|
expect(BatchOperation.stuck).to match_array(stuck_batch_operation)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue