Skip to content

Commit

Permalink
Fixed a bug that prevented jobs in sub modules from being serialized …
Browse files Browse the repository at this point in the history
…correctly
  • Loading branch information
Tobias Lütke committed Nov 26, 2008
1 parent d0a9c2d commit 4fd41a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/delayed/performable_method.rb
@@ -1,7 +1,7 @@
module Delayed
class PerformableMethod < Struct.new(:object, :method, :args)
CLASS_STRING_FORMAT = /^CLASS\:([A-Z]\w+)$/
AR_STRING_FORMAT = /^AR\:([A-Z]\w+)\:(\d+)$/
CLASS_STRING_FORMAT = /^CLASS\:([A-Z][\w\:]+)$/
AR_STRING_FORMAT = /^AR\:([A-Z][\w\:]+)\:(\d+)$/

def initialize(object, method, args)
raise NoMethodError, "undefined method `#{method}' for #{self.inspect}" unless object.respond_to?(method)
Expand All @@ -11,8 +11,12 @@ def initialize(object, method, args)
self.method = method.to_sym
end

def display_name
"#{object}##{method}"
def display_name
case self.object
when CLASS_STRING_FORMAT then "#{$1}.#{method}"
when AR_STRING_FORMAT then "#{$1}##{method}"
else "Unknown##{method}"
end
end

def perform
Expand Down
28 changes: 27 additions & 1 deletion spec/job_spec.rb
Expand Up @@ -8,6 +8,14 @@ def perform; @@runs += 1; end
class ErrorJob
cattr_accessor :runs; self.runs = 0
def perform; raise 'did not work'; end
end

module M
class ModuleJob
cattr_accessor :runs; self.runs = 0
def perform; @@runs += 1; end
end

end

describe Delayed::Job do
Expand Down Expand Up @@ -61,7 +69,17 @@ def perform; raise 'did not work'; end

SimpleJob.runs.should == 1
end


it "should work with jobs in modules" do
M::ModuleJob.runs.should == 0

Delayed::Job.enqueue M::ModuleJob.new
Delayed::Job.work_off

M::ModuleJob.runs.should == 1
end

it "should re-schedule by about 1 second at first and increment this more and more minutes when it fails to execute properly" do
Delayed::Job.enqueue ErrorJob.new
Delayed::Job.work_off(1)
Expand Down Expand Up @@ -196,7 +214,15 @@ def perform; raise 'did not work'; end

it "should be the method that will be called if its a performable method object" do
Delayed::Job.send_later(:clear_locks!)
Delayed::Job.last.name.should == 'CLASS:Delayed::Job#clear_locks!'
Delayed::Job.last.name.should == 'Delayed::Job.clear_locks!'

end
it "should be the instance method that will be called if its a performable method object" do
story = Story.create :text => "..."

story.send_later(:save)

Delayed::Job.last.name.should == 'Story#save'
end
end

Expand Down

0 comments on commit 4fd41a9

Please sign in to comment.