Class: Resque::Failure::Mongo

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

Overview

A Failure backend that stores exceptions in Mongo. 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(start = 0, count = 1) ⇒ Object



29
30
31
32
33
# File 'lib/resque/failure/mongo.rb', line 29

def self.all(start = 0, count = 1)
  start, count = [start, count].map { |n| Integer(n) }
  all_failures = Resque.mongo_failures.find().sort([:failed_at, :desc]).skip(start).limit(count).to_a
  all_failures.size == 1 ? all_failures.first : all_failures
end

.clearObject



75
76
77
# File 'lib/resque/failure/mongo.rb', line 75

def self.clear
  Resque.mongo_failures.remove
end

.countObject



21
22
23
# File 'lib/resque/failure/mongo.rb', line 21

def self.count
  Resque.mongo_failures.count
end

.remove(index) ⇒ Object



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

def self.remove(index)
  item = all(index)
  Resque.mongo_failures.remove(:_id => item['_id'])
end

.requeue(index) ⇒ Object



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

def self.requeue(index)
  item = all(index)
  item['retried_at'] = Time.now.strftime("%Y/%m/%d %H:%M:%S")
  Resque.mongo_failures.update({:_id => item['_id']}, item)
  Job.create(item['queue'], item['payload']['class'], *item['payload']['args'])
end

.search_countObject



25
26
27
# File 'lib/resque/failure/mongo.rb', line 25

def self.search_count
  @@search_results.count
end

.search_results(query, start = 0, count = 1) ⇒ Object

Looks for failed jobs who match a particular search string



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/resque/failure/mongo.rb', line 36

def self.search_results(query, start = 0, count = 1)
  # If the search query is nil or empty, return an empty array
  if query.nil? || query.empty?
    @@search_results = []
    return []
  end
  
  start, count = [start, count].map { |n| Integer(n) }
  set_results = Set.new
  
  # For each search term, retrieve the failed jobs that contain at least one relevant field matching the regexp defined by that search term
  query.split.each do |term|
    partial_results = Resque.mongo_failures.find(
      {"$or" => [ 
        {"exception" => /#{term}/i},
        {"error" => /#{term}/i},
        {"payload.class" => /#{term}/i},
        {"payload.args" => /#{term}/i},
        {"worker" => /#{term}/i},
        {"queue" => /#{term}/i},
        {"backtrace" => /#{term}/i}
      ] }
    ).sort([:failed_at, :desc]).to_a
      
    # If the set was empty, merge the first results, else intersect it with the current results
    if set_results.empty?
      set_results.merge(partial_results)
    else
      set_results = set_results & partial_results
    end
  end
  
  # search_res will be an array containing 'count' values, starting with 'start', sorted in descending order
  @@search_results = set_results.to_a || []
  search_results = set_results.to_a[start, count]
  
  search_results || []
end

Instance Method Details

#filter_backtrace(backtrace) ⇒ Object



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

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

#saveObject



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

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