-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ActiveSupport::Notifications and LogSubscriber for logs
This is the same mechanism Rails uses to output logs from events. This allows for more instrumentation and easier customization of log output. The default log output is completely unchanged. Since a bunch of events need cursor_position to keep the existing logging, I made it a default tag for all events.
- Loading branch information
Showing
5 changed files
with
68 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
def logger | ||
ActiveJob::Base.logger | ||
end | ||
|
||
def nil_enumerator(event) | ||
info do | ||
"[JobIteration::Iteration] `build_enumerator` returned nil. Skipping the job." | ||
end | ||
end | ||
|
||
def not_found(event) | ||
info do | ||
"[JobIteration::Iteration] Enumerator found nothing to iterate! " \ | ||
"times_interrupted=#{event.payload[:times_interrupted]} cursor_position=#{event.payload[:cursor_position]}" | ||
end | ||
end | ||
|
||
def interrupted(event) | ||
info do | ||
"[JobIteration::Iteration] Interrupting and re-enqueueing the job " \ | ||
"cursor_position=#{event.payload[:cursor_position]}" | ||
end | ||
end | ||
|
||
def completed(event) | ||
info do | ||
message = "[JobIteration::Iteration] Completed iterating. times_interrupted=%d total_time=%.3f" | ||
Kernel.format(message, event.payload[:times_interrupted], event.payload[:total_time]) | ||
end | ||
end | ||
end | ||
end | ||
|
||
JobIteration::LogSubscriber.attach_to(:iteration) |