From afb3d4f9e5820cb8f5cd4502e1d0525044ac6ef2 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 19 May 2014 12:46:27 +0200 Subject: [PATCH] Move to instance method and document usage --- README.md | 25 +++++++++++++++++-- .../queue_adapters/delayed_job_adapter.rb | 2 +- .../queue_adapters/inline_adapter.rb | 2 +- .../queue_adapters/resque_adapter.rb | 2 +- .../queue_adapters/sidekiq_adapter.rb | 2 +- .../queue_adapters/sucker_punch_adapter.rb | 2 +- test/jobs/gid_job.rb | 2 +- test/jobs/hello_job.rb | 2 +- 8 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 53cd1e86b6..dbd31d91ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,27 @@ 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. +## Usage + +Declare a job like so: + +```ruby +class MyJob < ActiveJob::Base + def perform(record) + record.do_work + end +end +``` + +Enqueue a job like so: + +```ruby +MyJob.enqueue record +``` + +That's it! + + ## GlobalID support Active Job supports GlobalID serialization for parameters. This makes it possible @@ -19,7 +40,7 @@ you then have to manually deserialize. Before, jobs would look like this: ```ruby class TrashableCleanupJob - def self.perfom(trashable_class, trashable_id, depth) + def perfom(trashable_class, trashable_id, depth) trashable = trashable_class.constantize.find(trashable_id) trashable.cleanup(depth) end @@ -30,7 +51,7 @@ Now you can simply do: ```ruby class TrashableCleanupJob - def self.perfom(trashable, depth) + def perfom(trashable, depth) trashable.cleanup(depth) end end diff --git a/lib/active_job/queue_adapters/delayed_job_adapter.rb b/lib/active_job/queue_adapters/delayed_job_adapter.rb index de53f66da1..14072e2801 100644 --- a/lib/active_job/queue_adapters/delayed_job_adapter.rb +++ b/lib/active_job/queue_adapters/delayed_job_adapter.rb @@ -11,7 +11,7 @@ module ActiveJob class JobWrapper def perform(job, *args) - job.perform *Parameters.deserialize(args) + job.new.perform *Parameters.deserialize(args) end end end diff --git a/lib/active_job/queue_adapters/inline_adapter.rb b/lib/active_job/queue_adapters/inline_adapter.rb index 36fc3c4951..cffa55af82 100644 --- a/lib/active_job/queue_adapters/inline_adapter.rb +++ b/lib/active_job/queue_adapters/inline_adapter.rb @@ -3,7 +3,7 @@ module ActiveJob class InlineAdapter class << self def queue(job, *args) - job.perform *Parameters.deserialize(args) + job.new.perform *Parameters.deserialize(args) end end end diff --git a/lib/active_job/queue_adapters/resque_adapter.rb b/lib/active_job/queue_adapters/resque_adapter.rb index c1e4000283..bb5f3df8d6 100644 --- a/lib/active_job/queue_adapters/resque_adapter.rb +++ b/lib/active_job/queue_adapters/resque_adapter.rb @@ -18,7 +18,7 @@ module ActiveJob end def perform(job_name, *args) - job_name.constantize.perform *Parameters.deserialize(args) + job_name.constantize.new.perform *Parameters.deserialize(args) end end diff --git a/lib/active_job/queue_adapters/sidekiq_adapter.rb b/lib/active_job/queue_adapters/sidekiq_adapter.rb index b02b3592da..957905f9d3 100644 --- a/lib/active_job/queue_adapters/sidekiq_adapter.rb +++ b/lib/active_job/queue_adapters/sidekiq_adapter.rb @@ -13,7 +13,7 @@ module ActiveJob include Sidekiq::Worker def perform(job_name, *args) - job_name.constantize.perform *Parameters.deserialize(args) + job_name.constantize.new.perform *Parameters.deserialize(args) end end end diff --git a/lib/active_job/queue_adapters/sucker_punch_adapter.rb b/lib/active_job/queue_adapters/sucker_punch_adapter.rb index b575927cfe..182b5ce018 100644 --- a/lib/active_job/queue_adapters/sucker_punch_adapter.rb +++ b/lib/active_job/queue_adapters/sucker_punch_adapter.rb @@ -13,7 +13,7 @@ module ActiveJob include SuckerPunch::Job def perform(job_name, *args) - job_name.perform *Parameters.deserialize(args) + job_name.new.perform *Parameters.deserialize(args) end end end diff --git a/test/jobs/gid_job.rb b/test/jobs/gid_job.rb index c1bfbb2655..c69e38d3cc 100644 --- a/test/jobs/gid_job.rb +++ b/test/jobs/gid_job.rb @@ -1,5 +1,5 @@ class GidJob < ActiveJob::Base - def self.perform(person) + def perform(person) $BUFFER << "Person with ID: #{person.id}" end end diff --git a/test/jobs/hello_job.rb b/test/jobs/hello_job.rb index d0e65f9674..25441dd0c8 100644 --- a/test/jobs/hello_job.rb +++ b/test/jobs/hello_job.rb @@ -1,5 +1,5 @@ class HelloJob < ActiveJob::Base - def self.perform(greeter = "David") + def perform(greeter = "David") $BUFFER << "#{greeter} says hello" end end