ruby
36 lines · 8 steps
Resilient background jobs in Rails
An Active Job and a controller cooperate to send order confirmations reliably, with retries, guards, and strong params.
Explained by
highlit
1class OrderConfirmationJob < ApplicationJob
2 queue_as :mailers
3
4 retry_on Net::OpenTimeout, wait: :polynomially_longer, attempts: 5
5 discard_on ActiveJob::DeserializationError
6
7 def perform(order)
8 return if order.confirmation_sent?
9
10 OrderMailer.with(order: order).confirmation.deliver_later(wait: 5.seconds)
11
12 order.touch(:confirmation_sent_at)
13 end
14end
15
16class OrdersController < ApplicationController
17 def create
18 @order = current_user.orders.build(order_params)
19
20 if @order.save
21 OrderMailer.with(order: @order, user: current_user)
22 .confirmation
23 .deliver_later(queue: :mailers)
24
25 redirect_to @order, notice: "Your order is confirmed."
26 else
27 render :new, status: :unprocessable_entity
28 end
29 end
30
31 private
32
33 def order_params
34 params.require(:order).permit(:address_id, :payment_method, line_items: [:sku, :quantity])
35 end
36end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Declarative retry_on and discard_on let a job handle transient and permanent failures without manual rescue blocks.
- 2Guard clauses plus a persisted timestamp make a job idempotent so re-runs don't double-send.
- 3Enqueuing mail with deliver_later keeps request handling fast by pushing delivery off the web thread.
Related explainers
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
ruby
module DurationFormatter UNITS = [ ['week', 604_800], ['day', 86_400],
Turning seconds into human-readable durations in Ruby
greedy-decomposition
modular-arithmetic
formatting
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :post belongs_to :author, class_name: "User"
Live-updating comments with Turbo in Rails
turbo-streams
callbacks
associations
Intermediate
8 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
ruby
class ReportBatcher BATCH_SIZE = 500 def initialize(account)
Batching monthly email summaries in Rails
batching
service-object
background-jobs
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :commentable, polymorphic: true, counter_cache: true belongs_to :author, class_name: "User"
How polymorphic comments work in Rails
polymorphic-association
concerns
counter-cache
Intermediate
8 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/resilient-background-jobs-in-rails-explained-ruby-4701/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.