diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b57292b31..688d2d668 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -24,6 +24,10 @@ class ApplicationController < ActionController::Base helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?, :administrateur_signed_in?, :current_administrateur, :current_account, :localization_enabled?, :set_locale + before_action do + Current.request_id = request.uuid + end + def staging_authenticate if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) } request_http_basic_authentication diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index 6b59ba733..691c33e40 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -3,11 +3,13 @@ class ApplicationJob < ActiveJob::Base DEFAULT_MAX_ATTEMPTS_JOBS = 25 - before_perform do |job| - Rails.logger.info("#{job.class.name} started at #{Time.zone.now}") - end + attr_writer :request_id - after_perform do |job| + around_perform do |job, block| + Rails.logger.info("#{job.class.name} started at #{Time.zone.now}") + Current.set(request_id: job.request_id) do + block.call + end Rails.logger.info("#{job.class.name} ended at #{Time.zone.now}") end @@ -18,4 +20,17 @@ class ApplicationJob < ActiveJob::Base def max_attempts ENV.fetch("MAX_ATTEMPTS_JOBS", DEFAULT_MAX_ATTEMPTS_JOBS).to_i end + + def request_id + @request_id ||= Current.request_id + end + + def serialize + super.merge('request_id' => request_id) + end + + def deserialize(job_data) + super + self.request_id = job_data['request_id'] + end end diff --git a/app/models/current.rb b/app/models/current.rb index 6d758d000..a4b8186bf 100644 --- a/app/models/current.rb +++ b/app/models/current.rb @@ -1,3 +1,3 @@ class Current < ActiveSupport::CurrentAttributes - attribute :instructeur, :administrateur + attribute :instructeur, :administrateur, :request_id end