amelioration(ExpiredUsersDeletionService): limite le nombre d'envoie d'email sur le job de suppression des utilisateurs

This commit is contained in:
Martin 2023-11-04 08:45:40 +01:00 committed by mfo
parent 8790ac4978
commit b989af5b47
4 changed files with 24 additions and 13 deletions

View file

@ -2,7 +2,7 @@ class Cron::ExpiredUsersDeletionJob < Cron::CronJob
self.schedule_expression = "every day at 11 pm"
def perform(*args)
return if ENV['EXPIRE_USER_DELETION_JOB_DISABLED'].present?
return if ENV['EXPIRE_USER_DELETION_JOB_LIMIT'].blank?
ExpiredUsersDeletionService.process_expired
end
end

View file

@ -9,6 +9,8 @@ class ExpiredUsersDeletionService
delete_expired_users(expiring_segment)
send_inactive_close_to_expiration_notice(expiring_segment)
end
rescue => e
Sentry.capture_exception(e, extra: { user_id: user.id })
end
def send_inactive_close_to_expiration_notice(users)
@ -22,7 +24,11 @@ class ExpiredUsersDeletionService
def delete_expired_users(users)
to_delete_only(users).find_each do |user|
user.delete_and_keep_track_dossiers_also_delete_user(nil)
begin
user.delete_and_keep_track_dossiers_also_delete_user(nil)
rescue => e
Sentry.capture_exception(e, extra: { user_id: user.id })
end
end
end
@ -44,9 +50,14 @@ class ExpiredUsersDeletionService
def to_notify_only(users)
users.where(inactive_close_to_expiration_notice_sent_at: nil)
.limit(limit)
end
def to_delete_only(users)
users.where.not(inactive_close_to_expiration_notice_sent_at: RETENTION_AFTER_NOTICE_IN_WEEK.weeks.ago..)
end
def limit
(ENV['EXPIRE_USER_DELETION_JOB_LIMIT'] || 10_000).to_i
end
end

View file

@ -254,5 +254,5 @@ BULK_EMAIL_QUEUE="low_priority"
# work in progress about attestation_v2
WEASYPRINT_URL="http://10.33.23.204:5000/pdf"
# Use this env var to disable the user expiration mechanism
EXPIRE_USER_DELETION_JOB_DISABLED=anything
# Use this env var customize the max number of deleted user per day
EXPIRE_USER_DELETION_JOB_LIMIT=10000

View file

@ -1,19 +1,19 @@
describe Cron::ExpiredUsersDeletionJob do
subject { described_class.perform_now }
context 'when env[EXPIRE_USER_DELETION_JOB_DISABLED] is present' do
before { expect(ENV).to receive(:[]).with('EXPIRE_USER_DELETION_JOB_DISABLED').and_return('anything') }
context 'when env[EXPIRE_USER_DELETION_JOB_LIMIT] is present' do
before { expect(ENV).to receive(:[]).with('EXPIRE_USER_DELETION_JOB_LIMIT').and_return('anything') }
it 'does not call ExpiredUsersDeletionService.process_expired' do
expect(ExpiredUsersDeletionService).not_to receive(:process_expired)
subject
end
end
context 'when env[EXPIRE_USER_DELETION_JOB_DISABLED] is absent' do
it 'calls ExpiredUsersDeletionService.process_expired' do
expect(ExpiredUsersDeletionService).to receive(:process_expired)
subject
end
end
context 'when env[EXPIRE_USER_DELETION_JOB_LIMIT] is absent' do
it 'does not call ExpiredUsersDeletionService.process_expired' do
expect(ExpiredUsersDeletionService).not_to receive(:process_expired)
subject
end
end
end