ruby
43 lines · 6 steps
Instrumenting every job in Rails
A base ActiveJob class that adds retry policy and structured, timed logging around every job's execution.
Explained by
highlit
1class ApplicationJob < ActiveJob::Base
2 retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
3 discard_on ActiveJob::DeserializationError
4
5 around_perform do |job, block|
6 metadata = {
7 job_id: job.job_id,
8 job_class: job.class.name,
9 queue: job.queue_name,
10 arguments: job.arguments.map { |arg| summarize(arg) },
11 executions: job.executions,
12 enqueued_at: job.enqueued_at
13 }
14
15 Rails.logger.tagged("job:#{job.class.name}", job.job_id) do
16 started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
17 Rails.logger.info(metadata.merge(event: "job.started").to_json)
18
19 begin
20 block.call
21 rescue => error
22 Rails.logger.error(metadata.merge(event: "job.failed", error: error.class.name, message: error.message).to_json)
23 raise
24 ensure
25 duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
26 Rails.logger.info(metadata.merge(event: "job.finished", duration_ms: (duration * 1000).round(2)).to_json)
27 end
28 end
29 end
30
31 private
32
33 def summarize(argument)
34 case argument
35 when ActiveRecord::Base
36 { gid: argument.to_gid_param }
37 when Hash
38 argument.transform_values { |value| summarize(value) }
39 else
40 argument
41 end
42 end
43end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1An around_perform callback is the natural place to wrap every job with cross-cutting concerns like logging and timing.
- 2Emitting JSON events with a monotonic clock gives you reliable, queryable metrics on job duration and failures.
- 3Sanitizing arguments before logging keeps sensitive records and payloads out of your logs while preserving traceability.
Related explainers
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class CreateOrderItems < ActiveRecord::Migration[7.1] def change create_table :order_items do |t| t.references :order, null: false, foreign_key: { on_delete: :cascade }
Enforcing order-item integrity in Rails
migrations
foreign-keys
validations
Intermediate
7 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
ruby
class ApacheLogParser LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/ TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"
Parsing Apache logs with named captures
regex
named-captures
parsing
Intermediate
6 steps
python
import time from flask import Flask, g, request
Timing requests with Flask hooks
middleware
request-lifecycle
instrumentation
Intermediate
5 steps
ruby
require "csv" def parse_transaction_row(line) fields = CSV.parse_line(line, headers: false, skip_blanks: true)
Parsing one CSV transaction row in Ruby
parsing
type-coercion
error-handling
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/instrumenting-every-job-in-rails-explained-ruby-68f4/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.