From 9f676c76e105742ac00e0bc11da3bd17ca862697 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 24 Feb 2021 17:51:48 +0000 Subject: [PATCH 1/2] config: fix zeitwerk warning about DynamicSmtpSettingsInterceptor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix a warning when running tests: > DEPRECATION WARNING: Initialization autoloaded the constant DynamicSmtpSettingsInterceptor. > > Being able to do this is deprecated. Autoloading during initialization is going to be an error condition in future versions of Rails. > > Reloading does not reboot the application, and therefore code executed during > initialization does not run again. So, if you reload DynamicSmtpSettingsInterceptor, for example, > the expected changes won't be reflected in that stale Class object. > > This autoloaded constant has been unloaded. > > Please, check the "Autoloading and Reloading Constants" guide for solutions. However if we fix as recommanded, the interceptor will get added each time the classes are reloaded. And as the actual class instance changed after the reloading, they won't be de-duplicated – *and* there's no way to remove the old interceptor without having a reference to the (now-deleted) class. Instead we load the interceptor once, and add a message about the class not being auto-reloaded. --- app/models/dynamic_smtp_settings_interceptor.rb | 6 ++++++ config/initializers/dynamic_smtp_settings_interceptor.rb | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/dynamic_smtp_settings_interceptor.rb b/app/models/dynamic_smtp_settings_interceptor.rb index 885c4b8e9..91d6398f8 100644 --- a/app/models/dynamic_smtp_settings_interceptor.rb +++ b/app/models/dynamic_smtp_settings_interceptor.rb @@ -1,3 +1,9 @@ +# Note: this class is instanciated when being added as an interceptor +# during the app initialization. +# +# If you edit this file in development env, you will need to restart +# the app to see the changes. + class DynamicSmtpSettingsInterceptor def self.delivering_email(message) if ENV['SENDINBLUE_BALANCING'] == 'enabled' diff --git a/config/initializers/dynamic_smtp_settings_interceptor.rb b/config/initializers/dynamic_smtp_settings_interceptor.rb index a3f8e2d13..26a5d4d96 100644 --- a/config/initializers/dynamic_smtp_settings_interceptor.rb +++ b/config/initializers/dynamic_smtp_settings_interceptor.rb @@ -1 +1,3 @@ -ActionMailer::Base.register_interceptor "DynamicSmtpSettingsInterceptor" +ActiveSupport.on_load(:action_mailer) do + ActionMailer::Base.register_interceptor "DynamicSmtpSettingsInterceptor" +end From d36a68431565eb4d54da5ac1e36e6a8df4a7fc24 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 24 Feb 2021 19:01:27 +0100 Subject: [PATCH 2/2] config: fix zeitwekr warning when reloading the app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Turns out we need not only to load the Job constants later, but also not to do the same work twice – otherwise we'll get a > ApiEntreprise::Job constant is already defined when attempting to re-define the constant. --- .../initializers/active_job_compatibility.rb | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/config/initializers/active_job_compatibility.rb b/config/initializers/active_job_compatibility.rb index a73482124..9a90cfe50 100644 --- a/config/initializers/active_job_compatibility.rb +++ b/config/initializers/active_job_compatibility.rb @@ -11,22 +11,24 @@ # - but let's keep these for a while to make external integrators's life easier. # To keep some margin, let's say this file can be safely deleted in May 2021.) -require 'excon' - Rails.application.reloader.to_prepare do - module ApiEntreprise - Job = APIEntreprise::Job - AssociationJob = APIEntreprise::AssociationJob - AttestationFiscaleJob = APIEntreprise::AttestationFiscaleJob - AttestationSocialeJob = APIEntreprise::AttestationSocialeJob - BilansBdfJob = APIEntreprise::BilansBdfJob - EffectifsAnnuelsJob = APIEntreprise::EffectifsAnnuelsJob - EffectifsJob = APIEntreprise::EffectifsJob - EntrepriseJob = APIEntreprise::EntrepriseJob - ExercicesJob = APIEntreprise::ExercicesJob - end + if !defined?(ApiEntreprise) + require 'excon' - module Cron - FixMissingAntivirusAnalysis = FixMissingAntivirusAnalysisJob + module ApiEntreprise + Job = APIEntreprise::Job + AssociationJob = APIEntreprise::AssociationJob + AttestationFiscaleJob = APIEntreprise::AttestationFiscaleJob + AttestationSocialeJob = APIEntreprise::AttestationSocialeJob + BilansBdfJob = APIEntreprise::BilansBdfJob + EffectifsAnnuelsJob = APIEntreprise::EffectifsAnnuelsJob + EffectifsJob = APIEntreprise::EffectifsJob + EntrepriseJob = APIEntreprise::EntrepriseJob + ExercicesJob = APIEntreprise::ExercicesJob + end + + module Cron + FixMissingAntivirusAnalysis = FixMissingAntivirusAnalysisJob + end end end