Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RedisConnection Check Require Redis Instance Parameter #39

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Changed

- **Breaking**: The Redis check now requires being configured with an instance of the Redis client, via the named `instance` parameter

### Added

- The `static` check, which uses the provided check parameters to return the same result every time.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Requires a configured ActiveRecord connection, and that ActiveRecord migrations

#### `redis`

Requires a configured global Redis client. Does not support configuration. Indicates whether the global client is currently connected to the Redis database. On success, returns something in the following format:
Requires the Redis gem. Requires configuration, an instance of a Redis client. Indicates whether the Redis client passed in is currently connected to the Redis database. On success, returns something in the following format:

```json
{
Expand Down
19 changes: 14 additions & 5 deletions lib/rack/ecg/check/redis_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ module Rack
class ECG
module Check
# @!method initialize
# Checks whether the global Redis client is currently connected to the
# database.
# Checks whether the given Redis client is currently connected to the
# database as identified by the ++instance++ option.
#
# Does not take any options.
# @option parameters instance [Redis] The Redis client
class RedisConnection
attr_reader :redis_instance

def initialize(parameters = {})
@redis_instance = parameters[:instance]
end

def result
value = ""
status = Status::OK
begin
if defined?(::Redis)
value = ::Redis.current.connected?
if redis_instance.nil?
status = Status::ERROR
value = "Redis instance parameters not found"
elsif defined?(::Redis)
value = redis_instance.connected?
status = value ? Status::OK : Status::ERROR
else
status = Status::ERROR
Expand Down
43 changes: 35 additions & 8 deletions spec/rack_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,51 @@ def self.connection

context "redis" do
let(:options) do
{ checks: [:redis] }
{ checks: [[:redis, { instance: instance }]] }
end
let(:instance) { instance_double("Redis", connected?: connected) }
let(:connected) { true }

before do
# make sure Redis is defined
class Redis
def connected?
end
end unless defined?(Redis)
end

context "when available" do
let(:instance) { double("current") }
let(:connected) { true }
it "is reported" do
class Redis
def self.current
end
end
expect(Redis).to(receive(:current).and_return(instance))
expect(instance).to(receive(:connected?).and_return(connected))
get "/_ecg"
expect(json_body["redis"]["status"]).to(eq("ok"))
expect(json_body["redis"]["value"]).to(eq(connected.to_s))
end
end

context "the instance is not connected" do
let(:connected) { false }

it "is reported" do
expect(instance).to(receive(:connected?).and_return(connected))
get "/_ecg"
expect(json_body["redis"]["status"]).to(eq("error"))
expect(json_body["redis"]["value"]).to(eq(connected.to_s))
end
end

context "without instance parameters" do
let(:options) do
{ checks: [:redis] }
end

it "is reported" do
get "/_ecg"
expect(json_body["redis"]["status"]).to(eq("error"))
expect(json_body["redis"]["value"]).to(eq("Redis instance parameters not found"))
end
end

context "when not available" do
it "is reported" do
Object.send(:remove_const, :Redis) if defined?(Redis)
Expand Down