destaticify healthz

This commit is contained in:
jesopo 2022-08-10 13:13:53 +00:00
parent caec0e614d
commit 5407e60412
2 changed files with 17 additions and 14 deletions

View File

@ -16,27 +16,29 @@ limitations under the License.
import * as http from "http"; import * as http from "http";
import { LogService } from "matrix-bot-sdk"; import { LogService } from "matrix-bot-sdk";
import { IConfig } from "../config";
// allowed to use the global configuration since this is only intended to be used by `src/index.ts`. // allowed to use the global configuration since this is only intended to be used by `src/index.ts`.
import config from '../config';
export class Healthz { export class Healthz {
private static healthCode: number; private healthCode: number;
public static set isHealthy(val: boolean) { constructor(private config: IConfig) { }
Healthz.healthCode = val ? config.health.healthz.healthyStatus : config.health.healthz.unhealthyStatus;
public set isHealthy(val: boolean) {
this.healthCode = val ? this.config.health.healthz.healthyStatus : this.config.health.healthz.unhealthyStatus;
} }
public static get isHealthy(): boolean { public get isHealthy(): boolean {
return Healthz.healthCode === config.health.healthz.healthyStatus; return this.healthCode === this.config.health.healthz.healthyStatus;
} }
public static listen() { public listen() {
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
res.writeHead(Healthz.healthCode); res.writeHead(this.healthCode);
res.end(`health code: ${Healthz.healthCode}`); res.end(`health code: ${this.healthCode}`);
}); });
server.listen(config.health.healthz.port, config.health.healthz.address, () => { server.listen(this.config.health.healthz.port, this.config.health.healthz.address, () => {
LogService.info("Healthz", `Listening for health requests on ${config.health.healthz.address}:${config.health.healthz.port}`); LogService.info("Healthz", `Listening for health requests on ${this.config.health.healthz.address}:${this.config.health.healthz.port}`);
}); });
} }
} }

View File

@ -39,9 +39,10 @@ import { patchMatrixClient } from "./utils";
LogService.info("index", "Starting bot..."); LogService.info("index", "Starting bot...");
Healthz.isHealthy = false; // start off unhealthy const healthz = new Healthz(config);
healthz.isHealthy = false; // start off unhealthy
if (config.health.healthz.enabled) { if (config.health.healthz.enabled) {
Healthz.listen(); healthz.listen();
} }
let bot: Mjolnir | null = null; let bot: Mjolnir | null = null;
@ -66,7 +67,7 @@ import { patchMatrixClient } from "./utils";
} }
try { try {
await bot.start(); await bot.start();
Healthz.isHealthy = true; healthz.isHealthy = true;
} catch (err) { } catch (err) {
console.error(`Mjolnir failed to start: ${err}`); console.error(`Mjolnir failed to start: ${err}`);
throw err; throw err;