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