0
+ class DeserializationError < StandardError
0
+ class Job < ActiveRecord::Base
0
+ ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
0
+ set_table_name :delayed_jobs
0
+ attr_accessor :logger, :jobs
0
+ attr_accessor :runs, :success, :failure
0
+ def initialize(jobs, logger = nil)
0
+ self.runs = self.success = self.failure = 0
0
+ ActiveRecord::Base.cache do
0
+ ActiveRecord::Base.transaction do
0
+ time = Benchmark.measure do
0
+ ActiveRecord::Base.uncached { job.destroy }
0
+ logger.debug "Executed job in #{time.real}"
0
+ rescue DeserializationError, StandardError, RuntimeError => e
0
+ logger.error "Job #{job.id}: #{e.class} #{e.message}"
0
+ logger.error e.backtrace.join("\n")
0
+ ActiveRecord::Base.uncached { job.reshedule e.message }
0
+ def self.enqueue(object, priority = 0)
0
+ raise ArgumentError, 'Cannot enqueue items which do not respond to perform' unless object.respond_to?(:perform)
0
+ Job.create(:handler => object, :priority => priority)
0
+ self['handler'] = object.to_yaml
0
+ @handler ||= deserialize(self['handler'])
0
+ def reshedule(message)
0
+ self.run_at = self.class.time_now + 5.minutes
0
+ self.last_error = message
0
+ def self.peek(limit = 1)
0
+ find(:first, :order => "priority DESC, run_at ASC", :conditions => ['run_at <= ?', time_now])
0
+ find(:all, :order => "priority DESC, run_at ASC", :limit => limit, :conditions => ['run_at <= ?', time_now])
0
+ def self.work_off(limit = 100)
0
+ jobs = Job.find(:all, :conditions => ['run_at <= ?', time_now], :order => "priority DESC, run_at ASC", :limit => limit)
0
+ Job::Runner.new(jobs, logger).run
0
+ (ActiveRecord::Base.default_timezone == :utc) ? Time.now.utc : Time.now
0
+ self.run_at ||= self.class.time_now
0
+ def deserialize(source)
0
+ attempt_to_load_file = true
0
+ handler = YAML.load(source) rescue nil
0
+ return handler if handler.respond_to?(:perform)
0
+ if source =~ ParseObjectFromYaml
0
+ # Constantize the object so that ActiveSupport can attempt
0
+ # its auto loading magic. Will raise LoadError if not successful.
0
+ # If successful, retry the yaml.load
0
+ handler = YAML.load(source)
0
+ return handler if handler.respond_to?(:perform)
0
+ if handler.is_a?(YAML::Object)
0
+ # Constantize the object so that ActiveSupport can attempt
0
+ # its auto loading magic. Will raise LoadError if not successful.
0
+ attempt_to_load(handler.class)
0
+ # If successful, retry the yaml.load
0
+ handler = YAML.load(source)
0
+ return handler if handler.respond_to?(:perform)
0
+ raise DeserializationError, 'Job failed to load: Unknown handler. Try to manually require the appropiate file.'
0
+ rescue TypeError, LoadError, NameError => e
0
+ raise DeserializationError, "Job failed to load: #{e.message}. Try to manually require the required file."
0
+ def attempt_to_load(klass)
0
\ No newline at end of file
Comments
No one has commented yet.