1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob
Abdelkader Boudih a75f085941 Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709'
git-subtree-dir: activejob
git-subtree-mainline: b45b99894a
git-subtree-split: 14f74a8331
2014-08-12 09:17:19 +00:00
..
lib Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
test Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
.gitignore Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
.travis.yml Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
activejob.gemspec Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
CHANGELOG.md Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
Gemfile Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
Gemfile.lock Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
MIT-LICENSE Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
Rakefile Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00
README.md Add 'activejob/' from commit '14f74a8331f94150dfee653224de8fc837797709' 2014-08-12 09:17:19 +00:00

Active Job -- Make work happen later

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, billing charges, or mailings. Anything that can be chopped up into small units of work and run in parallel, really.

It also serves as the backend for ActionMailer's #deliver_later functionality that makes it easy to turn any mailing into a job for running later. That's one of the most common jobs in a modern web application: Sending emails outside of the request-response cycle, so the user doesn't have to wait on it.

The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner". We can then have framework features and other gems build on top of that, without having to worry about API differences between Delayed Job and Resque. Picking your queuing backend becomes more of an operational concern, then. And you'll be able to switch between them without having to rewrite your jobs.

Usage

Set the queue adapter for Active Job:

ActiveJob::Base.queue_adapter = :inline # default queue adapter
# Adapters currently supported: :backburner, :delayed_job, :qu, :que, :queue_classic,
#                               :resque, :sidekiq, :sneakers, :sucker_punch

Declare a job like so:

class MyJob < ActiveJob::Base
  queue_as :my_jobs

  def perform(record)
    record.do_work
  end
end

Enqueue a job like so:

MyJob.enqueue record  # Enqueue a job to be performed as soon the queueing system is free.
MyJob.enqueue_at Date.tomorrow.noon, record  # Enqueue a job to be performed tomorrow at noon.
MyJob.enqueue_in 1.week, record # Enqueue a job to be performed 1 week from now.

That's it!

GlobalID support

Active Job supports GlobalID serialization for parameters. This makes it possible to pass live Active Record objects to your job instead of class/id pairs, which you then have to manually deserialize. Before, jobs would look like this:

class TrashableCleanupJob
  def perform(trashable_class, trashable_id, depth)
    trashable = trashable_class.constantize.find(trashable_id)
    trashable.cleanup(depth)
  end
end

Now you can simply do:

class TrashableCleanupJob
  def perform(trashable, depth)
    trashable.cleanup(depth)
  end
end

This works with any class that mixes in ActiveModel::GlobalIdentification, which by default has been mixed into Active Record classes.

Supported queueing systems

We currently have adapters for:

Auxiliary gems

Under development as a gem, targeted for Rails inclusion

Active Job is currently being developed in a separate repository until it's ready to be merged in with Rails. The current plan is to have Active Job be part of the Rails 4.2 release, but plans may change depending on when this framework stabilizes and feels ready.

License

Active Job is released under the MIT license: