-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuHeartbeatMonitor.pas
More file actions
303 lines (264 loc) · 8.78 KB
/
Copy pathuHeartbeatMonitor.pas
File metadata and controls
303 lines (264 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
unit uHeartbeatMonitor;
interface
uses
System.SysUtils,
System.Classes,
System.JSON,
System.Net.HttpClient,
System.Net.URLClient,
System.Net.Mime,
Vcl.ExtCtrls;
type
THeartbeatLogEvent = reference to procedure(const AMessage: string; const ASuccess: Boolean);
THeartbeatMonitor = class
private
FTimer: TTimer;
FURL: string;
FToken: string;
FServiceName: string;
FIntervalMinutes: Integer;
FGraceMinutes: Integer;
FNotificationEmails: string;
FMessage: string;
FOk: Boolean;
FDurationSeconds: Integer;
FOnLog: THeartbeatLogEvent;
FRunning: Boolean;
procedure OnTimerTick(Sender: TObject);
procedure DoLog(const AMessage: string; const ASuccess: Boolean);
function MontarJSONPayload(const AMessage: string; const AOk: Boolean; const ADurationSeconds: Integer): string;
function ExecutarEnvioHTTP(const AMessage: string; const AOk: Boolean; const ADurationSeconds: Integer; const ALogFilePath: string = ''): Boolean;
public
constructor Create;
destructor Destroy; override;
procedure Start;
procedure Stop;
procedure SendHeartbeat(const AAsync: Boolean = True); overload;
procedure SendHeartbeat(const AMessage: string; const AOk: Boolean = True;
const ADurationSeconds: Integer = 1; const AAsync: Boolean = True); overload;
procedure SendHeartbeat(const AMessage: string; const AOk: Boolean;
const ADurationSeconds: Integer; const ALogFilePath: string;
const AAsync: Boolean = True); overload;
property URL: string read FURL write FURL;
property Token: string read FToken write FToken;
property ServiceName: string read FServiceName write FServiceName;
property IntervalMinutes: Integer read FIntervalMinutes write FIntervalMinutes;
property GraceMinutes: Integer read FGraceMinutes write FGraceMinutes;
property NotificationEmails: string read FNotificationEmails write FNotificationEmails;
property Message: string read FMessage write FMessage;
property Ok: Boolean read FOk write FOk;
property DurationSeconds: Integer read FDurationSeconds write FDurationSeconds;
property OnLog: THeartbeatLogEvent read FOnLog write FOnLog;
property Running: Boolean read FRunning;
end;
implementation
{ THeartbeatMonitor }
constructor THeartbeatMonitor.Create;
begin
inherited Create;
FURL := 'https://dashboard.servicoskayser.com.br/api/heartbeat';
FToken := 'clt_live_g957SkC9Kr3q1xhQ2ZfuwP2oiYWf0t9rj89vi4Cp';
FServiceName := 'Servidor de relatório';
FIntervalMinutes := 30;
FGraceMinutes := 5;
FNotificationEmails := 'lucas@kayser.com.br';
FMessage := 'Servidor de relatório online';
FOk := True;
FDurationSeconds := 1;
FRunning := False;
FTimer := TTimer.Create(nil);
FTimer.Enabled := False;
FTimer.Interval := Cardinal(FIntervalMinutes) * 60 * 1000;
FTimer.OnTimer := OnTimerTick;
end;
destructor THeartbeatMonitor.Destroy;
begin
Stop;
FreeAndNil(FTimer);
inherited;
end;
procedure THeartbeatMonitor.Start;
begin
if FRunning then
Exit;
FRunning := True;
FTimer.Interval := Cardinal(FIntervalMinutes) * 60 * 1000;
FTimer.Enabled := True;
// Realiza o primeiro disparo imediatamente ao iniciar
SendHeartbeat(True);
end;
procedure THeartbeatMonitor.Stop;
begin
FRunning := False;
if Assigned(FTimer) then
FTimer.Enabled := False;
end;
procedure THeartbeatMonitor.OnTimerTick(Sender: TObject);
begin
SendHeartbeat(True);
end;
function THeartbeatMonitor.MontarJSONPayload(const AMessage: string; const AOk: Boolean; const ADurationSeconds: Integer): string;
var
JSONObj: TJSONObject;
LMsg: string;
LSeconds: Integer;
begin
if AMessage <> '' then
LMsg := AMessage
else
LMsg := FMessage;
if ADurationSeconds > 0 then
LSeconds := ADurationSeconds
else
LSeconds := FDurationSeconds;
JSONObj := TJSONObject.Create;
try
JSONObj.AddPair('service', FServiceName);
JSONObj.AddPair('interval_minutes', TJSONNumber.Create(FIntervalMinutes));
JSONObj.AddPair('grace_minutes', TJSONNumber.Create(FGraceMinutes));
JSONObj.AddPair('notification_emails', FNotificationEmails);
JSONObj.AddPair('ok', TJSONBool.Create(AOk));
JSONObj.AddPair('message', LMsg);
JSONObj.AddPair('duration_seconds', TJSONNumber.Create(LSeconds));
Result := JSONObj.ToJSON;
finally
JSONObj.Free;
end;
end;
function THeartbeatMonitor.ExecutarEnvioHTTP(const AMessage: string; const AOk: Boolean;
const ADurationSeconds: Integer; const ALogFilePath: string): Boolean;
var
LHttpClient: THTTPClient;
LRequestBody: TStringStream;
LFormData: TMultipartFormData;
LResponse: IHTTPResponse;
LPayload: string;
LHeaders: TNetHeaders;
LMsg: string;
LSeconds: Integer;
LTemAnexo: Boolean;
begin
Result := False;
if AMessage <> '' then
LMsg := AMessage
else
LMsg := FMessage;
if ADurationSeconds > 0 then
LSeconds := ADurationSeconds
else
LSeconds := FDurationSeconds;
LTemAnexo := (ALogFilePath <> '') and FileExists(ALogFilePath);
LHttpClient := THTTPClient.Create;
try
LHttpClient.ConnectionTimeout := 10000; // 10 segundos
LHttpClient.ResponseTimeout := 20000; // 20 segundos
LHttpClient.Accept := 'application/json';
if LTemAnexo then
begin
// Envio multipart/form-data com arquivo de log anexado
SetLength(LHeaders, 1);
LHeaders[0] := TNameValuePair.Create('Authorization', 'Bearer ' + FToken);
LFormData := TMultipartFormData.Create;
try
LFormData.AddField('service', FServiceName);
LFormData.AddField('interval_minutes', IntToStr(FIntervalMinutes));
LFormData.AddField('grace_minutes', IntToStr(FGraceMinutes));
LFormData.AddField('notification_emails', FNotificationEmails);
if AOk then
LFormData.AddField('ok', '1')
else
LFormData.AddField('ok', '0');
LFormData.AddField('message', LMsg);
LFormData.AddField('duration_seconds', IntToStr(LSeconds));
LFormData.AddFile('log_file', ALogFilePath);
LResponse := LHttpClient.Post(FURL, LFormData, nil, LHeaders);
finally
LFormData.Free;
end;
end
else
begin
// Envio padrao JSON application/json
SetLength(LHeaders, 2);
LHeaders[0] := TNameValuePair.Create('Authorization', 'Bearer ' + FToken);
LHeaders[1] := TNameValuePair.Create('Content-Type', 'application/json');
LPayload := MontarJSONPayload(AMessage, AOk, ADurationSeconds);
LRequestBody := TStringStream.Create(LPayload, TEncoding.UTF8);
try
LResponse := LHttpClient.Post(FURL, LRequestBody, nil, LHeaders);
finally
LRequestBody.Free;
end;
end;
if Assigned(LResponse) and (LResponse.StatusCode >= 200) and (LResponse.StatusCode < 300) then
begin
Result := True;
DoLog(Format('[Heartbeat] Sucesso (%d) - %s: %s', [LResponse.StatusCode, FormatDateTime('dd/mm/yyyy hh:nn:ss', Now), LMsg]), True);
end
else
begin
if Assigned(LResponse) then
DoLog(Format('[Heartbeat] Falha (%d: %s) - %s: %s', [LResponse.StatusCode, LResponse.StatusText, FormatDateTime('dd/mm/yyyy hh:nn:ss', Now), LMsg]), False)
else
DoLog(Format('[Heartbeat] Falha: Sem resposta da API - %s: %s', [FormatDateTime('dd/mm/yyyy hh:nn:ss', Now), LMsg]), False);
end;
except
on E: Exception do
begin
DoLog(Format('[Heartbeat] Erro de conexão: %s - %s', [E.Message, FormatDateTime('dd/mm/yyyy hh:nn:ss', Now)]), False);
end;
end;
LHttpClient.Free;
end;
procedure THeartbeatMonitor.DoLog(const AMessage: string; const ASuccess: Boolean);
var
LMsg: string;
LSuccess: Boolean;
begin
if not Assigned(FOnLog) then
Exit;
LMsg := AMessage;
LSuccess := ASuccess;
// Garante que o evento seja despachado de forma segura na Main Thread (VCL)
TThread.Queue(nil,
procedure
begin
if Assigned(FOnLog) then
FOnLog(LMsg, LSuccess);
end);
end;
procedure THeartbeatMonitor.SendHeartbeat(const AAsync: Boolean);
begin
SendHeartbeat(FMessage, FOk, FDurationSeconds, AAsync);
end;
procedure THeartbeatMonitor.SendHeartbeat(const AMessage: string; const AOk: Boolean;
const ADurationSeconds: Integer; const AAsync: Boolean);
begin
SendHeartbeat(AMessage, AOk, ADurationSeconds, '', AAsync);
end;
procedure THeartbeatMonitor.SendHeartbeat(const AMessage: string; const AOk: Boolean;
const ADurationSeconds: Integer; const ALogFilePath: string; const AAsync: Boolean);
var
LMsg: string;
LOk: Boolean;
LDuration: Integer;
LLogFile: string;
begin
LMsg := AMessage;
LOk := AOk;
LDuration := ADurationSeconds;
LLogFile := ALogFilePath;
if AAsync then
begin
TThread.CreateAnonymousThread(
procedure
begin
ExecutarEnvioHTTP(LMsg, LOk, LDuration, LLogFile);
end).Start;
end
else
begin
ExecutarEnvioHTTP(LMsg, LOk, LDuration, LLogFile);
end;
end;
end.