Skip to content

Commit

Permalink
Merge pull request #221 from Concordium/health-check
Browse files Browse the repository at this point in the history
Add the healthCheck endpoint to the gRPCClient
  • Loading branch information
shjortConcordium authored Sep 5, 2023
2 parents d28cbe0 + 780fee4 commit 95a5ebe
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
49 changes: 49 additions & 0 deletions examples/client/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { parseEndpoint } from '../shared/util';
import { createConcordiumClient } from '@concordium/node-sdk';
import { credentials } from '@grpc/grpc-js';

import meow from 'meow';

const cli = meow(
`
Usage
$ yarn run-example <path-to-this-file> [options]
Options
--help, Displays this message
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
`,
{
importMeta: import.meta,
flags: {
endpoint: {
type: 'string',
alias: 'e',
default: 'localhost:20000',
},
},
}
);

const [address, port] = parseEndpoint(cli.flags.endpoint);

const client = createConcordiumClient(
address,
Number(port),
credentials.createInsecure()
);

/**
* Queries the node for its health
*/
(async () => {
// #region documentation-snippet
const check = await client.healthCheck();
if (check.isHealthy) {
console.log('The node is healthy!');
} else {
console.log('The node is not healthy');
console.log('Message: ' + check.message);
}
// #endregion documentation-snippet
})();
7 changes: 4 additions & 3 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## Unreleased

### Fixed
- Added missing fields to `getBlockChainParameters` response. (rootKeys, level1Keys, level2Keys)

### Added

- `sendUpdateInstruction` to the gRPC Client.
- `healthCheck` to the gRPC Client.

### Fixed
- Added missing fields to `getBlockChainParameters` response. (rootKeys, level1Keys, level2Keys)

## 9.3.0

Expand Down
21 changes: 20 additions & 1 deletion packages/common/src/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import * as v1 from './types';
import * as v2 from '../grpc/v2/concordium/types';
import { Base58String, HexString, isRpcError } from './types';
import { QueriesClient } from '../grpc/v2/concordium/service.client';
import type { RpcTransport } from '@protobuf-ts/runtime-rpc';
import { HealthClient } from '../grpc/v2/concordium/health.client';
import type { RpcError, RpcTransport } from '@protobuf-ts/runtime-rpc';
import { CredentialRegistrationId } from './types/CredentialRegistrationId';
import * as translate from './GRPCTypeTranslation';
import { AccountAddress } from './types/accountAddress';
Expand Down Expand Up @@ -51,13 +52,15 @@ export type FindInstanceCreationReponse = {
*/
export class ConcordiumGRPCClient {
client: QueriesClient;
healthClient: HealthClient;

/**
* Initialize a gRPC client for a specific concordium node.
* @param transport RpcTransport to send communication over
*/
constructor(transport: RpcTransport) {
this.client = new QueriesClient(transport);
this.healthClient = new HealthClient(transport);
}

/**
Expand Down Expand Up @@ -1503,6 +1506,22 @@ export class ConcordiumGRPCClient {
private async getConsensusHeight() {
return (await this.getConsensusStatus()).lastFinalizedBlockHeight;
}

/**
* Queries the node to check its health
*
* {@codeblock ~~:client/healthCheck.ts#documentation-snippet}
*
* @returns a HealthCheck indicating whether the node is healthy or not and provides the message from the client, if not healthy.
*/
async healthCheck(): Promise<v1.HealthCheckResponse> {
try {
await this.healthClient.check({});
return { isHealthy: true };
} catch (e) {
return { isHealthy: false, message: (e as RpcError).message };
}
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2047,3 +2047,12 @@ export type SmartContractTypeValues =
| number
| string
| boolean;

export type HealthCheckResponse =
| {
isHealthy: true;
}
| {
isHealthy: false;
message?: string;
};

0 comments on commit 95a5ebe

Please sign in to comment.