Sha256: dd88c84284af4750a333b763be9dc342d152ef8ab62c89eb282d64a6954197a8

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kthxbye-1.3.2 lib/kthxbye/failure.rb
kthxbye-1.3.0 lib/kthxbye/failure.rb