Skip to content

Commit

Permalink
Add Redis healthcheck
Browse files Browse the repository at this point in the history
This was done in response to the work to enable continuous deployment
for local-links-manager. Given local-links-manager uses Redis on it's
own, without sidekiq, similarly to email-alert-frontend, it made sense
to move the recently written healthcheck by @1pretz1 to govuk-app-config
so it could be used by both applications.

Co-authored-by: Peter Hartshorn
<[email protected]>

Trello: https://trello.com/c/uAx6eKAQ/2276-activate-continuous-deployment-for-local-links-manager
  • Loading branch information
callumknights committed Jan 12, 2021
1 parent 6839359 commit 8c88f30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/govuk_app_config/govuk_healthcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "govuk_app_config/govuk_healthcheck/active_record"
require "govuk_app_config/govuk_healthcheck/mongoid"
require "govuk_app_config/govuk_healthcheck/rails_cache"
require "govuk_app_config/govuk_healthcheck/redis_connection"
require "govuk_app_config/govuk_healthcheck/sidekiq_redis"
require "govuk_app_config/govuk_healthcheck/threshold_check"
require "govuk_app_config/govuk_healthcheck/sidekiq_queue_check"
Expand Down
21 changes: 21 additions & 0 deletions lib/govuk_app_config/govuk_healthcheck/redis_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module GovukHealthcheck
class RedisConnection
def name
:redis_connection
end

def status
client = Redis.new

client.set("healthcheck", "val")
client.get("healthcheck")
client.del("healthcheck")

client.close

GovukHealthcheck::OK
rescue StandardError
GovukHealthcheck::CRITICAL
end
end
end
30 changes: 30 additions & 0 deletions spec/lib/govuk_healthcheck/redis_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "spec_helper"
require "govuk_app_config/govuk_healthcheck"
require_relative "shared_interface"

RSpec.describe GovukHealthcheck::RedisConnection do
let(:redis) { double(:redis) }
let(:redis_client) { double(:redis_client, set: "OK", get: "val", del: 1, close: nil) }

before do
stub_const("Redis", redis)
allow(redis).to receive(:new).and_return(redis_client)
end

context "when the database is connected" do
before { allow(redis_client).to receive(:set) }
it_behaves_like "a healthcheck"
it "returns OK" do
expect(redis_client).to receive(:set).with(anything, anything)
expect(subject.status).to eq(GovukHealthcheck::OK)
end
end

context "when the database is not connected" do
before { allow(redis_client).to receive(:set).with(anything, anything).and_raise("error") }
it_behaves_like "a healthcheck"
it "returns CRITICAL" do
expect(subject.status).to eq(GovukHealthcheck::CRITICAL)
end
end
end

0 comments on commit 8c88f30

Please sign in to comment.