Module: Wurk::Web::StatsRoutes

Defined in:
lib/wurk/web/stats_routes.rb

Overview

Sidekiq Web's machine-readable routes (spec ยง25.4): HEAD / (a Redis round trip, for uptime checks), GET /stats, GET /stats/queues, and GET /dashboard/stats redirecting to /stats. Uptime probes and scripts written against /sidekiq/stats read these shapes, so they are upstream's, not the SPA's /api/stats.

Plain Rack, so it serves both the engine (config/routes.rb points these paths here, behind the engine's gates) and the Rails-free run Sidekiq::Web (Web.call dispatches here, behind the same gates).

Constant Summary collapse

REDIS_KEYS =
%w[redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human].freeze
READ_METHODS =
%w[GET HEAD].freeze
JSON_HEADERS =
{ 'content-type' => 'application/json', 'cache-control' => 'private, no-store' }.freeze

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



24
25
26
# File 'lib/wurk/web/stats_routes.rb', line 24

def call(env)
  serve(env) || [404, { 'content-type' => 'text/plain' }, ['Not Found']]
end

.serve(env) ⇒ Object

The response for a path this module owns, or nil.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wurk/web/stats_routes.rb', line 29

def serve(env)
  method = env['REQUEST_METHOD']
  return unless READ_METHODS.include?(method)

  path = env['PATH_INFO'].to_s
  return head_root if method == 'HEAD' && ['', '/'].include?(path)

  case path
  when '/stats' then json(stats)
  when '/stats/queues' then json(PoolScope.scope { Wurk::Stats.new.queues })
  when '/dashboard/stats' then redirect(env)
  end
end