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.
Defining a job
Section titled “Defining a job”A job is a class that inherits from Tumbler::Job and implements perform:
class WelcomeEmailJob < Tumbler::Job async def perform(user_id: Int): Void user = User.find!(user_id) await Mailer.welcome(user).deliver endendEnqueuing
Section titled “Enqueuing”WelcomeEmailJob.enqueue(user.id) # as soon as possibleWelcomeEmailJob.enqueue(user.id, wait: 10.minutes) # laterenqueue 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.
Retries
Section titled “Retries”A job that raises is retried with exponential backoff, five times by default:
@[Retry(attempts: 10)]class FlakyApiJob < Tumbler::JobendAfter the last attempt, the job moves to the dead-letter queue, where you can inspect it, fix the bug, and pretend it never happened.
Running workers
Section titled “Running workers”rocks tumbler # start a workerIn development, rocks serve runs a worker in-process, so jobs work without extra
setup.