Created
February 13, 2018 15:42
-
-
Save kjlape/d77a0ec09add4cdb8d8c3cb5b957de76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.routes.draw do | |
| # ... | |
| get 'sidekiq-health' => SidekiqHealthMiddleware | |
| # ... | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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