Skip to content

Commit

Permalink
Factored duplication out of Job#deserialize. Made roodi happier about…
Browse files Browse the repository at this point in the history
… its cyclomatic complexity as well.
  • Loading branch information
Justin Knowlden committed Nov 29, 2008
1 parent 89c3a0b commit bcf8d1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 40 deletions.
58 changes: 19 additions & 39 deletions lib/delayed/job.rb
Expand Up @@ -185,7 +185,6 @@ def self.work_off(num = 100)
success, failure = 0, 0

num.times do

job = self.reserve do |j|
begin
j.perform
Expand All @@ -200,56 +199,37 @@ def self.work_off(num = 100)
end

return [success, failure]
end


end

# Moved into its own method so that new_relic can trace it.
def self.invoke_job(job, &block)
block.call(job)
end


private
private

def deserialize(source)
attempt_to_load_file = true

begin
handler = YAML.load(source) rescue nil
return handler if handler.respond_to?(:perform)

if handler.nil?
if source =~ ParseObjectFromYaml

# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
attempt_to_load($1)
handler = YAML.load(source) rescue nil

# If successful, retry the yaml.load
handler = YAML.load(source)
return handler if handler.respond_to?(:perform)
end
end

if handler.is_a?(YAML::Object)

# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
attempt_to_load(handler.class)

# If successful, retry the yaml.load
handler = YAML.load(source)
return handler if handler.respond_to?(:perform)
unless handler.respond_to?(:perform)
if handler.nil? && source =~ ParseObjectFromYaml
handler_class = $1
end
attempt_to_load(handler_class || handler.class)
handler = YAML.load(source)
end

raise DeserializationError, 'Job failed to load: Unknown handler. Try to manually require the appropiate file.'

rescue TypeError, LoadError, NameError => e
return handler if handler.respond_to?(:perform)

raise DeserializationError, "Job failed to load: #{e.message}. Try to manually require the required file."
end
raise DeserializationError,
'Job failed to load: Unknown handler. Try to manually require the appropiate file.'
rescue TypeError, LoadError, NameError => e
raise DeserializationError,
"Job failed to load: #{e.message}. Try to manually require the required file."
end

# Constantize the object so that ActiveSupport can attempt
# its auto loading magic. Will raise LoadError if not successful.
def attempt_to_load(klass)
klass.constantize
end
Expand All @@ -258,7 +238,7 @@ def self.db_time_now
(ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.now
end

protected
protected

def before_save
self.run_at ||= self.class.db_time_now
Expand Down
1 change: 1 addition & 0 deletions spec/database.rb
Expand Up @@ -3,6 +3,7 @@

require 'rubygems'
require 'active_record'
gem 'sqlite3-ruby'

require File.dirname(__FILE__) + '/../init'
require 'spec'
Expand Down
2 changes: 1 addition & 1 deletion spec/job_spec.rb
Expand Up @@ -283,7 +283,7 @@ def perform; @@runs += 1; end

it "should leave the queue in a consistent state and not run the job if locking fails" do
SimpleJob.runs.should == 0
@job.stub!(:lock_exclusively!).with(:any_args).once.and_raise(Delayed::Job::LockError)
@job.stub!(:lock_exclusively!).with(any_args).once.and_raise(Delayed::Job::LockError)
Delayed::Job.should_receive(:find_available).once.and_return([@job])
Delayed::Job.work_off(1)
SimpleJob.runs.should == 0
Expand Down

0 comments on commit bcf8d1d

Please sign in to comment.