Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-manager-bridge/src/main.ts
2498 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Container } from "inversify";
8
import * as express from "express";
9
import * as prometheusClient from "prom-client";
10
import { log, LogrusLogLevel } from "@gitpod/gitpod-protocol/lib/util/logging";
11
import { installLogCountMetric } from "@gitpod/gitpod-protocol/lib/util/logging-node";
12
import { DebugApp } from "@gitpod/gitpod-protocol/lib/util/debug-app";
13
import { TypeORM } from "@gitpod/gitpod-db/lib/typeorm/typeorm";
14
import { TracingManager } from "@gitpod/gitpod-protocol/lib/util/tracing";
15
import { ClusterServiceServer } from "./cluster-service-server";
16
import { BridgeController } from "./bridge-controller";
17
import { AppClusterWorkspaceInstancesController } from "./app-cluster-instance-controller";
18
import { redisMetricsRegistry } from "@gitpod/gitpod-db/lib";
19
import { health, startHealthEndpoint } from "./healthz";
20
21
log.enableJSONLogging("ws-manager-bridge", undefined, LogrusLogLevel.getFromEnv());
22
installLogCountMetric();
23
24
export const start = async (container: Container) => {
25
process.on("uncaughtException", function (err) {
26
// fix for https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/load-balancer-pick-first.ts#L309
27
if (err && err.message && err.message.includes("reading 'startConnecting'")) {
28
log.error("uncaughtException", err);
29
} else {
30
throw err;
31
}
32
});
33
34
try {
35
startHealthEndpoint();
36
const db = container.get(TypeORM);
37
await db.connect();
38
39
const tracingManager = container.get(TracingManager);
40
tracingManager.setup("ws-manager-bridge");
41
42
const metricsApp = express();
43
prometheusClient.collectDefaultMetrics();
44
metricsApp.get("/metrics", async (req, res) => {
45
res.set("Content-Type", prometheusClient.register.contentType);
46
47
const mergedRegistry = prometheusClient.Registry.merge([prometheusClient.register, redisMetricsRegistry()]);
48
res.send(await mergedRegistry.metrics());
49
});
50
const metricsPort = 9500;
51
const metricsHttpServer = metricsApp.listen(metricsPort, "127.0.0.1", () => {
52
log.info(`prometheus metrics server running on: 127.0.0.1:${metricsPort}`);
53
});
54
55
const debugApp = container.get<DebugApp>(DebugApp);
56
debugApp.start();
57
58
const bridgeController = container.get<BridgeController>(BridgeController);
59
await bridgeController.start();
60
61
const clusterServiceServer = container.get<ClusterServiceServer>(ClusterServiceServer);
62
await clusterServiceServer.start();
63
64
const appClusterInstanceController = container.get<AppClusterWorkspaceInstancesController>(
65
AppClusterWorkspaceInstancesController,
66
);
67
appClusterInstanceController.start();
68
69
process.on("SIGTERM", async () => {
70
log.info("SIGTERM received, stopping");
71
bridgeController.dispose();
72
73
if (metricsHttpServer) {
74
metricsHttpServer.close((err: any) => {
75
if (err) {
76
log.warn(`error closing prometheus metrics server`, { err });
77
}
78
});
79
}
80
clusterServiceServer.stop().then(() => log.info("gRPC shutdown completed"));
81
appClusterInstanceController.dispose();
82
});
83
log.info("ws-manager-bridge is up and running");
84
health.isHealthy = true;
85
await new Promise((rs, rj) => {});
86
} catch (err) {
87
log.error("Error during startup. Exiting.", err);
88
health.isHealthy = false;
89
process.exit(1);
90
}
91
};
92
93