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)


98
99
100
# File 'lib/resque/failure/redis.rb', line 98

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

.clear(queue = nil) ⇒ Object



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

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, order = 'desc') ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resque/failure/redis.rb', line 41

def self.each(offset = 0, limit = self.count, queue = :failed, class_name = nil, order = 'desc')
  all_items = Array(all(offset, limit, queue))
  reversed = false
  if order.eql? 'desc'
    all_items.reverse!
    reversed = true
  end
  all_items.each_with_index do |item, i|
    if !class_name || (item['payload'] && item['payload']['class'] == class_name)
      if reversed
        id = (all_items.length - 1) - (offset + i)
      else
        id = offset + i
      end
      yield id, item
    end
  end
end

.queuesObject



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

def self.queues
  [:failed]
end

.remove(id) ⇒ Object



72
73
74
75
76
# File 'lib/resque/failure/redis.rb', line 72

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

.remove_queue(queue) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/resque/failure/redis.rb', line 86

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



65
66
67
68
69
70
# File 'lib/resque/failure/redis.rb', line 65

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



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

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



102
103
104
105
# File 'lib/resque/failure/redis.rb', line 102

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 => UTF8Util.clean(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