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)


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

def logger
  @_logger ||= begin
    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>)


158
159
160
# File 'lib/good_job/log_subscriber.rb', line 158

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.



180
181
182
# File 'lib/good_job/log_subscriber.rb', line 180

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.



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

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

  info do
    "GoodJob deleted #{deleted_records_count} preserved #{'job'.pluralize(deleted_records_count)} finished before #{timestamp}."
  end
end

#create(event) ⇒ void

This method returns an undefined value.

Responds to the create.good_job notification.



18
19
20
21
22
23
24
25
# File 'lib/good_job/log_subscriber.rb', line 18

def create(event)
  # FIXME: This method does not match any good_job notifications.
  good_job = event.payload[:good_job]

  debug do
    "GoodJob created job resource with id #{good_job.id}"
  end
end

#finished_job_task(event) ⇒ void

This method returns an undefined value.

Responds to the finished_job_task.good_job notification.



38
39
40
41
42
43
44
45
# File 'lib/good_job/log_subscriber.rb', line 38

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

  error do
    "GoodJob error: #{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.



28
29
30
31
32
33
34
35
# File 'lib/good_job/log_subscriber.rb', line 28

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

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

#loggerLogger

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

Returns:

  • (Logger)


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

def logger
  GoodJob::LogSubscriber.logger
end

#notifier_listen(_event) ⇒ void

This method returns an undefined value.

Responds to the notifier_listen.good_job notification.



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

def notifier_listen(_event)
  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.



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

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.



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

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

  error do
    "Notifier errored: #{error}"
  end
end

#notifier_unlisten(_event) ⇒ void

This method returns an undefined value.

Responds to the notifier_unlisten.good_job notification.



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

def notifier_unlisten(_event)
  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.



86
87
88
89
90
91
92
93
94
# File 'lib/good_job/log_subscriber.rb', line 86

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

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

#scheduler_create_pool(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_create_pool.good_job notification.



48
49
50
51
52
53
54
55
56
# File 'lib/good_job/log_subscriber.rb', line 48

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 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.



77
78
79
80
81
82
83
# File 'lib/good_job/log_subscriber.rb', line 77

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.



68
69
70
71
72
73
74
# File 'lib/good_job/log_subscriber.rb', line 68

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

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

#scheduler_shutdown_start(event) ⇒ void

This method returns an undefined value.

Responds to the scheduler_shutdown_start.good_job notification.



59
60
61
62
63
64
65
# File 'lib/good_job/log_subscriber.rb', line 59

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

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