Skip to content
nesquena edited this page Jul 26, 2012 · 5 revisions

Backburner allows you to create jobs and place them on a beanstalk queue, and later pull those jobs off the queue and process them asynchronously. This guide walks you through the "happy path" to using Backburner.

First, if you haven't yet then you need to setup Backburner.

Enqueuing Jobs

The simplest way to enqueue jobs is by invoking the async method on any object which includes Backburner::Performable. Async enqueuing works for both instance and class methods on any performable object.

class User
  include Backburner::Performable
  queue "user-tasks" # queue defaults to "user"

  def activate(device_id)
    @device = Device.find(device_id)
    # ...
  end

  def self.reset_password(user_id)
    # ...
  end
end

# Async works for instance methods on a persisted model
@user = User.first
@user.async(:pri => 1000, :ttr => 100, :queue => "user.activate").activate(@device.id)

This will automatically enqueue a job for that user record that will run activate with the specified argument. You can also use async on class methods as well just as easily:

User.async(:pri => 100, :delay => 10.seconds).reset_password(@user.id)

The queue name used by default is the normalized class name (i.e {namespace}.user) if not otherwise specified. Note you are able to pass pri, ttr, delay and queue directly as options into async.

Working Jobs

The Backburner worker exists as a rake task:

require 'backburner/tasks'

so you can run:

$ bundle exec rake backburner:work
$ QUEUES=newsletter-sender,push-message bundle exec rake backburner:work

You can also just work jobs in ruby using:

Backburner.work

This will process jobs in all queues but you can also restrict processing to specific queues:

Backburner.work('newsletter_sender')
Clone this wiki locally