Skip to content

Commit

Permalink
Merge pull request #185 from alphagov/redis-healthcheck-lock
Browse files Browse the repository at this point in the history
Redis healthcheck lock
  • Loading branch information
thomasleese authored Jan 18, 2021
2 parents 16ca0f3 + 24fe1b0 commit b911c5b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 2.8.4

* Ensure Redis healthcheck avoids potential race condition ([#185](https://github.com/alphagov/govuk_app_config/pull/185))

# 2.8.3

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

# 2.8.2

Expand Down
12 changes: 8 additions & 4 deletions lib/govuk_app_config/govuk_healthcheck/redis.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
require "securerandom"

module GovukHealthcheck
class Redis
def name
:redis
:redis_connectivity
end

def status
client = ::Redis.new

client.set("healthcheck", "val")
client.get("healthcheck")
client.del("healthcheck")
key = "healthcheck-#{SecureRandom.hex}"

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

client.close

Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "2.8.3".freeze
VERSION = "2.8.4".freeze
end
17 changes: 14 additions & 3 deletions spec/lib/govuk_healthcheck/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@
before do
stub_const("Redis", redis)
allow(redis).to receive(:new).and_return(redis_client)
allow(SecureRandom).to receive(:hex).and_return("abc")
end

context "when the database is connected" do
before { allow(redis_client).to receive(:set) }
before do
allow(redis_client)
.to receive(:set).with("healthcheck-abc", anything)
end

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") }
before do
allow(redis_client)
.to receive(:set).with("healthcheck-abc", anything)
.and_raise("error")
end

it_behaves_like "a healthcheck"

it "returns CRITICAL" do
expect(subject.status).to eq(GovukHealthcheck::CRITICAL)
end
Expand Down

0 comments on commit b911c5b

Please sign in to comment.