Class: GoodJob::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/good_job/log_subscriber.rb

Overview

Listens to GoodJob notifications and logs them.

Each method corresponds to the name of a notification. For example, when the Scheduler shuts down, it sends a notification named “scheduler_shutdown.good_job” and the #scheduler_shutdown method will be called here. See the ActiveSupport::LogSubscriber documentation for more.

Notifications collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerLogger

Represents all the loggers attached to GoodJob::LogSubscriber with a single logging interface. Writing to this logger is a shortcut for writing to each of the loggers in loggers.

Returns:

  • (Logger)


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/good_job/log_subscriber.rb', line 202

def logger
  @_logger ||= if defined?(ActiveSupport::BroadcastLogger)
                 ActiveSupport::BroadcastLogger.new(*loggers)
               else
                 logger = Logger.new(StringIO.new)
                 loggers.each do |each_logger|
                   logger.extend(ActiveSupport::Logger.broadcast(each_logger))
                 end
                 logger
               end
end

.loggersArray<Logger>

Tracks all loggers that GoodJob::LogSubscriber is writing to. You can write to multiple logs by appending to this array. After updating it, you should usually call reset_logger to make sure they are all written to.

Defaults to GoodJob.logger.

Examples:

Write to STDOUT and to a file:

GoodJob::LogSubscriber.loggers << ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
GoodJob::LogSubscriber.loggers << ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("log/my_logs.log"))
GoodJob::LogSubscriber.reset_logger

Returns:

  • (Array<Logger>)


194
195
196
# File 'lib/good_job/log_subscriber.rb', line 194

def loggers
  @_loggers ||= [GoodJob.logger]
end

.reset_loggervoid

This method returns an undefined value.

Reset logger and force it to rebuild a new shortcut to all the loggers in loggers. You should usually call this after modifying the loggers array.



218
219
220
# File 'lib/good_job/log_subscriber.rb', line 218

def reset_logger
  @_logger = nil
end

Instance Method Details

#cleanup_preserved_jobs(event) ⇒ void

This method returns an undefined value.

Responds to the cleanup_preserved_jobs.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


147
148
149
150
151
152
153
154
# File 'lib/good_job/log_subscriber.rb', line 147

def cleanup_preserved_jobs(event)
  timestamp = event.payload[:timestamp]
  destroyed_records_count = event.payload[:destroyed_records_count]

  info do
    "GoodJob destroyed #{destroyed_records_count} preserved job execution #{'records'.pluralize(destroyed_records_count)} finished before #{timestamp}."
  end
end

#cron_manager_start(event) ⇒ void

This method returns an undefined value.

Responds to the cron_manager_start.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


54
55
56
57
58
59
60
61
# File 'lib/good_job/log_subscriber.rb', line 54

def cron_manager_start(event)
  cron_entries = event.payload[:cron_entries]
  cron_jobs_count = cron_entries.size

  info do
    "GoodJob started cron with #{cron_jobs_count} #{'job'.pluralize(cron_jobs_count)}."
  end
end

#finished_job_task(event) ⇒ void

This method returns an undefined value.

Responds to the finished_job_task.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


33
34
35
36
37
38
39
40
# File 'lib/good_job/log_subscriber.rb', line 33

def finished_job_task(event)
  exception = event.payload[:error]
  return unless exception

  error do
    "GoodJob error: #{exception.class}: #{exception}\n #{exception.backtrace}"
  end
end

#finished_timer_task(event) ⇒ void

This method returns an undefined value.

Responds to the finished_timer_task.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


23
24
25
26
27
28
29
30
# File 'lib/good_job/log_subscriber.rb', line 23

def finished_timer_task(event)
  exception = event.payload[:error]
  return unless exception

  error do
    "GoodJob error: #{exception.class}: #{exception}\n #{exception.backtrace}"
  end
end

#loggerLogger

Get the logger associated with this GoodJob::LogSubscriber instance.

Returns:

  • (Logger)


178
179
180
# File 'lib/good_job/log_subscriber.rb', line 178

def logger
  GoodJob::LogSubscriber.logger
end

#notifier_listen(event) ⇒ void

This method returns an undefined value.

Responds to the notifier_listen.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


115
116
117
118
119
# File 'lib/good_job/log_subscriber.rb', line 115

def notifier_listen(event) # rubocop:disable Lint/UnusedMethodArgument
  info do
    "Notifier subscribed with LISTEN"
  end
end

#notifier_notified(event) ⇒ void

This method returns an undefined value.

Responds to the notifier_notified.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


122
123
124
125
126
127
128
# File 'lib/good_job/log_subscriber.rb', line 122

def notifier_notified(event)
  payload = event.payload[:payload]

  debug do
    "Notifier received payload: #{payload}"
  end
end

#notifier_notify_error(event) ⇒ void

This method returns an undefined value.

Responds to the notifier_notify_error.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


131
132
133
134
135
136
137
# File 'lib/good_job/log_subscriber.rb', line 131

def notifier_notify_error(event)
  exception = event.payload[:error]

  error do
    "Notifier errored: #{exception.class}: #{exception}\n #{exception.backtrace}"
  end
end

#notifier_unlisten(event) ⇒ void

This method returns an undefined value.

Responds to the notifier_unlisten.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


140
141
142
143
144
# File 'lib/good_job/log_subscriber.rb', line 140

def notifier_unlisten(event) # rubocop:disable Lint/UnusedMethodArgument
  info do
    "Notifier unsubscribed with UNLISTEN"
  end
end

#perform_job(event) ⇒ void

This method returns an undefined value.

Responds to the perform_job.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


104
105
106
107
108
109
110
111
112
# File 'lib/good_job/log_subscriber.rb', line 104

def perform_job(event)
  job = event.payload[:job]
  process_id = event.payload[:process_id]
  thread_name = event.payload[:thread_name]

  info(tags: [process_id, thread_name]) do
    "Executed GoodJob #{job.id}"
  end
end

#scheduler_create_pool(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_create_pool.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


43
44
45
46
47
48
49
50
51
# File 'lib/good_job/log_subscriber.rb', line 43

def scheduler_create_pool(event)
  max_threads = event.payload[:max_threads]
  performer_name = event.payload[:performer_name]
  process_id = event.payload[:process_id]

  info(tags: [process_id]) do
    "GoodJob #{GoodJob::VERSION} started scheduler with queues=#{performer_name} max_threads=#{max_threads}."
  end
end

#scheduler_restart_pools(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_restart_pools.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


95
96
97
98
99
100
101
# File 'lib/good_job/log_subscriber.rb', line 95

def scheduler_restart_pools(event)
  process_id = event.payload[:process_id]

  info(tags: [process_id]) do
    "GoodJob scheduler has restarted."
  end
end

#scheduler_shutdown(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_shutdown.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


73
74
75
76
77
78
79
# File 'lib/good_job/log_subscriber.rb', line 73

def scheduler_shutdown(event)
  process_id = event.payload[:process_id]

  info(tags: [process_id]) do
    "GoodJob scheduler is shut down."
  end
end

#scheduler_shutdown_kill(event) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/good_job/log_subscriber.rb', line 81

def scheduler_shutdown_kill(event)
  process_id = event.payload[:process_id]

  warn(tags: [process_id]) do
    active_job_ids = event.payload.fetch(:active_job_ids, [])
    if active_job_ids.any?
      "GoodJob scheduler has been killed. The following Active Jobs were interrupted: #{active_job_ids.join(' ')}"
    else
      "GoodJob scheduler has been killed."
    end
  end
end

#scheduler_shutdown_start(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_shutdown_start.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


64
65
66
67
68
69
70
# File 'lib/good_job/log_subscriber.rb', line 64

def scheduler_shutdown_start(event)
  process_id = event.payload[:process_id]

  info(tags: [process_id]) do
    "GoodJob shutting down scheduler..."
  end
end

#systemd_watchdog_error(event) ⇒ void

This method returns an undefined value.

Responds to the systemd_watchdog_error.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


166
167
168
169
170
171
172
# File 'lib/good_job/log_subscriber.rb', line 166

def systemd_watchdog_error(event)
  exception = event.payload[:error]

  error do
    "Error pinging systemd: #{exception.class}: #{exception}\n #{exception.backtrace}"
  end
end

#systemd_watchdog_start(event) ⇒ void

This method returns an undefined value.

Responds to the systemd_watchdog_start.good_job notification.

Parameters:

  • event (ActiveSupport::Notifications::Event)


157
158
159
160
161
162
163
# File 'lib/good_job/log_subscriber.rb', line 157

def systemd_watchdog_start(event)
  interval = event.payload[:interval]

  info do
    "Pinging systemd watchdog every #{interval.round(1)} seconds"
  end
end