mirror of
https://github.com/wassname/talk.git
synced 2026-09-12 13:01:11 +08:00
feat: removed throng and cluster support (#2961)
This commit is contained in:
@@ -9,7 +9,7 @@ import http from "http";
|
||||
import { Db } from "mongodb";
|
||||
import nunjucks from "nunjucks";
|
||||
import path from "path";
|
||||
import { AggregatorRegistry, register } from "prom-client";
|
||||
import { register } from "prom-client";
|
||||
|
||||
import {
|
||||
cacheHeadersMiddleware,
|
||||
@@ -224,34 +224,11 @@ export default function createMetricsServer(config: Config) {
|
||||
);
|
||||
}
|
||||
|
||||
// If we are running in concurrency mode, we should setup the aggregator for
|
||||
// the cluster metrics.
|
||||
if (config.get("concurrency") > 1) {
|
||||
// Create the aggregator registry for metrics.
|
||||
const aggregatorRegistry = new AggregatorRegistry();
|
||||
|
||||
// Use the aggregator registry to handle serving metrics.
|
||||
server.get("/cluster_metrics", (req, res, next) => {
|
||||
aggregatorRegistry.clusterMetrics((err, metrics) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
res.set("Content-Type", aggregatorRegistry.contentType);
|
||||
res.send(metrics);
|
||||
});
|
||||
});
|
||||
|
||||
logger.info({ path: "/cluster_metrics" }, "mounted metrics handler");
|
||||
} else {
|
||||
// Use the memory register to handle serving metrics.
|
||||
server.get("/metrics", (req, res) => {
|
||||
res.set("Content-Type", register.contentType);
|
||||
res.end(register.metrics());
|
||||
});
|
||||
|
||||
logger.info({ path: "/metrics" }, "mounted metrics handler");
|
||||
}
|
||||
// Use the memory register to handle serving metrics.
|
||||
server.get("/metrics", (req, res) => {
|
||||
res.set("Content-Type", register.contentType);
|
||||
res.end(register.metrics());
|
||||
});
|
||||
|
||||
// Error handling.
|
||||
server.use(notFoundMiddleware);
|
||||
|
||||
@@ -2,7 +2,6 @@ import Joi from "@hapi/joi";
|
||||
import convict from "convict";
|
||||
import { parseConnectionString } from "mongodb-core";
|
||||
import ms from "ms";
|
||||
import os from "os";
|
||||
|
||||
import { LOCALES } from "coral-common/helpers/i18n/locales";
|
||||
import { ensureEndSlash } from "coral-common/utils";
|
||||
@@ -95,12 +94,6 @@ const config = convict({
|
||||
default: false,
|
||||
env: "ENABLE_GRAPHIQL",
|
||||
},
|
||||
concurrency: {
|
||||
doc: "The number of worker nodes to spawn to handle traffic",
|
||||
format: Number,
|
||||
default: os.cpus().length,
|
||||
env: "CONCURRENCY",
|
||||
},
|
||||
port: {
|
||||
doc: "The port to bind.",
|
||||
format: "port",
|
||||
@@ -137,7 +130,6 @@ const config = convict({
|
||||
format: "mongo-uri",
|
||||
default: "mongodb://127.0.0.1:27017/coral",
|
||||
env: "MONGODB_URI",
|
||||
|
||||
sensitive: true,
|
||||
},
|
||||
redis: {
|
||||
@@ -145,7 +137,6 @@ const config = convict({
|
||||
format: "redis-uri",
|
||||
default: "redis://127.0.0.1:6379",
|
||||
env: "REDIS_URI",
|
||||
|
||||
sensitive: true,
|
||||
},
|
||||
redis_options: {
|
||||
@@ -160,7 +151,6 @@ const config = convict({
|
||||
format: "*",
|
||||
default: "keyboard cat", // TODO: (wyattjoh) evaluate best solution
|
||||
env: "SIGNING_SECRET",
|
||||
|
||||
sensitive: true,
|
||||
},
|
||||
signing_algorithm: {
|
||||
@@ -174,7 +164,6 @@ const config = convict({
|
||||
format: "*",
|
||||
default: null,
|
||||
env: "MANAGEMENT_SIGNING_SECRET",
|
||||
|
||||
sensitive: true,
|
||||
},
|
||||
management_signing_algorithm: {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import cluster from "cluster";
|
||||
import express, { Express } from "express";
|
||||
import { GraphQLSchema } from "graphql";
|
||||
import { RedisPubSub } from "graphql-redis-subscriptions";
|
||||
@@ -273,14 +272,10 @@ class Server {
|
||||
signingConfig: this.signingConfig,
|
||||
});
|
||||
|
||||
// We only want to setup a metrics server iff the concurrency is 1 or the
|
||||
// concurrency is greater than one and this is the master process.
|
||||
if (config.get("concurrency") === 1 || cluster.isMaster) {
|
||||
// Configure the metrics server and start it.
|
||||
const port = this.config.get("metrics_port");
|
||||
await listenAndServe(createMetricsServer(this.config), port);
|
||||
logger.info({ port }, "now listening for metrics");
|
||||
}
|
||||
// Configure the metrics server and start it.
|
||||
const port = this.config.get("metrics_port");
|
||||
await listenAndServe(createMetricsServer(this.config), port);
|
||||
logger.info({ port, path: "/metrics" }, "now listening for metrics");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import bunyan, { LogLevelString } from "bunyan";
|
||||
import cluster from "cluster";
|
||||
|
||||
import config from "coral-server/config";
|
||||
|
||||
@@ -11,9 +10,6 @@ export type Logger = ReturnType<typeof bunyan.createLogger>;
|
||||
const logger = bunyan.createLogger({
|
||||
name: "coral",
|
||||
|
||||
// Attach the cluster node information to the log entries.
|
||||
clusterNode: cluster.worker ? `worker.${cluster.worker.id}` : "master",
|
||||
|
||||
// Include file references in log entries.
|
||||
src: true,
|
||||
serializers,
|
||||
|
||||
+4
-52
@@ -29,44 +29,13 @@ process.on("unhandledRejection", (err) => {
|
||||
});
|
||||
|
||||
import express from "express";
|
||||
import throng from "throng";
|
||||
|
||||
import createCoral from "./core";
|
||||
import Server from "./core/server";
|
||||
import logger from "./core/server/logger";
|
||||
|
||||
// Create the app that will serve as the mounting point for the Coral Server.
|
||||
const parent = express();
|
||||
|
||||
// worker will start the worker process.
|
||||
async function worker(server: Server) {
|
||||
try {
|
||||
// Start the server.
|
||||
await server.start({ parent });
|
||||
|
||||
logger.debug("started server worker");
|
||||
} catch (err) {
|
||||
logger.error({ err }, "can not start server in worker mode");
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// master will start the master process.
|
||||
async function master(server: Server) {
|
||||
const workerCount = server.config.get("concurrency");
|
||||
logger.debug({ workerCount }, "spawning workers to handle traffic");
|
||||
|
||||
try {
|
||||
// Process jobs.
|
||||
await server.process();
|
||||
|
||||
logger.debug("started server master");
|
||||
} catch (err) {
|
||||
logger.error({ err }, "can not start server in master mode");
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// bootstrap will create a new Coral server, and start it up.
|
||||
async function bootstrap() {
|
||||
try {
|
||||
@@ -75,31 +44,14 @@ async function bootstrap() {
|
||||
// Create the server instance.
|
||||
const server = createCoral();
|
||||
|
||||
// Determine the number of workers.
|
||||
const workerCount = server.config.get("concurrency");
|
||||
|
||||
// Connect the server to databases.
|
||||
await server.connect();
|
||||
|
||||
if (workerCount === 1) {
|
||||
logger.debug(
|
||||
{ workerCount },
|
||||
"not utilizing cluster as concurrency level is 1"
|
||||
);
|
||||
// Start processing jobs.
|
||||
await server.process();
|
||||
|
||||
// Start processing jobs.
|
||||
await server.process();
|
||||
|
||||
// Start the server.
|
||||
await server.start({ parent });
|
||||
} else {
|
||||
// Launch the server start within throng.
|
||||
throng({
|
||||
workers: workerCount,
|
||||
start: () => worker(server),
|
||||
master: () => master(server),
|
||||
});
|
||||
}
|
||||
// Start the server.
|
||||
await server.start({ parent });
|
||||
} catch (err) {
|
||||
logger.error({ err }, "can not bootstrap server");
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user