class GoodJob::LogSubscriber

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 {api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html ActiveSupport::LogSubscriber} documentation for more.

Public Class Methods

logger() click to toggle source

Represents all the loggers attached to {LogSubscriber} with a single logging interface. Writing to this logger is a shortcut for writing to each of the loggers in {LogSubscriber.loggers}. @return [Logger]

    # File lib/good_job/log_subscriber.rb
178 def logger
179   @_logger ||= begin
180     logger = Logger.new(StringIO.new)
181     loggers.each do |each_logger|
182       logger.extend(ActiveSupport::Logger.broadcast(each_logger))
183     end
184     logger
185   end
186 end
loggers() click to toggle source

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

Defaults to {GoodJob.logger}. @return [Array<Logger>] @example 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
    # File lib/good_job/log_subscriber.rb
170 def loggers
171   @_loggers ||= [GoodJob.logger]
172 end
reset_logger() click to toggle source

Reset {LogSubscriber.logger} and force it to rebuild a new shortcut to all the loggers in {LogSubscriber.loggers}. You should usually call this after modifying the {LogSubscriber.loggers} array. @return [void]

    # File lib/good_job/log_subscriber.rb
192 def reset_logger
193   @_logger = nil
194 end

Public Instance Methods

cleanup_preserved_jobs(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
141 def cleanup_preserved_jobs(event)
142   timestamp = event.payload[:timestamp]
143   deleted_records_count = event.payload[:deleted_records_count]
144 
145   info do
146     "GoodJob deleted #{deleted_records_count} preserved #{'job'.pluralize(deleted_records_count)} finished before #{timestamp}."
147   end
148 end
create(event) click to toggle source

@!macro notification_responder

Responds to the +$0.good_job+ notification.
@param event [ActiveSupport::Notifications::Event]
@return [void]
   # File lib/good_job/log_subscriber.rb
20 def create(event)
21   # FIXME: This method does not match any good_job notifications.
22   good_job = event.payload[:good_job]
23 
24   debug do
25     "GoodJob created job resource with id #{good_job.id}"
26   end
27 end
cron_manager_start(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
61 def cron_manager_start(event)
62   cron_jobs = event.payload[:cron_jobs]
63   cron_jobs_count = cron_jobs.size
64 
65   info do
66     "GoodJob started cron with #{cron_jobs_count} #{'jobs'.pluralize(cron_jobs_count)}."
67   end
68 end
finished_job_task(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
40 def finished_job_task(event)
41   exception = event.payload[:error]
42   return unless exception
43 
44   error do
45     "GoodJob error: #{exception}\n #{exception.backtrace}"
46   end
47 end
finished_timer_task(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
30 def finished_timer_task(event)
31   exception = event.payload[:error]
32   return unless exception
33 
34   error do
35     "GoodJob error: #{exception}\n #{exception.backtrace}"
36   end
37 end
logger() click to toggle source

Get the logger associated with this {LogSubscriber} instance. @return [Logger]

    # File lib/good_job/log_subscriber.rb
154 def logger
155   GoodJob::LogSubscriber.logger
156 end
notifier_listen(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
109 def notifier_listen(event) # rubocop:disable Lint/UnusedMethodArgument
110   info do
111     "Notifier subscribed with LISTEN"
112   end
113 end
notifier_notified(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
116 def notifier_notified(event)
117   payload = event.payload[:payload]
118 
119   debug do
120     "Notifier received payload: #{payload}"
121   end
122 end
notifier_notify_error(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
125 def notifier_notify_error(event)
126   error = event.payload[:error]
127 
128   error do
129     "Notifier errored: #{error}"
130   end
131 end
notifier_unlisten(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
134 def notifier_unlisten(event) # rubocop:disable Lint/UnusedMethodArgument
135   info do
136     "Notifier unsubscribed with UNLISTEN"
137   end
138 end
perform_job(event) click to toggle source

@!macro notification_responder

    # File lib/good_job/log_subscriber.rb
 98 def perform_job(event)
 99   good_job = event.payload[:good_job]
100   process_id = event.payload[:process_id]
101   thread_name = event.payload[:thread_name]
102 
103   info(tags: [process_id, thread_name]) do
104     "Executed GoodJob #{good_job.id}"
105   end
106 end
scheduler_create_pool(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
50 def scheduler_create_pool(event)
51   max_threads = event.payload[:max_threads]
52   performer_name = event.payload[:performer_name]
53   process_id = event.payload[:process_id]
54 
55   info(tags: [process_id]) do
56     "GoodJob started scheduler with queues=#{performer_name} max_threads=#{max_threads}."
57   end
58 end
scheduler_restart_pools(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
89 def scheduler_restart_pools(event)
90   process_id = event.payload[:process_id]
91 
92   info(tags: [process_id]) do
93     "GoodJob scheduler has restarted."
94   end
95 end
scheduler_shutdown(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
80 def scheduler_shutdown(event)
81   process_id = event.payload[:process_id]
82 
83   info(tags: [process_id]) do
84     "GoodJob scheduler is shut down."
85   end
86 end
scheduler_shutdown_start(event) click to toggle source

@!macro notification_responder

   # File lib/good_job/log_subscriber.rb
71 def scheduler_shutdown_start(event)
72   process_id = event.payload[:process_id]
73 
74   info(tags: [process_id]) do
75     "GoodJob shutting down scheduler..."
76   end
77 end

Private Instance Methods

tag_logger(*tags, &block) click to toggle source

Add “GoodJob” plus any specified tags to every {ActiveSupport::TaggedLogging} logger in {LogSubscriber.loggers}. Tags are only applicable inside the block passed to this method. @yield [void] @return [void]

    # File lib/good_job/log_subscriber.rb
204 def tag_logger(*tags, &block)
205   tags = tags.dup.unshift("GoodJob").compact
206   good_job_tag = ["ActiveJob"].freeze
207 
208   self.class.loggers.inject(block) do |inner, each_logger|
209     if each_logger.respond_to?(:tagged)
210       tags_for_logger = if each_logger.formatter.current_tags.include?("ActiveJob")
211                           good_job_tag + tags
212                         else
213                           tags
214                         end
215 
216       proc { each_logger.tagged(*tags_for_logger, &inner) }
217     else
218       inner
219     end
220   end.call
221 end