Class: Resque::Mcp::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/mcp/adapter.rb

Overview

The only code in the gem that talks to Resque. Rails-free.

Defined Under Namespace

Classes: FailureOutOfRangeError, FailureQueueRequiredError, UnknownQueueError

Constant Summary collapse

FILTER_SCAN_CHUNK =

Chunk size for the tail-backwards scan under a class_name filter.

100

Instance Method Summary collapse

Constructor Details

#initialize(resque: ::Resque) ⇒ Adapter

Returns a new instance of Adapter.



31
32
33
# File 'lib/resque/mcp/adapter.rb', line 31

def initialize(resque: ::Resque)
  @resque = resque
end

Instance Method Details

#failure(index, queue: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/resque/mcp/adapter.rb', line 83

def failure(index, queue: nil)
  ensure_failure_queue!(queue)
  count = @resque::Failure.count(queue)
  raise FailureOutOfRangeError.new(index, count) unless index >= 0 && index < count
  item = to_array(@resque::Failure.all(index, 1, queue)).first
  # The list can shrink between the count read and the fetch.
  raise FailureOutOfRangeError.new(index, @resque::Failure.count(queue)) if item.nil?
  normalize_failure(index, item)
end

#failures(offset:, limit:, class_name: nil, queue: nil) ⇒ Object

Newest-first pagination over the failed list. offset counts raw list positions back from the newest record; returned indexes are raw list positions (what requeue/remove take). Failures are RPUSH'd so index 0 is the OLDEST record, and Failure.each's 'desc' only reverses within a fetched slice — the newest-first window has to be translated to raw indexes here.



73
74
75
76
77
78
79
80
81
# File 'lib/resque/mcp/adapter.rb', line 73

def failures(offset:, limit:, class_name: nil, queue: nil)
  ensure_failure_queue!(queue)
  raw_total = @resque::Failure.count(queue)
  if class_name.nil?
    unfiltered_failures(offset, limit, raw_total, queue)
  else
    filtered_failures(offset, limit, raw_total, class_name, queue)
  end
end

#peek(queue, offset:, limit:) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/resque/mcp/adapter.rb', line 59

def peek(queue, offset:, limit:)
  ensure_known_queue!(queue)
  jobs = to_array(@resque.peek(queue, offset, limit)).map do |item|
    build_job(item, queue: queue)
  end
  {size: @resque.size(queue), jobs: jobs}
end

#queue_size(queue) ⇒ Object



54
55
56
57
# File 'lib/resque/mcp/adapter.rb', line 54

def queue_size(queue)
  ensure_known_queue!(queue)
  @resque.size(queue)
end

#queuesObject



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

def queues
  @resque.queue_sizes
end

#redis_identifierObject

Resque.redis_id can embed user:password@; only this stripped form may reach tool responses.



105
106
107
# File 'lib/resque/mcp/adapter.rb', line 105

def redis_identifier
  strip_userinfo(@resque.redis_id.to_s)
end

#statsObject

One queue_sizes snapshot, not Resque.info — info reruns the queue sweep, letting pending disagree with queue_sizes in one response.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/resque/mcp/adapter.rb', line 37

def stats
  queue_sizes = @resque.queue_sizes
  {
    pending: queue_sizes.values.sum,
    processed: @resque::Stat[:processed],
    failed: @resque::Failure.count,
    queues: queue_sizes.size,
    workers: @resque.workers.size,
    working: @resque.working.size,
    queue_sizes: queue_sizes
  }
end

#workersObject

Normalized snapshot of all registered workers, sorted by id so pagination over the unordered Redis set is deterministic. started is an opaque string (never parsed, like failed_at).



96
97
98
99
100
101
# File 'lib/resque/mcp/adapter.rb', line 96

def workers
  expired_ids = @resque::Worker.all_workers_with_expired_heartbeats.map(&:to_s)
  @resque.workers
    .map { |worker| normalize_worker(worker, expired_ids) }
    .sort_by(&:id)
end