Skip to content

Commit bb61ffd

Browse files
committed
Types for MonitoringServer
1 parent 9c02a2f commit bb61ffd

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

packages/host/src/lib/host.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
CPMConnectorOptions,
1111
HostProxy,
1212
IComponent,
13-
IMonitoringServer,
1413
IMonitoringServerConstructor,
1514
IObjectLogger,
1615
LogLevel,
@@ -145,7 +144,6 @@ export class Host implements IComponent {
145144
hostSize = this.getSize();
146145
ipvAddress: any;
147146
adapterName: string = "uninitialized";
148-
monitoringServer?: IMonitoringServer;
149147

150148
/**
151149
* S3 client.
@@ -226,7 +224,9 @@ export class Host implements IComponent {
226224
}
227225

228226
if (sthConfig.monitorgingServer) {
229-
this.startMonitoringServer(sthConfig.monitorgingServer.port);
227+
this.startMonitoringServer(sthConfig.monitorgingServer.port).then(() => {}, (e) => {
228+
throw new Error(e);
229+
});
230230
}
231231

232232
this.auditor = new Auditor();
@@ -261,19 +261,17 @@ export class Host implements IComponent {
261261
}
262262
}
263263

264-
private async startMonitoringServer(port: number) {
265-
const { MonitoringServer } = await loadModule({ name: "@scramjet/monitoring-server" }) as unknown as { MonitoringServer: IMonitoringServerConstructor };
264+
private async startMonitoringServer(port: number): Promise<void> {
265+
const { MonitoringServer } = await loadModule<{ MonitoringServer: IMonitoringServerConstructor }>({ name: "@scramjet/monitoring-server" });
266266

267267
this.logger.info("Starting monitoring server on port", port);
268268

269-
this.monitoringServer = new MonitoringServer({
269+
const monitoringServer = new MonitoringServer({
270270
port,
271-
validator: async () => {
272-
return await this.loadCheck.getLoadCheck();
273-
}
271+
check: async () => !!await this.loadCheck.getLoadCheck()
274272
});
275273

276-
this.monitoringServer.startServer();
274+
monitoringServer.start();
277275
}
278276

279277
getId() {

packages/module-loader/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { ObjLogger } from "@scramjet/obj-logger";
22
import { ModuleLoaderOpts } from "@scramjet/types";
33

44
export const logger = new ObjLogger("ModuleLoader");
5-
export async function loadModule(opts: ModuleLoaderOpts): Promise<any> {
5+
export async function loadModule<T>(opts: ModuleLoaderOpts): Promise<T> {
66
logger.info("Loading module", opts.name);
77

88
if (!opts.name) {
99
throw new Error("Name missing");
1010
}
1111

12-
let module: ClassDecorator | null;
12+
let module: T;
1313

1414
const heap = process.memoryUsage().heapUsed;
1515

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
import { createServer } from "http";
2-
import { IMonitoringServer, MonitoringServerOptions } from "@scramjet/types";
2+
import { IMonitoringServer, MonitoringServerOptions, MonitoringServerValidator as MonitoringServerHealthCheck } from "@scramjet/types";
33

44
export class MonitoringServer implements IMonitoringServer {
5-
id = "MonitoringServer";
6-
75
private options: MonitoringServerOptions;
6+
private checks: MonitoringServerHealthCheck[] = [];
87

98
constructor(options: MonitoringServerOptions) {
109
this.options = options;
10+
11+
if (Array.isArray(options.check)) {
12+
this.checks = options.check;
13+
}
14+
15+
if (typeof this.options.check === "function") {
16+
this.checks.push(this.options.check);
17+
}
18+
}
19+
20+
async handleHealtzRequest(): Promise<boolean> {
21+
return Promise.all(this.checks.map(v => v())).then(res => res.every(v => v === true), () => false);
1122
}
1223

13-
startServer() {
14-
const server = createServer(async (req, res) => {
24+
start() {
25+
createServer(async (req, res) => {
1526
if (req.url === "/healtz" && req.method === "GET") {
16-
let ok = true;
17-
18-
if (this.options.validator) {
19-
try {
20-
ok = await this.options.validator();
21-
} catch (_e) {
22-
res.statusCode = 500;
23-
res.end();
24-
}
25-
}
27+
const healtz = await this.handleHealtzRequest();
2628

2729
res.setHeader("Content-type", "text/plain");
2830

29-
if (ok) {
31+
if (healtz) {
3032
res.statusCode = 200;
3133
res.end("ok");
32-
} else {
33-
res.statusCode = 500;
34-
res.end();
34+
35+
return;
3536
}
36-
} else {
37-
res.statusCode = 404;
37+
38+
res.statusCode = 500;
3839
res.end();
3940
}
40-
});
41-
42-
server.listen(this.options.port);
41+
}).listen(this.options.port);
4342
}
4443
}

packages/types/src/monitoring-server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { MaybePromise } from "./utils";
22

3+
export type MonitoringServerValidator = () => MaybePromise<boolean>;
4+
35
export type MonitoringServerOptions = {
46
port: number,
5-
validator: MaybePromise<any>;
7+
check?: MonitoringServerValidator | MonitoringServerValidator[];
68
}
79

810
export interface IMonitoringServer {
9-
startServer(): void;
11+
start(): void;
1012
}
1113

1214
export interface IMonitoringServerConstructor {

0 commit comments

Comments
 (0)