Class: Resque::Failure::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/resque/failure/redis.rb

Overview

A Failure backend that stores exceptions in Redis. Very simple but works out of the box, along with support in the Resque web app.

Instance Attribute Summary

Attributes inherited from Base

#exception, #payload, #queue, #worker

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #log, url

Constructor Details

This class inherits a constructor from Resque::Failure::Base

Class Method Details

.all(offset = 0, limit = 1, queue = nil) ⇒ Object



36
37
38
39
# File 'lib/resque/failure/redis.rb', line 36

def self.all(offset = 0, limit = 1, queue = nil)
  check_queue(queue)
  Resque.list_range(:failed, offset, limit)
end

.check_queue(queue) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
# File 'lib/resque/failure/redis.rb', line 87

def self.check_queue(queue)
  raise ArgumentError, "invalid queue: #{queue}" if queue && queue.to_s != "failed"
end

.clear(queue = nil) ⇒ Object



49
50
51
52
# File 'lib/resque/failure/redis.rb', line 49

def self.clear(queue = nil)
  check_queue(queue)
  Resque.redis.del(:failed)
end

.count(queue = nil, class_name = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/resque/failure/redis.rb', line 20

def self.count(queue = nil, class_name = nil)
  check_queue(queue)

  if class_name
    n = 0
    each(0, count(queue), queue, class_name) { n += 1 } 
    n
  else
    Resque.redis.llen(:failed).to_i
  end
end

.each(offset = 0, limit = self.count, queue = :failed, class_name = nil) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/resque/failure/redis.rb', line 41

def self.each(offset = 0, limit = self.count, queue = :failed, class_name = nil)
  Array(all(offset, limit, queue)).each_with_index do |item, i|
    if !class_name || (item['payload'] && item['payload']['class'] == class_name)
      yield offset + i, item
    end
  end
end

.queuesObject



32
33
34
# File 'lib/resque/failure/redis.rb', line 32

def self.queues
  [:failed]
end

.remove(id) ⇒ Object



61
62
63
64
65
# File 'lib/resque/failure/redis.rb', line 61

def self.remove(id)
  sentinel = ""
  Resque.redis.lset(:failed, id, sentinel)
  Resque.redis.lrem(:failed, 1,  sentinel)
end

.remove_queue(queue) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/resque/failure/redis.rb', line 75

def self.remove_queue(queue)
  i = 0
  while job = all(i)
    if job['queue'] == queue
      # This will remove the failure from the array so do not increment the index.
      remove(i)
    else
      i += 1
    end
  end
end

.requeue(id) ⇒ Object



54
55
56
57
58
59
# File 'lib/resque/failure/redis.rb', line 54

def self.requeue(id)
  item = all(id)
  item['retried_at'] = Time.now.strftime("%Y/%m/%d %H:%M:%S")
  Resque.redis.lset(:failed, id, Resque.encode(item))
  Job.create(item['queue'], item['payload']['class'], *item['payload']['args'])
end

.requeue_queue(queue) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/resque/failure/redis.rb', line 67

def self.requeue_queue(queue)
  i = 0
  while job = all(i)
     requeue(i) if job['queue'] == queue
     i += 1
  end
end

Instance Method Details

#filter_backtrace(backtrace) ⇒ Object



91
92
93
94
# File 'lib/resque/failure/redis.rb', line 91

def filter_backtrace(backtrace)
  index = backtrace.index { |item| item.include?('/lib/resque/job.rb') }
  backtrace.first(index.to_i)
end

#saveObject



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/resque/failure/redis.rb', line 6

def save
  data = {
    :failed_at => Time.now.strftime("%Y/%m/%d %H:%M:%S %Z"),
    :payload   => payload,
    :exception => exception.class.to_s,
    :error     => UTF8Util.clean(exception.to_s),
    :backtrace => filter_backtrace(Array(exception.backtrace)),
    :worker    => worker.to_s,
    :queue     => queue
  }
  data = Resque.encode(data)
  Resque.redis.rpush(:failed, data)
end