Skip to content

Commit

Permalink
feat: option to enable a graceful shutdown via cli
Browse files Browse the repository at this point in the history
Closes: 1223
Signed-off-by: Travis Payne <travis.payne@accenture.com>
  • Loading branch information
Travis Payne authored and travis-payne committed Oct 15, 2021
1 parent bfe342e commit c345cb0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/cactus-cmd-api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Prometheus Exporter](#prometheus-exporter)
- [Usage Prometheus](#usage-prometheus)
- [Prometheus Integration](#prometheus-integration)
- [Shutdown Hook](#shutdown-hook)
- [Helper code](#helper-code)
- [response.type.ts](#responsetypets)
- [data-fetcher.ts](#data-fetcherts)
Expand Down Expand Up @@ -384,6 +385,18 @@ Here the `host:port` is where the prometheus exporter metrics are exposed. The t
Once edited, you can start the prometheus service by referencing the above edited prometheus.yml file.
On the prometheus graphical interface (defaulted to http://localhost:9090), choose **Graph** from the menu bar, then select the **Console** tab. From the **Insert metric at cursor** drop down, select **cactus_api_server_total_plugin_imports** and click **execute**

### Shutdown Hook

The API config contains a flag:
```json
{
"enableShutdownHook": true
}
```
This allows for graceful shutdown of the API server after a SIGINT via cli CTRL + C. This hook can be disabled by passing in false either via the TypeScript constructor or the JSON config file.



### Helper code

###### response.type.ts
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"convict": "6.0.0",
"convict-format-with-validator": "6.2.0",
"cors": "2.8.5",
"async-exit-hook": "2.0.1",
"express": "4.17.1",
"express-http-proxy": "1.6.2",
"express-jwt": "6.0.0",
Expand Down
19 changes: 18 additions & 1 deletion packages/cactus-cmd-api-server/src/main/typescript/api-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AddressInfo } from "net";
import type { Server as SecureServer } from "https";
import exitHook from "async-exit-hook";
import os from "os";
import path from "path";
import tls from "tls";
Expand Down Expand Up @@ -37,7 +38,12 @@ import {
import { PluginRegistry } from "@hyperledger/cactus-core";
import { installOpenapiValidationMiddleware } from "@hyperledger/cactus-core";

import { Logger, LoggerProvider, Servers } from "@hyperledger/cactus-common";
import {
Bools,
Logger,
LoggerProvider,
Servers,
} from "@hyperledger/cactus-common";

import { ICactusApiServerOptions } from "./config/config-service";
import OAS from "../json/openapi.json";
Expand All @@ -61,6 +67,7 @@ export interface IApiServerConstructorOptions {
readonly httpServerCockpit?: Server | SecureServer;
readonly config: ICactusApiServerOptions;
readonly prometheusExporter?: PrometheusExporter;
readonly enableShutdownHook?: boolean;
}

export class ApiServer {
Expand All @@ -85,6 +92,8 @@ export class ApiServer {
private readonly expressApi: Application;
private readonly expressCockpit: Application;
private readonly pluginsPath: string;
private readonly enableShutdownHook: boolean;

public prometheusExporter: PrometheusExporter;

public get className(): string {
Expand All @@ -99,6 +108,14 @@ export class ApiServer {
throw new Error(`ApiServer#ctor options.config was falsy`);
}

this.enableShutdownHook = Bools.isBooleanStrict(options.enableShutdownHook)
? (options.enableShutdownHook as boolean)
: true;

if (this.enableShutdownHook) {
exitHook(() => this.shutdown());
}

LoggerProvider.setLogLevel(options.config.logLevel);

if (this.options.httpServerApi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface ICactusApiServerOptions {
keyPairPem: string;
keychainSuffixKeyPairPem: string;
minNodeVersion: string;
enableShutdownHook: boolean;
}

export class ConfigService {
Expand Down Expand Up @@ -396,6 +397,16 @@ export class ConfigService {
format: "*",
default: "CACTUS_NODE_KEY_PAIR_PEM",
},
enableShutdownHook: {
doc:
"It will cause the API server to listen to OS process signals and will attempt " +
"to gracefully shut itself down in response to these when the flag is enabled " +
"(which is the default behavior). You will want to turn this off if you are embedding " +
"the API server in your own application and would like to stop the API server from " +
"meddling in the OS process signal handling when you take care of it yourself in your own code.",
format: Boolean,
default: true,
},
};
}

Expand Down Expand Up @@ -465,6 +476,7 @@ export class ConfigService {
const apiBaseUrl = `${apiProtocol}//${apiHost}:${apiPort}`;
const grpcPort = (schema.grpcPort as SchemaObj).default;
const grpcMtlsEnabled = (schema.grpcMtlsEnabled as SchemaObj).default;
const enableShutdownHook = (schema.enableShutdownHook as SchemaObj).default;

const keyPair = JWK.generateSync("EC", "secp256k1", { use: "sig" }, true);
const keyPairPem = keyPair.toPEM(true);
Expand Down Expand Up @@ -580,6 +592,7 @@ export class ConfigService {
keychainSuffixKeyPairPem: (schema.keychainSuffixKeyPairPem as SchemaObj)
.default,
plugins,
enableShutdownHook,
};
}

Expand Down

0 comments on commit c345cb0

Please sign in to comment.