Merge pull request #7895 from tchak/feat-active-job-request-id

feat(jobs): add request_id to jobs payload
This commit is contained in:
Paul Chavard 2022-10-12 14:39:57 +02:00 committed by GitHub
commit 8f5a60e283
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View file

@ -24,6 +24,10 @@ class ApplicationController < ActionController::Base
helper_method :multiple_devise_profile_connect?, :instructeur_signed_in?, :current_instructeur, :current_expert, :expert_signed_in?, 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 :administrateur_signed_in?, :current_administrateur, :current_account, :localization_enabled?, :set_locale
before_action do
Current.request_id = request.uuid
end
def staging_authenticate def staging_authenticate
if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) } if StagingAuthService.enabled? && !authenticate_with_http_basic { |username, password| StagingAuthService.authenticate(username, password) }
request_http_basic_authentication request_http_basic_authentication

View file

@ -3,11 +3,13 @@ class ApplicationJob < ActiveJob::Base
DEFAULT_MAX_ATTEMPTS_JOBS = 25 DEFAULT_MAX_ATTEMPTS_JOBS = 25
before_perform do |job| attr_writer :request_id
Rails.logger.info("#{job.class.name} started at #{Time.zone.now}")
end
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}") Rails.logger.info("#{job.class.name} ended at #{Time.zone.now}")
end end
@ -18,4 +20,17 @@ class ApplicationJob < ActiveJob::Base
def max_attempts def max_attempts
ENV.fetch("MAX_ATTEMPTS_JOBS", DEFAULT_MAX_ATTEMPTS_JOBS).to_i ENV.fetch("MAX_ATTEMPTS_JOBS", DEFAULT_MAX_ATTEMPTS_JOBS).to_i
end 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 end

View file

@ -1,3 +1,3 @@
class Current < ActiveSupport::CurrentAttributes class Current < ActiveSupport::CurrentAttributes
attribute :instructeur, :administrateur attribute :instructeur, :administrateur, :request_id
end end