Skip to content

Tumbler (background jobs)

A rock tumbler takes rough stones and, after weeks of quiet rattling in the garage, gives you polished ones. Tumbler does the same for slow work: sending email, resizing images, calling that one API that takes eleven seconds. It runs them in the background, so your requests stay fast.

A job is a class that inherits from Tumbler::Job and implements perform:

app/jobs/welcome_email_job.am
class WelcomeEmailJob < Tumbler::Job
async def perform(user_id: Int): Void
user = User.find!(user_id)
await Mailer.welcome(user).deliver
end
end
WelcomeEmailJob.enqueue(user.id) # as soon as possible
WelcomeEmailJob.enqueue(user.id, wait: 10.minutes) # later

enqueue takes the same arguments as perform and is type-checked against it. Pass IDs rather than records: the job may run after the record has changed, and a stale record is how you end up emailing someone their old name.

A job that raises is retried with exponential backoff, five times by default:

@[Retry(attempts: 10)]
class FlakyApiJob < Tumbler::Job
end

After the last attempt, the job moves to the dead-letter queue, where you can inspect it, fix the bug, and pretend it never happened.

Terminal window
rocks tumbler # start a worker

In development, rocks serve runs a worker in-process, so jobs work without extra setup.