Skip to content

Instantly share code, notes, and snippets.

@kjlape
Created February 13, 2018 15:42
Show Gist options
  • Select an option

  • Save kjlape/d77a0ec09add4cdb8d8c3cb5b957de76 to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/d77a0ec09add4cdb8d8c3cb5b957de76 to your computer and use it in GitHub Desktop.
Rails.application.routes.draw do
# ...
get 'sidekiq-health' => SidekiqHealthMiddleware
# ...
end
class SidekiqHealthMiddleware
LATENCY_TOLERANCE_SECONDS = ENV.fetch('SIDEKIQ_LATENCY_TOLERANCE', 5).to_f
def body
healthy? ? 'OK' : 'UHOH'
end
def healthy?
Sidekiq::Queue.new.latency < LATENCY_TOLERANCE_SECONDS
end
def headers
{ 'Content-Type' => 'text/plain' }
end
def status_code
200
end
def response
[status_code, headers, [body]]
end
def call(*)
response
end
def self.call(*params)
new.call(*params)
end
end
describe SidekiqHealthMiddleware do
describe '.call' do
subject { described_class.call }
let(:queue) { double :queue }
let(:headers) { { 'Content-Type' => 'text/plain' } }
let(:response) { [200, headers, [body]] }
before do
allow(Sidekiq::Queue).to receive(:new) { queue }
allow(queue).to receive(:latency) { latency }
end
context 'ok' do
let(:latency) { described_class::LATENCY_TOLERANCE_SECONDS - 1 }
it { is_expected.to match [200, headers, ['OK']] }
end
context 'not ok' do
let(:latency) { described_class::LATENCY_TOLERANCE_SECONDS + 1 }
it { is_expected.to match [200, headers, ['UHOH']] }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment