update
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using BackOffice.BFF.Health.Protobuf;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
|
||||
namespace BackOffice.BFF.Application.HealthCQ.Queries.GetSystemHealth;
|
||||
|
||||
public class GetSystemHealthQueryHandler : IRequestHandler<GetSystemHealthQuery, GetSystemHealthResponse>
|
||||
{
|
||||
private readonly IApplicationContractContext _context;
|
||||
|
||||
public GetSystemHealthQueryHandler(IApplicationContractContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetSystemHealthResponse> Handle(GetSystemHealthQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var services = new List<ServiceHealthModel>();
|
||||
var overallHealthy = true;
|
||||
|
||||
// Check CMS Commission Service
|
||||
var commissionHealthy = false;
|
||||
long commissionResponseTime = 0;
|
||||
try
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
await _context.Commissions.GetAllWeeklyPoolsAsync(
|
||||
new BackOffice.BFF.Commission.Protobuf.GetAllWeeklyPoolsRequest
|
||||
{
|
||||
PageIndex = 1,
|
||||
PageSize = 1
|
||||
},
|
||||
cancellationToken: cancellationToken);
|
||||
stopwatch.Stop();
|
||||
commissionHealthy = true;
|
||||
commissionResponseTime = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Service is down
|
||||
}
|
||||
|
||||
services.Add(new ServiceHealthModel
|
||||
{
|
||||
ServiceName = "CMS Commission Service",
|
||||
Status = commissionHealthy ? "Healthy" : "Unhealthy",
|
||||
Description = commissionHealthy ? "Connected" : "Connection failed",
|
||||
ResponseTimeMs = commissionResponseTime,
|
||||
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
});
|
||||
if (!commissionHealthy) overallHealthy = false;
|
||||
|
||||
// Check CMS Configuration Service
|
||||
var configHealthy = false;
|
||||
long configResponseTime = 0;
|
||||
try
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
await _context.Configurations.GetAllConfigurationsAsync(
|
||||
new BackOffice.BFF.Configuration.Protobuf.GetAllConfigurationsRequest
|
||||
{
|
||||
PageIndex = 1,
|
||||
PageSize = 1
|
||||
},
|
||||
cancellationToken: cancellationToken);
|
||||
stopwatch.Stop();
|
||||
configHealthy = true;
|
||||
configResponseTime = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Service is down
|
||||
}
|
||||
|
||||
services.Add(new ServiceHealthModel
|
||||
{
|
||||
ServiceName = "CMS Configuration Service",
|
||||
Status = configHealthy ? "Healthy" : "Unhealthy",
|
||||
Description = configHealthy ? "Connected" : "Connection failed",
|
||||
ResponseTimeMs = configResponseTime,
|
||||
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
});
|
||||
if (!configHealthy) overallHealthy = false;
|
||||
|
||||
// Check Network Membership Service
|
||||
var networkHealthy = false;
|
||||
long networkResponseTime = 0;
|
||||
try
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
await _context.NetworkMemberships.GetNetworkStatisticsAsync(
|
||||
new BackOffice.BFF.NetworkMembership.Protobuf.GetNetworkStatisticsRequest(),
|
||||
cancellationToken: cancellationToken);
|
||||
stopwatch.Stop();
|
||||
networkHealthy = true;
|
||||
networkResponseTime = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Service is down
|
||||
}
|
||||
|
||||
services.Add(new ServiceHealthModel
|
||||
{
|
||||
ServiceName = "Network Membership Service",
|
||||
Status = networkHealthy ? "Healthy" : "Unhealthy",
|
||||
Description = networkHealthy ? "Connected" : "Connection failed",
|
||||
ResponseTimeMs = networkResponseTime,
|
||||
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
});
|
||||
if (!networkHealthy) overallHealthy = false;
|
||||
|
||||
// Check Club Membership Service
|
||||
var clubHealthy = false;
|
||||
long clubResponseTime = 0;
|
||||
try
|
||||
{
|
||||
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
|
||||
await _context.ClubMemberships.GetClubStatisticsAsync(
|
||||
new BackOffice.BFF.ClubMembership.Protobuf.GetClubStatisticsRequest(),
|
||||
cancellationToken: cancellationToken);
|
||||
stopwatch.Stop();
|
||||
clubHealthy = true;
|
||||
clubResponseTime = stopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Service is down
|
||||
}
|
||||
|
||||
services.Add(new ServiceHealthModel
|
||||
{
|
||||
ServiceName = "Club Membership Service",
|
||||
Status = clubHealthy ? "Healthy" : "Unhealthy",
|
||||
Description = clubHealthy ? "Connected" : "Connection failed",
|
||||
ResponseTimeMs = clubResponseTime,
|
||||
LastCheck = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
});
|
||||
if (!clubHealthy) overallHealthy = false;
|
||||
|
||||
return new GetSystemHealthResponse
|
||||
{
|
||||
OverallHealthy = overallHealthy,
|
||||
Services = { services },
|
||||
CheckedAt = Timestamp.FromDateTime(DateTime.UtcNow)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user