Module: HireFire::Macro::Deprecated::Resque

Included in:
Resque
Defined in:
lib/hirefire/macro/deprecated/resque.rb

Overview

Provides backward compatibility with the deprecated Resque macro. For new implementations, refer to Resque.

Instance Method Summary collapse

Instance Method Details

#queue(*queues) ⇒ Integer

Retrieves the total number of jobs in the specified Resque queue(s).

This method counts the number of jobs in either specific queues or all queues if none are specified. It includes both queued and in-progress jobs.

Examples:

Counting jobs in all queues

HireFire::Macro::Resque.queue

Counting jobs in the “default” queue

HireFire::Macro::Resque.queue("default")

Counting jobs in both “default” and “critical” queues

HireFire::Macro::Resque.queue("default", "critical")

Parameters:

  • queues (Array<String, Symbol>)

    Queue names to count jobs in. Pass an empty array or no arguments to count jobs in all queues.

Returns:

  • (Integer)

    Total number of jobs in the specified queues.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hirefire/macro/deprecated/resque.rb', line 23

def queue(*queues)
  queues = queues.flatten.map(&:to_s)
  queues = ::Resque.queues if queues.empty?

  return 0 if queues.empty?

  redis = ::Resque.redis
  worker_ids = Array(redis.smembers(:workers)).compact
  raw_jobs = redis.pipelined do |redis|
    worker_ids.map { |id| redis.get("worker:#{id}") }
  end
  jobs_in_progress = raw_jobs.map { |raw_job| ::Resque.decode(raw_job) || {} }

  jobs_in_queues = redis.pipelined do |redis|
    queues.map { |queue| redis.llen("queue:#{queue}") }
  end.map(&:to_i).sum

  in_progress_count = jobs_in_progress.count { |job| queues.include?(job["queue"]) }

  jobs_in_queues + in_progress_count
end