Skip to content

Commit

Permalink
feat: add grpc healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar committed Dec 18, 2024
1 parent 3cabc5a commit 6eb190f
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 193 deletions.
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function bootstrap() {
});
const configService = app.select(SharedModule).get(ApiConfigService);

if (configService.isProduction) {
app.useLogger(['log', 'warn', 'error']);
}

// Middleware setup
app.enableCors({
origin: '*',
Expand Down
64 changes: 64 additions & 0 deletions src/modules/grpc/gen/ts/immortal-health-service.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 0 additions & 117 deletions src/modules/grpc/gen/ts/immortal.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/modules/grpc/gen/ts/kraken.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/modules/grpc/immortal-grpc.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import type { OnModuleInit } from '@nestjs/common';
import type { ClientGrpc, ClientOptions } from '@nestjs/microservices';
import { Client, Transport } from '@nestjs/microservices';

import type { ImmortalServiceClient } from './gen/ts/immortal';
import { IMMORTAL_PACKAGE_NAME, IMMORTAL_SERVICE_NAME } from './gen/ts/immortal';
import type { HealthServiceClient } from './gen/ts/immortal-health-service';
import { HEALTH_SERVICE_NAME, IMMORTAL_PACKAGE_NAME } from './gen/ts/immortal-health-service';

export class ImmortalGrpcClient implements OnModuleInit {
static instance: ImmortalGrpcClient;

private clientGrpc: ClientGrpc;

public serviceClient: ImmortalServiceClient;
public serviceClient: HealthServiceClient;

constructor(
private readonly url: string,
Expand All @@ -32,7 +32,7 @@ export class ImmortalGrpcClient implements OnModuleInit {
options: {
url: this.url,
package: IMMORTAL_PACKAGE_NAME,
protoPath: path.join(__dirname, 'gen', 'immortal.proto'),
protoPath: path.join(__dirname, 'proto', 'immortal-health-service.proto'),
credentials: this.isSecure ? ChannelCredentials.createSsl() : ChannelCredentials.createInsecure(),
},
};
Expand All @@ -41,6 +41,6 @@ export class ImmortalGrpcClient implements OnModuleInit {
}

onModuleInit() {
this.serviceClient = this.clientGrpc.getService<ImmortalServiceClient>(IMMORTAL_SERVICE_NAME);
this.serviceClient = this.clientGrpc.getService<HealthServiceClient>(HEALTH_SERVICE_NAME);
}
}
27 changes: 27 additions & 0 deletions src/modules/grpc/proto/immortal-health-service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";

package immortal;

service HealthService {
rpc Status (StatusRequest) returns (StatusResponse);
}

enum Status {
UNKNOWN = 0;
CONNECTED = 1;
DISCONNECTED = 2;
}

message Service {
string name = 1;
Status status = 2;
string message = 3;
}

message StatusRequest {}

message StatusResponse {
repeated Service services = 1;
int64 uptime = 2;
string version = 3;
}
67 changes: 0 additions & 67 deletions src/modules/grpc/proto/immortal.proto

This file was deleted.

1 change: 1 addition & 0 deletions src/modules/service-registry/enums/service-types.enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export enum ServiceType {
RELAY = 'RELAY',
NNS = 'NNS',
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';

import { Status } from '../../../../src/modules/grpc/gen/ts/immortal-health-service';
import { ImmortalGrpcClient } from '../../../../src/modules/grpc/immortal-grpc.client';
import type { ServiceRegistryEntity } from '../entities/service-registry.entity';
import { ServiceStatus } from '../enums/service-status.enum';
import { ServiceType } from '../enums/service-types.enum';
import { ServiceRegistryRepository } from '../service-registry.repository';
import ServiceRegistryService from './service-registry.service';

Expand Down Expand Up @@ -72,7 +76,17 @@ export default class ServiceRegistryHealthCheckService implements OnModuleInit,
private async performHealthCheck(service: ServiceRegistryEntity): Promise<void> {
try {
this.logger.debug(`Performing health check for service: ${service.type} (ID: ${service._id})...`);
const isHealthy = false; // TODO: Implement actual health check logic (e.g., gRPC health check)

let isHealthy = false;

if (service.type === ServiceType.RELAY) {
const client = new ImmortalGrpcClient(service.url, false);

const res = await lastValueFrom(client.serviceClient.status({}));

isHealthy = res.services.every((s) => s.status === Status.CONNECTED);
}

service.assign({
lastHealthCheck: Date.now(),
status: isHealthy ? ServiceStatus.ACTIVE : ServiceStatus.UN_HEALTHY,
Expand Down
Loading

0 comments on commit 6eb190f

Please sign in to comment.