Skip to content

Commit 885b6d6

Browse files
committed
Added HealthChecks
1 parent d6abc32 commit 885b6d6

5 files changed

+952
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{ ***************************************************************************
2+
3+
Copyright (c) 2016-2021 Kike Pérez
4+
5+
Unit : Quick.Core.Extensions.HealthChecks.Entity
6+
Description : Core Extensions Entity Health Checks
7+
Author : Kike Pérez
8+
Version : 1.0
9+
Created : 12/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.Entity;
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.Core.Entity,
42+
Quick.Core.DependencyInjection,
43+
Quick.Core.Extensions.HealthChecks;
44+
45+
type
46+
TEntityHealthCheck = class(THealthCheck)
47+
private
48+
fDBContext : TDBContext;
49+
public
50+
constructor Create(aDBContext : TDBContext; aTimeSpan : TTimeSpan);
51+
destructor Destroy; override;
52+
procedure Check; override;
53+
end;
54+
55+
TEntityHealthChecksExtension = class(THealthChecksExtension)
56+
class function AddDBContextCheck<T : TDBContext>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
57+
end;
58+
59+
implementation
60+
61+
{ TEntityHealthChecksExtension }
62+
63+
class function TEntityHealthChecksExtension.AddDBContextCheck<T>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
64+
var
65+
check : IHealthCheck;
66+
db : T;
67+
begin
68+
Result := HealthChecksService;
69+
db := HealthChecksService.ServiceCollection.Resolve<T>();
70+
//db := (PTypeInfo(TypeInfo(T)).TypeData.ClassType.Create) as T;
71+
//TDBContext(db).Database := HealthChecksService.ServiceCollection.Resolve<T>().Database.Clone;
72+
//TDBContext(db).Connection.FromConnectionString(Integer(TDBContext(db).Connection.Provider),TDBContext(db).Connection.GetCustomConnectionString);
73+
//TDBContext(db).Connect;
74+
check := TEntityHealthCheck.Create(db,aTimeSpan);
75+
check.Name := aName;
76+
Result := HealthChecksService.AddCheck(check);
77+
end;
78+
79+
{ TEntityHealthCheck }
80+
81+
procedure TEntityHealthCheck.Check;
82+
begin
83+
inherited;
84+
try
85+
fDBContext.Database.GetTableNames;
86+
except
87+
on E : Exception do raise Exception.CreateFmt('Error connection to "%s" database!',[fDBContext.Connection.Database]);
88+
end;
89+
end;
90+
91+
constructor TEntityHealthCheck.Create(aDBContext : TDBContext; aTimeSpan : TTimeSpan);
92+
begin
93+
inherited Create(aTimeSpan);
94+
fName := 'Entity';
95+
fDBContext := aDBContext;
96+
end;
97+
98+
destructor TEntityHealthCheck.Destroy;
99+
begin
100+
inherited;
101+
end;
102+
103+
end.
104+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{ ***************************************************************************
2+
3+
Copyright (c) 2016-2021 Kike Pérez
4+
5+
Unit : Quick.Core.Extensions.HealthChecks.SqlServer
6+
Description : Core Extensions SqlServer Health Checks
7+
Author : Kike Pérez
8+
Version : 1.0
9+
Created : 12/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.SqlServer;
31+
32+
{$i QuickCore.inc}
33+
34+
interface
35+
36+
uses
37+
System.SysUtils,
38+
System.TimeSpan,
39+
Quick.Commons,
40+
Quick.Core.Entity,
41+
Quick.Core.DependencyInjection,
42+
Quick.Core.Extensions.HealthChecks;
43+
44+
type
45+
TSqlServerHealthCheck = class(THealthCheck)
46+
private
47+
fEntityDatabase : TEntityDatabase;
48+
public
49+
constructor Create(aDataBase : TEntityDatabase; aTimeSpan : TTimeSpan);
50+
destructor Destroy; override;
51+
procedure Check; override;
52+
end;
53+
54+
TSqlServerHealthChecksExtension = class(THealthChecksExtension)
55+
class function AddSqlServerCheck<T : TDBContext>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
56+
end;
57+
58+
implementation
59+
60+
{ TSqlServerHealthChecksExtension }
61+
62+
class function TSqlServerHealthChecksExtension.AddSqlServerCheck<T>(const aName : string; aTimeSpan : TTimeSpan) : THealthChecksService;
63+
var
64+
check : IHealthCheck;
65+
db : TEntityDatabase;
66+
begin
67+
check := TSqlServerHealthCheck.Create(db,aTimeSpan);
68+
check.Name := aName;
69+
Result := HealthChecksService.AddCheck(check);
70+
end;
71+
72+
{ TSqlServerHealthCheck }
73+
74+
procedure TSqlServerHealthCheck.Check;
75+
begin
76+
inherited;
77+
fEntityDatabase.Connect;
78+
if not fEntityDatabase.IsConnected then raise Exception.CreateFmt('Error connection to "%s" database!',[fEntityDatabase.Connection.Database]);
79+
end;
80+
81+
constructor TSqlServerHealthCheck.Create(aDataBase : TEntityDatabase; aTimeSpan : TTimeSpan);
82+
begin
83+
inherited Create(aTimeSpan);
84+
fName := 'SqlServer';
85+
fEntityDatabase := aDataBase.Clone;
86+
end;
87+
88+
destructor TSqlServerHealthCheck.Destroy;
89+
begin
90+
fEntityDatabase.Free;
91+
inherited;
92+
end;
93+
94+
end.

0 commit comments

Comments
 (0)