Class: Resque::Data::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/resque-data/fetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Fetcher

Returns a new instance of Fetcher.



4
5
6
7
8
9
10
11
12
13
# File 'lib/resque-data/fetcher.rb', line 4

def initialize(params)
  @multi_namespace = params.delete(:multi_namespace)
  @multi_namespace = true if @multi_namespace.nil?

  (params[:redis] || 'redis://localhost:6379').tap do |r|
    @connection, @default_namespace = parse_redis(r)
  end

  @default_namespace ||= params[:namespace] || :resque
end

Instance Method Details

#failed_countObject



33
34
35
# File 'lib/resque-data/fetcher.rb', line 33

def failed_count
  {failed: redis.llen(:failed)}
end

#fetchObject



15
16
17
# File 'lib/resque-data/fetcher.rb', line 15

def fetch
  fetch_for(@default_namespace)
end

#fetch_for(ns) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/resque-data/fetcher.rb', line 19

def fetch_for(ns)
  ns = @default_namespace unless @multi_namespace

  using_namespace(ns) do
    queue_counts.merge(failed_count).merge(worker_counts)
  end
end

#queue_countsObject



27
28
29
30
31
# File 'lib/resque-data/fetcher.rb', line 27

def queue_counts
  {queues: redis.smembers(:queues).map {|q|
    {queue: q, count: redis.llen("queue:#{q}")}
  }}
end

#redisObject



58
59
60
# File 'lib/resque-data/fetcher.rb', line 58

def redis
  @redis ||= Redis::Namespace.new(@default_namespace, redis: @connection)
end

#using_namespace(ns) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/resque-data/fetcher.rb', line 50

def using_namespace(ns)
  redis.namespace = ns
  ret = yield
ensure
  redis.namespace = @default_namespace
  ret
end

#worker_countsObject



37
38
39
40
41
42
# File 'lib/resque-data/fetcher.rb', line 37

def worker_counts
  workers = redis.smembers(:workers)
  working = 0
  workers.each {|w| working += 1 if redis.exists("worker:#{w}")}
  {working: working, workers: workers.length}
end

#worker_statsObject



44
45
46
47
48
# File 'lib/resque-data/fetcher.rb', line 44

def worker_stats
  Hash[redis.smembers(:workers).map do |w|
    [w, redis.exists("worker:#{w}") ? :working : :idle]
  end]
end