Permalink
Browse files

Added Delayed::Worker.default_priority

  • Loading branch information...
1 parent fc692e2 commit 948223b6a86dde0c1eb1e46155072cdd0190c970 @eugenebolshakov eugenebolshakov committed with bkeepers Jun 23, 2010
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
2 .rvmrc
@@ -0,0 +1,2 @@
+rvm_gemset_create_on_use_flag=1
+rvm gemset use ree-1.8.7@delayed_job
@@ -16,7 +16,7 @@ def enqueue(*args)
raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
end
- priority = args.first || 0
+ priority = args.first || Delayed::Worker.default_priority
run_at = args[1]
self.create(:payload_object => object, :priority => priority.to_i, :run_at => run_at)
end
@@ -108,4 +108,4 @@ def set_default_run_at
end
end
-end
+end
@@ -4,14 +4,15 @@ def initialize(target, options)
@target = target
@options = options
end
-
+
def method_missing(method, *args)
- Job.create @options.merge(
- :payload_object => PerformableMethod.new(@target, method.to_sym, args)
- )
+ Job.create({
+ :payload_object => PerformableMethod.new(@target, method.to_sym, args),
+ :priority => Delayed::Worker.default_priority
+ }.merge(@options))
end
end
-
+
module MessageSending
def delay(options = {})
DelayProxy.new(self, options)
@@ -39,4 +40,4 @@ def handle_asynchronously(method)
end
end
end
-end
+end
View
@@ -3,10 +3,11 @@
module Delayed
class Worker
- cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time, :sleep_delay, :logger
+ cattr_accessor :min_priority, :max_priority, :max_attempts, :max_run_time, :default_priority, :sleep_delay, :logger
self.sleep_delay = 5
self.max_attempts = 25
self.max_run_time = 4.hours
+ self.default_priority = 0
# By default failed jobs are destroyed after too many attempts. If you want to keep them around
# (perhaps to inspect the reason for the failure), set this to false.
@@ -6,6 +6,7 @@ def create_job(opts = {})
before do
Delayed::Worker.max_priority = nil
Delayed::Worker.min_priority = nil
+ Delayed::Worker.default_priority = 99
SimpleJob.runs = 0
end
@@ -32,6 +33,11 @@ def create_job(opts = {})
@job.priority.should == 5
end
+ it "should use default priority when it is not set" do
+ @job = @backend.enqueue SimpleJob.new
+ @job.priority.should == 99
+ end
+
it "should be able to set run_at when enqueuing items" do
later = @backend.db_time_now + 5.minutes
@job = @backend.enqueue SimpleJob.new, 5, later
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe Delayed::MessageSending do
+ describe "handle_asynchronously" do
+ class Story < ActiveRecord::Base
+ def tell!(arg)
+ end
+ handle_asynchronously :tell!
+ end
+
+ it "should alias original method" do
+ Story.new.should respond_to(:tell_without_delay!)
+ Story.new.should respond_to(:tell_with_delay!)
+ end
+
+ it "should create a PerformableMethod" do
+ story = Story.create!
+ lambda {
+ job = story.tell!(1)
+ job.payload_object.class.should == Delayed::PerformableMethod
+ job.payload_object.method.should == :tell_without_delay!
+ job.payload_object.args.should == [1]
+ }.should change { Delayed::Job.count }
+ end
+ end
+
+ context "delay" do
+ it "should create a new PerformableMethod job" do
+ lambda {
+ job = "hello".delay.count('l')
+ job.payload_object.class.should == Delayed::PerformableMethod
+ job.payload_object.method.should == :count
+ job.payload_object.args.should == ['l']
+ }.should change { Delayed::Job.count }.by(1)
+ end
+
+ it "should set default priority" do
+ Delayed::Worker.default_priority = 99
+ job = Object.delay.to_s
+ job.priority.should == 99
+ Delayed::Worker.default_priority = 0
+ end
+
+ it "should set job options" do
+ run_at = Time.parse('2010-05-03 12:55 AM')
+ job = Object.delay(:priority => 20, :run_at => run_at).to_s
+ job.run_at.should == run_at
+ job.priority.should == 20
+ end
+ end
+end

0 comments on commit 948223b

Please sign in to comment.