Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Factored duplication out of Job#deserialize. Made roodi happier about…

… its cyclomatic complexity as well.
  • Loading branch information...
commit bcf8d1d2decd70b59b63640263bfde9a12959c5c 1 parent 89c3a0b
Justin Knowlden authored
Showing with 21 additions and 40 deletions.
  1. +19 −39 lib/delayed/job.rb
  2. +1 −0  spec/database.rb
  3. +1 −1  spec/job_spec.rb
View
58 lib/delayed/job.rb
@@ -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
@@ -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
@@ -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
View
1  spec/database.rb
@@ -3,6 +3,7 @@
require 'rubygems'
require 'active_record'
+gem 'sqlite3-ruby'
require File.dirname(__FILE__) + '/../init'
require 'spec'
View
2  spec/job_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.