Class: Resque::QueueStats

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Helpers
Defined in:
lib/resque/queue_stats.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

classify, constantize, decode, encode, mongo, mongo_queues, mongo_stats, mongo_workers

Constructor Details

#initialize(queue) ⇒ QueueStats

Returns a new instance of QueueStats.



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

def initialize(queue)
  @queue = queue
  queue_doc = mongo_queues.find({ :queue => queue.to_s }, :limit => 1).to_a[0]
  ### insert a new document if we don't know anyting about this queue
  if queue_doc
    @size = queue_doc['count']
  else
    @size = 0
    mongo_queues.insert({ :queue => queue.to_s, :count => 0 }) unless queue_doc
  end
end

Class Method Details

.add_job(queue, count = 1) ⇒ Object



46
47
48
# File 'lib/resque/queue_stats.rb', line 46

def self.add_job(queue,count=1)
  QueueStats.new(queue).add_job(count)
end

.list(names = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/resque/queue_stats.rb', line 31

def self.list(names = nil)
  if names
    criteria = [*names].collect do |name|
      name =~ /\*$/ ? Regexp.new("^#{name[0..-2]}") : name
    end
    mongo_queues.distinct(:queue, :queue => {'$in' => criteria}).to_a
  else
    mongo_queues.distinct(:queue).to_a
  end
end

.remove(queue) ⇒ Object



42
43
44
# File 'lib/resque/queue_stats.rb', line 42

def self.remove(queue)
  mongo_queues.remove({:queue => queue.to_s})
end

.remove_job(queue, count = 1) ⇒ Object



50
51
52
# File 'lib/resque/queue_stats.rb', line 50

def self.remove_job(queue, count = 1)
  self.add_job(queue,-count)
end

.size(queue) ⇒ Object



54
55
56
# File 'lib/resque/queue_stats.rb', line 54

def self.size(queue)
  QueueStats.new(queue).size
end

Instance Method Details

#add_job(count = 1) ⇒ Object



18
19
20
21
# File 'lib/resque/queue_stats.rb', line 18

def add_job(count=1)
  mongo_queues.update({ :queue => @queue.to_s }, { '$inc' => { :count => count } })
  @size += count
end

#remove_job(count = 1) ⇒ Object



23
24
25
# File 'lib/resque/queue_stats.rb', line 23

def remove_job(count=1)
  add_job(-count)
end

#sizeObject



27
28
29
# File 'lib/resque/queue_stats.rb', line 27

def size
  @size
end