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 "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 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.