|
| 1 | +{ *************************************************************************** |
| 2 | +
|
| 3 | + Copyright (c) 2016-2021 Kike Pérez |
| 4 | +
|
| 5 | + Unit : Quick.Core.Extensions.HealthChecks.Redis |
| 6 | + Description : Core Extensions Entity Health Checks |
| 7 | + Author : Kike Pérez |
| 8 | + Version : 1.0 |
| 9 | + Created : 14/02/2021 |
| 10 | + Modified : 21/02/2021 |
| 11 | +
|
| 12 | + This file is part of QuickCore: https://github.com/exilon/QuickCore |
| 13 | +
|
| 14 | + *************************************************************************** |
| 15 | +
|
| 16 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | + you may not use this file except in compliance with the License. |
| 18 | + You may obtain a copy of the License at |
| 19 | +
|
| 20 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +
|
| 22 | + Unless required by applicable law or agreed to in writing, software |
| 23 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | + See the License for the specific language governing permissions and |
| 26 | + limitations under the License. |
| 27 | +
|
| 28 | + *************************************************************************** } |
| 29 | + |
| 30 | +unit Quick.Core.Extensions.HealthChecks.Redis; |
| 31 | + |
| 32 | +{$i QuickCore.inc} |
| 33 | + |
| 34 | +interface |
| 35 | + |
| 36 | +uses |
| 37 | + System.SysUtils, |
| 38 | + System.TypInfo, |
| 39 | + System.TimeSpan, |
| 40 | + Quick.Commons, |
| 41 | + Quick.Options, |
| 42 | + Quick.Data.Redis, |
| 43 | + Quick.Core.DependencyInjection, |
| 44 | + Quick.Core.Extensions.HealthChecks; |
| 45 | + |
| 46 | +type |
| 47 | + TRedisHealthCheckOptions = class(TOptions) |
| 48 | + private |
| 49 | + fHost : string; |
| 50 | + fPort : Integer; |
| 51 | + fPassword : string; |
| 52 | + fDatabaseNumber : Integer; |
| 53 | + fConnectionTimeout: Integer; |
| 54 | + fReadTimeout: Integer; |
| 55 | + public |
| 56 | + constructor Create; override; |
| 57 | + published |
| 58 | + property Host : string read fHost write fHost; |
| 59 | + property Port : Integer read fPort write fPort; |
| 60 | + property Password : string read fPassword write fPassword; |
| 61 | + property DatabaseNumber : Integer read fDatabaseNumber write fDatabaseNumber; |
| 62 | + property ConnectionTimeout : Integer read fConnectionTimeout write fConnectionTimeout; |
| 63 | + property ReadTimeout : Integer read fReadTimeout write fReadTimeout; |
| 64 | + end; |
| 65 | + |
| 66 | + TRedisHealthCheck = class(THealthCheck) |
| 67 | + private |
| 68 | + fRedisClient : TRedisClient; |
| 69 | + fRedisOptions : TRedisHealthCheckOptions; |
| 70 | + public |
| 71 | + constructor Create(aRedisOptions : TRedisHealthCheckOptions; aTimeSpan : TTimeSpan); |
| 72 | + destructor Destroy; override; |
| 73 | + procedure Check; override; |
| 74 | + end; |
| 75 | + |
| 76 | + TRedisHealthChecksExtension = class(THealthChecksExtension) |
| 77 | + class function AddRedisCheck(const aName : string; aRedisOptionsProc : TConfigureOptionsProc<TRedisHealthCheckOptions>; aTimeSpan : TTimeSpan) : THealthChecksService; |
| 78 | + end; |
| 79 | + |
| 80 | +implementation |
| 81 | + |
| 82 | +{ TRedisHealthChecksExtension } |
| 83 | + |
| 84 | +class function TRedisHealthChecksExtension.AddRedisCheck(const aName : string; aRedisOptionsProc : TConfigureOptionsProc<TRedisHealthCheckOptions>; aTimeSpan : TTimeSpan) : THealthChecksService; |
| 85 | +var |
| 86 | + check : IHealthCheck; |
| 87 | +begin |
| 88 | + if not Assigned(aRedisOptionsProc) then raise Exception.Create('RedisOptions param cannot be nil!'); |
| 89 | + |
| 90 | + var redisOptions := TRedisHealthCheckOptions.Create; |
| 91 | + aRedisOptionsProc(redisOptions); |
| 92 | + check := TRedisHealthCheck.Create(redisOptions,aTimeSpan); |
| 93 | + check.Name := aName; |
| 94 | + Result := HealthChecksService.AddCheck(check); |
| 95 | +end; |
| 96 | + |
| 97 | +{ TEntityHealthCheck } |
| 98 | + |
| 99 | +procedure TRedisHealthCheck.Check; |
| 100 | +begin |
| 101 | + inherited; |
| 102 | + fRedisClient.Disconnect; |
| 103 | + try |
| 104 | + fRedisClient.Connect; |
| 105 | + finally |
| 106 | + fRedisClient.Disconnect; |
| 107 | + end; |
| 108 | +end; |
| 109 | + |
| 110 | +constructor TRedisHealthCheck.Create(aRedisOptions : TRedisHealthCheckOptions; aTimeSpan : TTimeSpan); |
| 111 | +begin |
| 112 | + inherited Create(aTimeSpan); |
| 113 | + fName := 'Redis'; |
| 114 | + fRedisOptions := aRedisOptions; |
| 115 | + fRedisClient := TRedisClient.Create; |
| 116 | + fRedisClient.Host := aRedisOptions.Host; |
| 117 | + fRedisClient.Port := aRedisOptions.Port; |
| 118 | + fRedisClient.DataBaseNumber := aRedisOptions.DatabaseNumber; |
| 119 | + fRedisClient.Password := aRedisOptions.Password; |
| 120 | + fRedisClient.ConnectionTimeout := aRedisOptions.ConnectionTimeout; |
| 121 | + fRedisClient.ReadTimeout := aRedisOptions.ReadTimeout; |
| 122 | +end; |
| 123 | + |
| 124 | +destructor TRedisHealthCheck.Destroy; |
| 125 | +begin |
| 126 | + fRedisClient.Free; |
| 127 | + inherited; |
| 128 | +end; |
| 129 | + |
| 130 | +{ TRedisHealthCheckOptions } |
| 131 | + |
| 132 | +constructor TRedisHealthCheckOptions.Create; |
| 133 | +begin |
| 134 | + inherited; |
| 135 | + fReadTimeout := 3000; |
| 136 | + fConnectionTimeout := 2000; |
| 137 | +end; |
| 138 | + |
| 139 | +end. |
| 140 | + |
0 commit comments