Class: ActiveJob::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber show all
Defined in:
activejob/lib/active_job/log_subscriber.rb

Overview

:nodoc:

Constant Summary

Constants inherited from ActiveSupport::LogSubscriber

ActiveSupport::LogSubscriber::BLACK, ActiveSupport::LogSubscriber::BLUE, ActiveSupport::LogSubscriber::CYAN, ActiveSupport::LogSubscriber::GREEN, ActiveSupport::LogSubscriber::LEVEL_CHECKS, ActiveSupport::LogSubscriber::MAGENTA, ActiveSupport::LogSubscriber::MODES, ActiveSupport::LogSubscriber::RED, ActiveSupport::LogSubscriber::WHITE, ActiveSupport::LogSubscriber::YELLOW

Instance Attribute Summary

Attributes inherited from ActiveSupport::LogSubscriber

#event_levels

Attributes inherited from ActiveSupport::Subscriber

#patterns

Instance Method Summary collapse

Methods inherited from ActiveSupport::LogSubscriber

attach_to, #call, flush_all!, #initialize, log_subscribers, #publish_event, #silenced?

Methods inherited from ActiveSupport::Subscriber

attach_to, #call, detach_from, #initialize, method_added, #publish_event, subscribers

Constructor Details

This class inherits a constructor from ActiveSupport::LogSubscriber

Instance Method Details

#discard(event) ⇒ Object



130
131
132
133
134
135
136
137
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

#perform(event) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



76
77
78
79
80
81
82
83
# 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

#retry_stopped(event) ⇒ Object



120
121
122
123
124
125
126
127
# 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