Slide 16
Slide 16 text
03 · Dispatching with ActiveJob
A plain ActiveJob — run by a physical device
class AnalysisDispatchJob < ApplicationJob
discard_on ActiveRecord::RecordNotFound
# device is gone: don't retry
retry_on StandardError, wait: :polynomially_longer, attempts: 3
def perform(device_id, params = {})
device = Device.find(device_id)
return :skipped unless device.online?
# backpressure, not an exception
job_id = SecureRandom.uuid
JobRun.create!(job_id:, device:, state: "running", started_at: Time.current)
device.transition_to!("busy")
# → Turbo Streams flips the badge
DeviceCommandChannel.broadcast_to(device, command: "start", job_id:, params:)
end
end
Backpressure — busy is a normal state for a machine, so skip, don't raise. Retry — transient failures,
spaced out. Discard — a deleted device is not worth retrying.
16