class DelayedJobLogging::LogMessage

Attributes

job[R]

Public Class Methods

new(job) click to toggle source
# File lib/delayed_job_logging.rb, line 36
def initialize(job)
  @job = job
end

Public Instance Methods

log(status, exception: nil) click to toggle source
# File lib/delayed_job_logging.rb, line 40
def log(status, exception: nil)
  DelayedJobLogging.logger.info(
    ActiveSupport::JSON.encode(
      message_for(status, exception: exception)
    )
  )
end

Private Instance Methods

format_exception(exception) click to toggle source
# File lib/delayed_job_logging.rb, line 74
        def format_exception(exception)
  return unless exception

  "#{exception.class}: #{exception.message}\n" +
    exception.backtrace.join("\n")
end
job_arguments() click to toggle source
# File lib/delayed_job_logging.rb, line 64
        def job_arguments
  worker.instance_variables.inject({}) do |vars, var|
    vars.merge(var => worker.instance_variable_get(var))
  end
end
message_for(status, exception:) click to toggle source

The method constructs a hash. Imnsho all alternatives would be inferior to this. rubocop:disable Metrics/MethodLength

# File lib/delayed_job_logging.rb, line 50
        def message_for(status, exception:)
  {
    job_attempts: job.attempts,
    job_exception: format_exception(exception),
    job_id: job.id,
    job_worker_name: worker.class.name,
    job_arguments: job_arguments,
    job_priority: job.priority,
    job_queue: job.queue,
    job_run_at: (job.run_at || Time.now).iso8601,
    job_status: status
  }
end
worker() click to toggle source
# File lib/delayed_job_logging.rb, line 70
        def worker
  @_worker ||= YAML.load(job.handler)
end