Skip to content

Commit

Permalink
amelioration(email): passe les jobs non prioritaire [appelons ça des …
Browse files Browse the repository at this point in the history
…bulk email], dans la queue de low_priority
  • Loading branch information
Martin committed Oct 18, 2023
1 parent dbb68f2 commit 6368fb6
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 35 deletions.
14 changes: 14 additions & 0 deletions app/jobs/priorized_mail_delivery_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class PriorizedMailDeliveryJob < ActionMailer::MailDeliveryJob
def queue_name
mailer, action_name = @arguments
if mailer.constantize.critical_email?(action_name)
super
else
custom_queue
end
end

def custom_queue
ENV.fetch('BULK_EMAIL_QUEUE') { Rails.application.config.action_mailer.deliver_later_queue_name }
end
end
4 changes: 1 addition & 3 deletions app/mailers/administrateur_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ def api_token_expiration(user, tokens)
reply_to: CONTACT_EMAIL)
end

private

def forced_delivery_for_action?
def self.critical_email?(action_name)
action_name == "activate_before_expiration"
end
end
4 changes: 4 additions & 0 deletions app/mailers/administration_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ def refuse_admin(admin_email)
subject: subject,
reply_to: CONTACT_EMAIL)
end

def self.critical_email?(action_name)
false
end
end
1 change: 1 addition & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ApplicationMailer < ActionMailer::Base
include MailerDolistConcern
include MailerMonitoringConcern
include BalancedDeliveryConcern
include PriorityDeliveryConcern

helper :application # gives access to all helpers defined within `application_helper`.
default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>"
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/avis_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ def avis_invitation(avis, targeted_user_link = nil) # ensure re-entrance if exis
mail(to: email, subject: subject)
end
end

def self.critical_email?(action_name)
false
end
end
14 changes: 7 additions & 7 deletions app/mailers/concerns/balanced_delivery_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module BalancedDeliveryConcern
extend ActiveSupport::Concern

included do
before_action :add_delivery_method, if: :forced_delivery?
before_action :add_delivery_method, if: :forced_delivery_provider?

private

def forced_delivery_for_action?
false
def critical_email?
self.class.critical_email?(action_name)
end

def forced_delivery?
SafeMailer.forced_delivery_method.present? && forced_delivery_for_action?
private

def forced_delivery_provider?
SafeMailer.forced_delivery_method.present? && critical_email?
end

def add_delivery_method
Expand Down
10 changes: 10 additions & 0 deletions app/mailers/concerns/priority_delivery_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module PriorityDeliveryConcern
extend ActiveSupport::Concern
included do
self.delivery_job = PriorizedMailDeliveryJob

def self.critical_email?(action_name)
raise NotImplementedError
end
end
end
4 changes: 3 additions & 1 deletion app/mailers/devise_user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class DeviseUserMailer < Devise::Mailer
include MailerDolistConcern
include MailerMonitoringConcern
include BalancedDeliveryConcern
include PriorityDeliveryConcern

layout 'mailers/layout'

def template_paths
Expand All @@ -19,7 +21,7 @@ def confirmation_instructions(record, token, opts = {})
super
end

def forced_delivery_for_action?
def self.critical_email?(action_name)
true
end
end
4 changes: 4 additions & 0 deletions app/mailers/dossier_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ def notify_transfer(transfer)
end
end

def self.critical_email?(action_name)
false
end

protected

def prevent_perform_deliveries
Expand Down
5 changes: 5 additions & 0 deletions app/mailers/expert_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def send_dossier_decision_v2(avis)

mail(template_name: 'send_dossier_decision', to: email, subject: subject)
end

def self.critical_email?(action_name)
false
end

end

def decision_dossier(dossier)
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/groupe_gestionnaire_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def notify_added_gestionnaires(groupe_gestionnaire, added_gestionnaires, current

mail(bcc: added_gestionnaire_emails, subject: subject)
end

def self.critical_email?(action_name)
false
end
end
4 changes: 4 additions & 0 deletions app/mailers/groupe_instructeur_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ def notify_added_instructeurs(group, added_instructeurs, current_instructeur_ema

mail(bcc: added_instructeur_emails, subject: subject)
end

def self.critical_email?(action_name)
false
end
end
4 changes: 1 addition & 3 deletions app/mailers/instructeur_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def send_notifications(instructeur, data)
mail(to: instructeur.email, subject: subject)
end

private

def forced_delivery_for_action?
def self.critical_email?(action_name)
action_name == "send_login_token"
end
end
2 changes: 1 addition & 1 deletion app/mailers/invite_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def send_mail(invite, subject, reply_to)
reply_to: reply_to)
end

def forced_delivery_for_action?
def self.critical_email?(action_name)
true
end
end
4 changes: 4 additions & 0 deletions app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def self.send_pending_correction(dossier)
with(dossier: dossier).send_notification
end

def self.critical_email?(action_name)
false
end

private

def set_services_publics_plus
Expand Down
5 changes: 5 additions & 0 deletions app/mailers/preactivate_users_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ def reinvite(model, model_name)
reply_to: CONTACT_EMAIL,
body: body)
end

def self.critical_email?(action_name)
false
end

end
5 changes: 5 additions & 0 deletions app/mailers/resend_attestation_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def resend_attestation(dossier)
mail(to: to, subject: subject, body: body(dossier))
end

def self.critical_email?(action_name)
false
end


private

def body(dossier)
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/super_admin_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ def dolist_report(to, csv_path)

mail(to: to, subject: "Dolist report", body: "Ci-joint le rapport d'emails récents envoyés via Dolist.")
end

def self.critical_email?(action_name)
false
end
end
2 changes: 1 addition & 1 deletion app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def send_archive(administrateur_or_instructeur, procedure, archive)
mail(to: administrateur_or_instructeur.email, subject: subject)
end

def forced_delivery_for_action?
def self.critical_email?(action_name)
['france_connect_merge_confirmation', "new_account_warning", "ask_for_merge", "invite_instructeur"].include?(action_name)
end
end
1 change: 1 addition & 0 deletions config/env.example.optional
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,4 @@ DS_LOG_LEVEL='info'
# Admins group usage (gestionnaire de groupes d'administrateurs)
# can be removed if needed when EVERY PARTS of the feature will be merged / only used in routes.rb
ADMINS_GROUP_ENABLED="disabled"
BULK_EMAIL_QUEUE="low_priority"
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
helo: ENV['HELO_ENABLED'] == 'enabled' ? 100 : 0,
letter_opener: ENV['HELO_ENABLED'] == 'enabled' ? 0 : 100
}
config.action_mailer.delivery_method = :balancer
config.action_mailer.delivery_method = :letter_opener_web

config.action_mailer.default_url_options = { host: ENV.fetch("APP_HOST") }
config.action_mailer.asset_host = "http://" + ENV.fetch("APP_HOST")
Expand Down
29 changes: 27 additions & 2 deletions spec/mailers/administrateur_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
RSpec.describe AdministrateurMailer, type: :mailer do
let(:procedure) { create(:procedure) }
let(:admin_email) { 'administrateur@email.fr' }

describe '.notify_procedure_expires_when_termine_forced' do
subject { described_class.notify_procedure_expires_when_termine_forced(admin_email, procedure) }

it { expect(subject.to).to eq([admin_email]) }
it { expect(subject.subject).to include("La suppression automatique des dossiers a été activée sur la démarche") }

context 'when perform_later is called' do
let(:custom_queue) { 'low_priority' }
before { ENV['BULK_EMAIL_QUEUE'] = custom_queue }
it 'enqueues email is custom queue for low priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
end
end
end

describe '.activate_before_expiration' do
let(:user) { create(:user, reset_password_sent_at: 2.days.ago) }
let(:token) { SecureRandom.hex }
subject { described_class.activate_before_expiration(user, token) }

context 'without SafeMailer configured' do
subject { described_class.activate_before_expiration(user, token) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
end

context 'with SafeMailer configured' do
let(:forced_delivery_method) { :kikoo }
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
subject { described_class.activate_before_expiration(user, token) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
end

context 'when perform_later is called' do
it 'enqueues email in default queue for high priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
end
end
end

describe '.notify_service_without_siret' do
subject { described_class.notify_service_without_siret(admin_email) }

it { expect(subject.to).to eq([admin_email]) }
it { expect(subject.subject).to eq("Siret manquant sur un de vos services") }
it { expect(subject.body).to include("un de vos services n'a pas son siret renseigné") }

context 'when perform_later is called' do
let(:custom_queue) { 'low_priority' }
before { ENV['BULK_EMAIL_QUEUE'] = custom_queue }
it 'enqueues email is custom queue for low priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
end
end
end
end
11 changes: 9 additions & 2 deletions spec/mailers/devise_user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
let(:user) { create(:user) }
let(:token) { SecureRandom.hex }
describe '.confirmation_instructions' do
subject { described_class.confirmation_instructions(user, token, opts = {}) }

context 'without SafeMailer configured' do
subject { described_class.confirmation_instructions(user, token, opts = {}) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
end

context 'with SafeMailer configured' do
let(:forced_delivery_method) { :kikoo }
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
subject { described_class.confirmation_instructions(user, token, opts = {}) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
end

context 'when perform_later is called' do
it 'enqueues email in default queue for high priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
end
end
end
end
28 changes: 25 additions & 3 deletions spec/mailers/instructeur_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@
let(:sender) { create(:instructeur) }
let(:recipient) { create(:instructeur) }
let(:dossier) { create(:dossier) }

subject { described_class.send_dossier(sender, dossier, recipient) }

it { expect(subject.body).to include('Bonjour') }

context 'when perform_later is called' do
let(:custom_queue) { 'low_priority' }
before { ENV['BULK_EMAIL_QUEUE'] = custom_queue }

it 'enqueues email is custom queue for low priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
end
end
end

describe '#send_login_token' do
let(:user) { create(:instructeur) }
let(:token) { SecureRandom.hex }
subject { described_class.send_login_token(user, token) }

context 'without SafeMailer configured' do
subject { described_class.send_login_token(user, token) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(nil) }
end

context 'with SafeMailer configured' do
let(:forced_delivery_method) { :kikoo }
before { allow(SafeMailer).to receive(:forced_delivery_method).and_return(forced_delivery_method) }
subject { described_class.send_login_token(user, token) }
it { expect(subject[BalancerDeliveryMethod::FORCE_DELIVERY_METHOD_HEADER]&.value).to eq(forced_delivery_method.to_s) }
end

context 'when perform_later is called' do
it 'enqueues email in default queue for high priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(Rails.application.config.action_mailer.deliver_later_queue_name)
end
end
end

describe '#last_week_overview' do
Expand Down Expand Up @@ -62,5 +75,14 @@
expect(subject.body).to be_blank
end
end

context 'when perform_later is called' do
let(:custom_queue) { 'low_priority' }
before { ENV['BULK_EMAIL_QUEUE'] = custom_queue }

it 'enqueues email is custom queue for low priority delivery' do
expect { subject.deliver_later }.to have_enqueued_job.on_queue(custom_queue)
end
end
end
end
Loading

0 comments on commit 6368fb6

Please sign in to comment.