module Kthxbye module Failure include Helper extend Helper def self.all redis.hvals( :failed ).sort.map{|x| decode( x )} end # returns a specified failed job data def self.find(id) decode( redis.hget( :failed, id ) ) end # gets count of all errors def self.count redis.hkeys( :failed ).size end # gets count of all errors of a specific type def self.count_type(type) vals = redis.hvals( :failed ) vals.each {|x| o = decode(x); vals.delete x if o['type'] !~ /#{type.to_s}/} vals.size end # creates a Failure object. def self.create(job, exception) failed_attempts = (Failure.find(job.id)['attempts'].to_i + 1) if redis.hexists( :failed, job.id ) error = { :type => exception.class.to_s, :error => exception.to_s, :job => job.id, :queue => job.queue, :time => Time.now, :backtrace => Array( exception.backtrace ), :attempts => (failed_attempts || 1) } redis.hset( :failed, job.id, encode( error ) ) job.dequeue end def self.rerun(id) failure = Failure.find(id) Job.find(id, failure['queue']).rerun end def self.fails_for_job(id) failure = decode( redis.hget( :failed, id ) ) return failure ? failure['attempts'] : 0 end # the only method allowed to clear exceptions out of the exception store def self.clear_exception(id) redis.hdel( :failed, id ) end def self.clear_all redis.del( :failed ) end end end