Class: Quredis::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/quredis/admin.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Admin

Returns a new instance of Admin.



8
9
10
# File 'lib/quredis/admin.rb', line 8

def initialize(opts={})
  @options = opts
end

Instance Method Details

#destroy_queue(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/quredis/admin.rb', line 51

def destroy_queue(name)
  queue_key = "quredis:queue:#{name}"
  str = redis.get(queue_key)
  queue = JSON.parse(str)
  redis.multi do |multi|
    multi.del(queue['escape'])
    multi.del(queue['transit'])
    multi.del(queue['ingress'])
    multi.del(queue_key)
    multi.zrem("quredis:queues", name)
  end
end

#purge(name, type) ⇒ Object



64
65
66
67
68
69
# File 'lib/quredis/admin.rb', line 64

def purge(name, type)
  queue_key = "quredis:queue:#{name}"
  str = redis.get(queue_key)
  queue = JSON.parse(str)
  redis.del(queue[type])
end

#queue(name, type) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/quredis/admin.rb', line 39

def queue(name, type)
  str = redis.get("quredis:queue:#{name}")
  queue = JSON.parse(str)
  list = queue[type]

  total, items = redis.multi do |multi|
    multi.llen(list)
    multi.lrange(list, 0, 100)
  end
  {:type => type, :queue => name, :list => list, :total => total, :offset => 0, :items => items}    
end

#queues(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/quredis/admin.rb', line 16

def queues(params={})
  offset = params.fetch(:offset, 0).to_i
  limit = params.fetch(:limit, 1000).to_i
  queue_names, total = redis.multi do |multi|
    multi.zrange("quredis:queues", offset, offset+limit)
    multi.zcard("quredis:queues")
  end
  queue_keys = queue_names.map {|queue_name| "quredis:queue:#{queue_name}"}
  queues = redis.mget(*queue_keys).map {|str| JSON.parse(str)}

  queues.each do |queue| 
    ingress_count, transit_count, escape_count = redis.multi do |multi|
      multi.llen(queue['ingress'])
      multi.llen(queue['transit'])
      multi.llen(queue['escape'])
    end
    queue['ingress_count'] = ingress_count
    queue['transit_count'] = transit_count
    queue['escape_count'] = escape_count
  end
  {:total => total, :offset => offset, :items => queues}
end

#redisObject



12
13
14
# File 'lib/quredis/admin.rb', line 12

def redis
  @redis ||= Redis.new(@options)
end

#retry(name, type) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/quredis/admin.rb', line 71

def retry(name, type)
  queue_key = "quredis:queue:#{name}"
  str = redis.get(queue_key)
  queue = JSON.parse(str)
  from = queue[type]
  to = queue['ingress']
  message = redis.rpoplpush(from, to) while message
end