123456789_123456789_123456789_123456789_123456789_

Class: ActiveJob::LogSubscriber

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveSupport::LogSubscriber
Defined in: activejob/lib/active_job/log_subscriber.rb

Constant Summary

::ActiveSupport::LogSubscriber - Inherited

BLACK, BLUE, CYAN, GREEN, LEVEL_CHECKS, MAGENTA, MODES, RED, WHITE, YELLOW

Class Attribute Summary

Class Method Summary

::ActiveSupport::LogSubscriber - Inherited

::ActiveSupport::Subscriber - Inherited

.attach_to

Attach the subscriber to a namespace.

.detach_from

Detach the subscriber from a namespace.

.method_added

Adds event subscribers for all new methods added to the class.

.new, .subscribers, .add_event_subscriber, .fetch_public_methods, .find_attached_subscriber, .invalid_event?, .pattern_subscribed?, .prepare_pattern, .remove_event_subscriber

Instance Attribute Summary

Instance Method Summary

Constructor Details

This class inherits a constructor from ActiveSupport::LogSubscriber

Class Attribute Details

.backtrace_cleaner (rw)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

.backtrace_cleaner?Boolean (rw)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

Instance Attribute Details

#backtrace_cleaner (rw)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

#backtrace_cleaner?Boolean (rw)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 7

class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

Instance Method Details

#args_info(job) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 145

def args_info(job)
  if job.class.log_arguments? && job.arguments.any?
    " with arguments: " +
      job.arguments.map { |arg| format(arg).inspect }.join(", ")
  else
    ""
  end
end

#discard(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 130

def discard(event)
  job = event.payload[:job]
  ex = event.payload[:error]

  error do
    "Discarded #{job.class} (Job ID: #{job.job_id}) due to a #{ex.class} (#{ex.message})."
  end
end

#enqueue(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 9

def enqueue(event)
  job = event.payload[:job]
  ex = event.payload[:exception_object] || job.enqueue_error

  if ex
    error do
      "Failed enqueuing #{job.class.name} to #{queue_name(event)}: #{ex.class} (#{ex.message})"
    end
  elsif event.payload[:aborted]
    info do
      "Failed enqueuing #{job.class.name} to #{queue_name(event)}, a before_enqueue callback halted the enqueuing execution."
    end
  else
    info do
      "Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)}" + args_info(job)
    end
  end
end

#enqueue_all(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 49

def enqueue_all(event)
  info do
    jobs = event.payload[:jobs]
    adapter = event.payload[:adapter]
    enqueued_count = event.payload[:enqueued_count]

    if enqueued_count == jobs.size
      enqueued_jobs_message(adapter, jobs)
    elsif jobs.any?(&:successfully_enqueued?)
      enqueued_jobs = jobs.select(&:successfully_enqueued?)

      failed_enqueue_count = jobs.size - enqueued_count
      if failed_enqueue_count == 0
        enqueued_jobs_message(adapter, enqueued_jobs)
      else
        "#{enqueued_jobs_message(adapter, enqueued_jobs)}. "\
          "Failed enqueuing #{failed_enqueue_count} #{'job'.pluralize(failed_enqueue_count)}"
      end
    else
      failed_enqueue_count = jobs.size - enqueued_count
      "Failed enqueuing #{failed_enqueue_count} #{'job'.pluralize(failed_enqueue_count)} "\
        "to #{ActiveJob.adapter_name(adapter)}"
    end
  end
end

#enqueue_at(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 29

def enqueue_at(event)
  job = event.payload[:job]
  ex = event.payload[:exception_object] || job.enqueue_error

  if ex
    error do
      "Failed enqueuing #{job.class.name} to #{queue_name(event)}: #{ex.class} (#{ex.message})"
    end
  elsif event.payload[:aborted]
    info do
      "Failed enqueuing #{job.class.name} to #{queue_name(event)}, a before_enqueue callback halted the enqueuing execution."
    end
  else
    info do
      "Enqueued #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)} at #{scheduled_at(event)}" + args_info(job)
    end
  end
end

#enqueue_retry(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 105

def enqueue_retry(event)
  job = event.payload[:job]
  ex = event.payload[:error]
  wait = event.payload[:wait]

  info do
    if ex
      "Retrying #{job.class} (Job ID: #{job.job_id}) after #{job.executions} attempts in #{wait.to_i} seconds, due to a #{ex.class} (#{ex.message})."
    else
      "Retrying #{job.class} (Job ID: #{job.job_id}) after #{job.executions} attempts in #{wait.to_i} seconds."
    end
  end
end

#enqueue_source_location (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 199

def enqueue_source_location
  Thread.each_caller_location do |location|
    frame = backtrace_cleaner.clean_frame(location)
    return frame if frame
  end
  nil
end

#enqueued_jobs_message(adapter, enqueued_jobs) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 207

def enqueued_jobs_message(adapter, enqueued_jobs)
  enqueued_count = enqueued_jobs.size
  job_classes_counts = enqueued_jobs.map(&:class).tally.sort_by { |_k, v| -v }
  "Enqueued #{enqueued_count} #{'job'.pluralize(enqueued_count)} to #{ActiveJob.adapter_name(adapter)}"\
    " (#{job_classes_counts.map { |klass, count| "#{count} #{klass}" }.join(', ')})"
end

#error(progname = nil, &block) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 183

def error(progname = nil, &block)
  return unless super

  if ActiveJob.verbose_enqueue_logs
    log_enqueue_source
  end
end

#format(arg) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 154

def format(arg)
  case arg
  when Hash
    arg.transform_values { |value| format(value) }
  when Array
    arg.map { |value| format(value) }
  when GlobalID::Identification
    arg.to_global_id rescue arg
  else
    arg
  end
end

#info(progname = nil, &block) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 175

def info(progname = nil, &block)
  return unless super

  if ActiveJob.verbose_enqueue_logs
    log_enqueue_source
  end
end

#log_enqueue_source (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 191

def log_enqueue_source
  source = enqueue_source_location

  if source
    logger.info("#{source}")
  end
end

#logger (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 171

def logger
  ActiveJob::Base.logger
end

#perform(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 86

def perform(event)
  job = event.payload[:job]
  ex = event.payload[:exception_object]
  if ex
    error do
      "Error performing #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)} in #{event.duration.round(2)}ms: #{ex.class} (#{ex.message}):\n" + Array(ex.backtrace).join("\n")
    end
  elsif event.payload[:aborted]
    error do
      "Error performing #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)} in #{event.duration.round(2)}ms: a before_perform callback halted the job execution"
    end
  else
    info do
      "Performed #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)} in #{event.duration.round(2)}ms"
    end
  end
end

#perform_start(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 76

def perform_start(event)
  info do
    job = event.payload[:job]
    enqueue_info = job.enqueued_at.present? ? " enqueued at #{job.enqueued_at.utc.iso8601(9)}" : ""

    "Performing #{job.class.name} (Job ID: #{job.job_id}) from #{queue_name(event)}" + enqueue_info + args_info(job)
  end
end

#queue_name(event) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 141

def queue_name(event)
  ActiveJob.adapter_name(event.payload[:adapter]) + "(#{event.payload[:job].queue_name})"
end

#retry_stopped(event)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 120

def retry_stopped(event)
  job = event.payload[:job]
  ex = event.payload[:error]

  error do
    "Stopped retrying #{job.class} (Job ID: #{job.job_id}) due to a #{ex.class} (#{ex.message}), which reoccurred on #{job.executions} attempts."
  end
end

#scheduled_at(event) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/log_subscriber.rb', line 167

def scheduled_at(event)
  Time.at(event.payload[:job].scheduled_at).utc
end