Class: Resque::Mcp::Tools::WorkerStats

Inherits:
Base
  • Object
show all
Defined in:
lib/resque/mcp/tools/worker_stats.rb

Constant Summary collapse

STATES =
%w[all working idle].freeze

Constants inherited from Base

Base::ARGS_FULL_MAX, Base::ARGS_PREVIEW_MAX, Base::BACKTRACE_MAX_FRAMES, Base::ERROR_TRUNCATE_MAX, Base::LIMIT_MAX

Class Method Summary collapse

Class Method Details

.call(server_context:, state: "all", offset: 0, limit: 50) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/resque/mcp/tools/worker_stats.rb', line 23

def self.call(server_context:, state: "all", offset: 0, limit: 50, **)
  unless STATES.include?(state)
    return error_response("state must be one of: #{STATES.join(", ")}")
  end

  all = adapter(server_context).workers
  counts = {
    total: all.size,
    working: all.count(&:working?),
    idle: all.count(&:idle?),
    heartbeat_expired: all.count(&:heartbeat_expired)
  }
  selected = (state == "all") ? all : all.select { |w| w.state == state }
  clamped = clamp_limit(limit)
  workers = (selected[offset, clamped] || []).map do |record|
    job = record.current_job
    record.to_h.merge(current_job: job && {
      queue: job.queue,
      class: job.class_name,
      args_preview: args_preview(job),
      run_at: job.run_at
    })
  end
  success_response({
    workers: workers,
    counts: counts,
    page: page_envelope(
      total: selected.size, offset: offset, limit: clamped,
      returned: workers.size, requested_limit: limit
    )
  }, server_context)
end