Skip to content

Commit

Permalink
Deprecate #send_later and #send_at in favor of new #delay method
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Nov 13, 2010
1 parent 49444b6 commit 2f11ac2
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.textile
Expand Up @@ -68,14 +68,14 @@ Delayed::Worker.backend.auto_upgrade!

h2. Queuing Jobs

Call @#send_later(method, params)@ on any object and it will be processed in the background.
Call @.delay.method(params)@ on any object and it will be processed in the background.

<pre>
# without delayed_job
Notifier.deliver_signup(@user)

# with delayed_job
Notifier.send_later :deliver_signup, @user
Notifier.delay.deliver_signup @user
</pre>

If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration:
Expand Down
2 changes: 1 addition & 1 deletion benchmarks.rb
Expand Up @@ -26,7 +26,7 @@
Delayed::Worker.backend = backend

n = 10000
n.times { "foo".send_later :length }
n.times { "foo".delay.length }

x.report(backend.to_s) { Delayed::Worker.new(:quiet => true).work_off(n) }
end
Expand Down
12 changes: 7 additions & 5 deletions lib/delayed/message_sending.rb
Expand Up @@ -18,21 +18,23 @@ def delay(options = {})
end

def send_later(method, *args)
Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sym, args)
warn "[DEPRECATION] `object.send_later(:method)` is deprecated. Use `object.delay.method"
delay.__send__(method, *args)
end

def send_at(time, method, *args)
Delayed::Job.enqueue(Delayed::PerformableMethod.new(self, method.to_sym, args), 0, time)
warn "[DEPRECATION] `object.send_at(time, :method)` is deprecated. Use `object.delay(:run_at => time).method"
delay(:run_at => time).__send__(method, *args)
end

module ClassMethods
def handle_asynchronously(method)
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
with_method, without_method = "#{aliased_method}_with_send_later#{punctuation}", "#{aliased_method}_without_send_later#{punctuation}"
with_method, without_method = "#{aliased_method}_with_delay#{punctuation}", "#{aliased_method}_without_delay#{punctuation}"
define_method(with_method) do |*args|
send_later(without_method, *args)
delay.__send__(without_method, *args)
end
alias_method_chain method, :send_later
alias_method_chain method, :delay
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/backend/mongo_mapper_job_spec.rb
Expand Up @@ -41,14 +41,14 @@ def tell

it "should ignore not found errors because they are permanent" do
story = MongoStory.create :text => 'Once upon a time...'
job = story.send_later(:tell)
job = story.delay.tell
story.destroy
lambda { job.invoke_job }.should_not raise_error
end

it "should store the object as string" do
story = MongoStory.create :text => 'Once upon a time...'
job = story.send_later(:tell)
job = story.delay.tell

job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.object.should == story
Expand All @@ -59,7 +59,7 @@ def tell

it "should store arguments as string" do
story = MongoStory.create :text => 'Once upon a time...'
job = MongoStoryReader.new.send_later(:read, story)
job = MongoStoryReader.new.delay.read(story)
job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.method.should == :read
job.payload_object.args.should == [story]
Expand Down
4 changes: 2 additions & 2 deletions spec/backend/shared_backend_spec.rb
Expand Up @@ -173,12 +173,12 @@ def create_job(opts = {})
end

it "should be the method that will be called if its a performable method object" do
@job = Story.send_later(:create)
@job = Story.delay.create
@job.name.should == "Story.create"
end

it "should be the instance method that will be called if its a performable method object" do
@job = Story.create(:text => "...").send_later(:save)
@job = Story.create(:text => "...").delay.save
@job.name.should == 'Story#save'
end
end
Expand Down
46 changes: 3 additions & 43 deletions spec/delayed_method_spec.rb
@@ -1,25 +1,9 @@
require 'spec_helper'

describe 'random ruby objects' do
describe Object do
before { Delayed::Job.delete_all }

it "should respond_to :send_later method" do
Object.new.respond_to?(:send_later)
end

it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
lambda { Object.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
end

it "should add a new entry to the job table when send_later is called on it" do
lambda { Object.new.send_later(:to_s) }.should change { Delayed::Job.count }.by(1)
end

it "should add a new entry to the job table when send_later is called on the class" do
lambda { Object.send_later(:to_s) }.should change { Delayed::Job.count }.by(1)
end

it "should call send later on methods which are wrapped with handle_asynchronously" do
it "should call #delay on methods which are wrapped with handle_asynchronously" do
story = Story.create :text => 'Once upon...'

Delayed::Job.count.should == 0
Expand All @@ -29,33 +13,11 @@
Delayed::Job.count.should == 1
job = Delayed::Job.first
job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.method.should == :whatever_without_send_later
job.payload_object.method.should == :whatever_without_delay
job.payload_object.args.should == [1, 5]
job.payload_object.perform.should == 'Once upon...'
end

context "send_at" do
it "should queue a new job" do
lambda do
"string".send_at(1.hour.from_now, :length)
end.should change { Delayed::Job.count }.by(1)
end

it "should schedule the job in the future" do
time = 1.hour.from_now.utc.to_time
job = "string".send_at(time, :length)
job.run_at.to_i.should == time.to_i
end

it "should store payload as PerformableMethod" do
job = "string".send_at(1.hour.from_now, :count, 'r')
job.payload_object.class.should == Delayed::PerformableMethod
job.payload_object.method.should == :count
job.payload_object.args.should == ['r']
job.payload_object.perform.should == 1
end
end

context "delay" do
it "should raise a ArgumentError if target method doesn't exist" do
lambda { Object.new.delay.method_that_does_not_exist }.should raise_error(NoMethodError)
Expand All @@ -80,7 +42,5 @@
job = 3.delay.+(5)
job.payload_object.args.should == [5]
end

end

end
2 changes: 1 addition & 1 deletion spec/story_spec.rb
Expand Up @@ -11,7 +11,7 @@
end

it "should not return its result if it storytelling is delayed" do
@story.send_later(:tell).should_not == 'Once upon a time...'
@story.delay.tell.should_not == 'Once upon a time...'
end

end

0 comments on commit 2f11ac2

Please sign in to comment.