Skip to content

Commit 9f292bc

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

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

packages/host/src/lib/host.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class Host implements IComponent {
226226
}
227227

228228
if (sthConfig.monitorgingServer) {
229-
this.startMonitoringServer(sthConfig.monitorgingServer.port);
229+
this.startMonitoringServer(sthConfig.monitorgingServer.port).then(() => true);
230230
}
231231

232232
this.auditor = new Auditor();
@@ -262,15 +262,13 @@ export class Host implements IComponent {
262262
}
263263

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

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

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

276274
this.monitoringServer.startServer();

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: 22 additions & 23 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

1324
startServer() {
14-
const server = createServer(async (req, res) => {
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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 {

0 commit comments

Comments
 (0)