UNPKG

3.26 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8Object.defineProperty(exports, "__esModule", { value: true });
9const Decorators_1 = require("../ioc/Decorators");
10const AdminPortPlugin_1 = require("../web/AdminPortPlugin");
11const LogManager_1 = require("../log/LogManager");
12const NewrelicUtil_1 = require("../newrelic/NewrelicUtil");
13const HealthCheck_1 = require("./HealthCheck");
14const ContextReadyHealthCheck_1 = require("./ContextReadyHealthCheck");
15const logger = LogManager_1.LogManager.getLogger(__filename);
16let HealthCheckManager = class HealthCheckManager extends HealthCheck_1.HealthCheckGroup {
17 constructor() {
18 super(...arguments);
19 this.healthChecksToRegister = [];
20 }
21 start() {
22 this.healthChecksToRegister.forEach((healthCheck) => {
23 logger.debug(`Registering health check: ${healthCheck.getCheckName()}`);
24 this.addCheck(healthCheck);
25 });
26 super.start();
27 }
28 stop() {
29 super.stop();
30 }
31};
32__decorate([
33 Decorators_1.AutowireGroup(HealthCheck_1.HEALTH_CHECK_GROUP)
34], HealthCheckManager.prototype, "healthChecksToRegister", void 0);
35__decorate([
36 Decorators_1.StartMethod
37], HealthCheckManager.prototype, "start", null);
38__decorate([
39 Decorators_1.StopMethod
40], HealthCheckManager.prototype, "stop", null);
41HealthCheckManager = __decorate([
42 Decorators_1.Lazy(false)
43], HealthCheckManager);
44exports.HealthCheckManager = HealthCheckManager;
45class HealthCheckPlugin {
46 constructor() {
47 this.name = 'HealthCheckPlugin';
48 }
49 getName() {
50 return this.name;
51 }
52 willStart(app, pluginContext) {
53 const context = app.getContext();
54 context.registerSingletons(HealthCheckManager, ContextReadyHealthCheck_1.ContextReadyHealthCheck);
55 }
56 didStart(app, pluginContext) {
57 const context = app.getContext();
58 const express = pluginContext.get(AdminPortPlugin_1.default.CONTEXT_APP_KEY);
59 if (express) {
60 express.get('/health', async (req, res) => {
61 NewrelicUtil_1.NewrelicUtil.setIgnoreTransaction(true);
62 const healthCheckManager = await context.getObjectByName('HealthCheckManager');
63 const healthCheckResult = healthCheckManager.getLastResult();
64 if (healthCheckResult.status === HealthCheck_1.HealthCheckStatus.OK || healthCheckResult.status === HealthCheck_1.HealthCheckStatus.NOT_READY) {
65 res.send(healthCheckResult);
66 }
67 else {
68 res.status(503).send(healthCheckResult);
69 }
70 });
71 }
72 }
73}
74exports.default = HealthCheckPlugin;
75//# sourceMappingURL=HealthCheckPlugin.js.map
\No newline at end of file