Skip to content

Commit

Permalink
Merge pull request #183 from alphagov/add-redis-healthcheck
Browse files Browse the repository at this point in the history
Add Redis healthcheck
  • Loading branch information
callumknights authored Jan 13, 2021
2 parents 50c5320 + 5970941 commit 3867153
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* Add new Redis healthcheck and relevant tests (https://github.com/alphagov/govuk_app_config/pull/183)

# 2.8.2

* Allow apps to configure the host and protocol for Statsd ([#180](https://github.com/alphagov/govuk_app_config/pull/180))
Expand Down
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"
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.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module GovukHealthcheck
class Redis
def name
:redis
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_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::Redis 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 3867153

Please sign in to comment.