Class: LogStruct::Integrations::ActiveJob::LogSubscriber

Inherits:
ActiveJob::LogSubscriber
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/log_struct/integrations/active_job/log_subscriber.rb

Overview

Structured logging for ActiveJob

Instance Method Summary collapse

Instance Method Details

#enqueue(event) ⇒ void

This method returns an undefined value.

Parameters:

  • event (::ActiveSupport::Notifications::Event)


17
18
19
20
21
22
23
24
25
# File 'lib/log_struct/integrations/active_job/log_subscriber.rb', line 17

def enqueue(event)
  job = T.cast(event.payload[:job], ::ActiveJob::Base)
  ts = event.time ? Time.at(event.time) : Time.now
  base_fields = build_base_fields(job)
  logger.info(Log::ActiveJob::Enqueue.new(
    **base_fields.to_kwargs,
    timestamp: ts
  ))
end

#enqueue_at(event) ⇒ void

This method returns an undefined value.

Parameters:

  • event (::ActiveSupport::Notifications::Event)


28
29
30
31
32
33
34
35
36
37
# File 'lib/log_struct/integrations/active_job/log_subscriber.rb', line 28

def enqueue_at(event)
  job = T.cast(event.payload[:job], ::ActiveJob::Base)
  ts = event.time ? Time.at(event.time) : Time.now
  base_fields = build_base_fields(job)
  logger.info(Log::ActiveJob::Schedule.new(
    **base_fields.to_kwargs,
    scheduled_at: job.scheduled_at,
    timestamp: ts
  ))
end

#perform(event) ⇒ void

This method returns an undefined value.

Parameters:

  • event (::ActiveSupport::Notifications::Event)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/log_struct/integrations/active_job/log_subscriber.rb', line 40

def perform(event)
  job = T.cast(event.payload[:job], ::ActiveJob::Base)
  exception = event.payload[:exception_object]

  if exception
    # Log the exception with the job context
    log_exception(exception, job, event)
  else
    start_float = event.time
    end_float = event.end
    ts = start_float ? Time.at(start_float) : Time.now
    finished_at = end_float ? Time.at(end_float) : Time.now
    base_fields = build_base_fields(job)
    logger.info(Log::ActiveJob::Finish.new(
      **base_fields.to_kwargs,
      duration_ms: event.duration.to_f,
      finished_at: finished_at,
      timestamp: ts
    ))
  end
end

#perform_start(event) ⇒ void

This method returns an undefined value.

Parameters:

  • event (::ActiveSupport::Notifications::Event)


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/log_struct/integrations/active_job/log_subscriber.rb', line 63

def perform_start(event)
  job = T.cast(event.payload[:job], ::ActiveJob::Base)
  ts = event.time ? Time.at(event.time) : Time.now
  started_at = ts
  attempt = job.executions
  base_fields = build_base_fields(job)
  logger.info(Log::ActiveJob::Start.new(
    **base_fields.to_kwargs,
    started_at: started_at,
    attempt: attempt,
    timestamp: ts
  ))
end