Class: SidekiqQueueStatus::Middleware
- Inherits:
-
Object
- Object
- SidekiqQueueStatus::Middleware
- Defined in:
- lib/sidekiq_queue_status/middleware.rb
Constant Summary collapse
- DEFAULT_TRESHOLD =
30- HEADERS =
{ 'Content-Type' => 'application/json', 'Cache-Control' => 'no-cache' }
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, configured_tresholds) ⇒ Middleware
constructor
A new instance of Middleware.
- #queues ⇒ Object
Constructor Details
#initialize(app, configured_tresholds) ⇒ Middleware
Returns a new instance of Middleware.
11 12 13 14 |
# File 'lib/sidekiq_queue_status/middleware.rb', line 11 def initialize(app, configured_tresholds) @app = app @latency_treshold = Hash.new(DEFAULT_TRESHOLD).merge(configured_tresholds) end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sidekiq_queue_status/middleware.rb', line 16 def call(env) if env["PATH_INFO"] =~ %r{\A/queue-status\Z} errors = [] queues.each do |name, latency| max_latency = @latency_treshold[name] errors << "Queue #{name} above threshold of #{max_latency}s" if latency > max_latency end body = { latencies: queues, errors: errors }.to_json status = errors.any? ? 503 : 200 [status, HEADERS, [body]] else @app.call(env) end end |
#queues ⇒ Object
31 32 33 |
# File 'lib/sidekiq_queue_status/middleware.rb', line 31 def queues Sidekiq::Queue.all.map { |q| [q.name, q.latency.to_i] }.to_h end |