diff --git a/README.textile b/README.textile index 3e0f12639..e832d5a74 100644 --- a/README.textile +++ b/README.textile @@ -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.
 # without delayed_job
 Notifier.deliver_signup(@user)
 
 # with delayed_job
-Notifier.send_later :deliver_signup, @user
+Notifier.delay.deliver_signup @user
 
If a method should always be run in the background, you can call @#handle_asynchronously@ after the method declaration: diff --git a/benchmarks.rb b/benchmarks.rb index 96a26b5a9..ccaa7e45e 100644 --- a/benchmarks.rb +++ b/benchmarks.rb @@ -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 diff --git a/lib/delayed/message_sending.rb b/lib/delayed/message_sending.rb index 389d8ee25..da83b5c2f 100644 --- a/lib/delayed/message_sending.rb +++ b/lib/delayed/message_sending.rb @@ -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 diff --git a/spec/backend/mongo_mapper_job_spec.rb b/spec/backend/mongo_mapper_job_spec.rb index aecd5c28f..26aa3b8ed 100644 --- a/spec/backend/mongo_mapper_job_spec.rb +++ b/spec/backend/mongo_mapper_job_spec.rb @@ -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 == "LOAD;MongoStory;#{story.id}" @@ -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 == ["LOAD;MongoStory;#{story.id}"] diff --git a/spec/backend/shared_backend_spec.rb b/spec/backend/shared_backend_spec.rb index 551ed6c23..7e0562d4c 100644 --- a/spec/backend/shared_backend_spec.rb +++ b/spec/backend/shared_backend_spec.rb @@ -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 diff --git a/spec/delayed_method_spec.rb b/spec/delayed_method_spec.rb index 1b4dd29e5..055fac785 100644 --- a/spec/delayed_method_spec.rb +++ b/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 @@ -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) @@ -80,7 +42,5 @@ job = 3.delay.+(5) job.payload_object.args.should == [5] end - end - end diff --git a/spec/story_spec.rb b/spec/story_spec.rb index 61d5f3a61..7a87cada9 100644 --- a/spec/story_spec.rb +++ b/spec/story_spec.rb @@ -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 \ No newline at end of file