mirror of
https://github.com/wassname/talk.git
synced 2026-06-27 18:07:26 +08:00
feat: removed throng and cluster support (#2961)
This commit is contained in:
@@ -10,10 +10,6 @@
|
||||
"description": "The shared secret to use to sign JSON Web Tokens (JWT) with the selected signing algorithm.",
|
||||
"generator": "secret"
|
||||
},
|
||||
"CONCURRENCY": {
|
||||
"description": "The number of worker nodes to spawn to handle traffic.",
|
||||
"value": "1"
|
||||
},
|
||||
"LOCALE": {
|
||||
"description": "Specify the default locale to use for all requests without a locale specified",
|
||||
"value": "en-US"
|
||||
|
||||
Generated
-14
@@ -8261,12 +8261,6 @@
|
||||
"integrity": "sha512-78AdXtlhpCHT0K3EytMpn4JNxaf5tbqbLcbIRoQIHzpTIyjpxLQKRoxU55ujBXAtg3Nl2h/XWvfDa9dsMOd0pQ==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/throng": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/@types/throng/-/throng-4.0.2.tgz",
|
||||
"integrity": "sha512-7tgh3R6vwtjj01URmhWXFSkWnm4wDJjsqLm8WPwIWadYjfsKAFi0HqabMQCU2JJ4TbeSGkb51qv27bBPN5Bubw==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/tmp": {
|
||||
"version": "0.0.33",
|
||||
"resolved": "https://registry.npmjs.org/@types/tmp/-/tmp-0.0.33.tgz",
|
||||
@@ -48699,14 +48693,6 @@
|
||||
"integrity": "sha512-fcwX4mndzpLQKBS1DVYhGAcYaYt7vsHNIvQV+WXMvnow5cgjPphq5CaayLaGsjRdSCKZFNGt7/GYAuXaNOiYCA==",
|
||||
"dev": true
|
||||
},
|
||||
"throng": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/throng/-/throng-4.0.0.tgz",
|
||||
"integrity": "sha1-mDxroZk7WOroWZmKpof/6I34TBc=",
|
||||
"requires": {
|
||||
"lodash.defaults": "^4.0.1"
|
||||
}
|
||||
},
|
||||
"through": {
|
||||
"version": "2.3.8",
|
||||
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||
|
||||
+1
-3
@@ -41,7 +41,7 @@
|
||||
"generate:schema": "node ./scripts/generateSchemaTypes.js",
|
||||
"docz": "docz",
|
||||
"start": "NODE_ENV=production node dist/index.js",
|
||||
"start:development": "NODE_ENV=development CONCURRENCY=${CONCURRENCY:-1} TS_NODE_PROJECT=./src/tsconfig.json ts-node-dev --inspect --transpile-only --no-notify -r tsconfig-paths/register ./src/index.ts",
|
||||
"start:development": "NODE_ENV=development TS_NODE_PROJECT=./src/tsconfig.json ts-node-dev --inspect --transpile-only --no-notify -r tsconfig-paths/register ./src/index.ts",
|
||||
"start:webpackDevServer": "ts-node --transpile-only ./scripts/start.ts",
|
||||
"lint": "npm-run-all --parallel lint:* tscheck:*",
|
||||
"lint:server": "eslint 'src/**/*.{js,ts,tsx}' --ignore-pattern 'src/core/client/**'",
|
||||
@@ -135,7 +135,6 @@
|
||||
"source-map-support": "^0.5.16",
|
||||
"stack-utils": "^2.0.1",
|
||||
"striptags": "^3.1.1",
|
||||
"throng": "^4.0.0",
|
||||
"tsscmp": "^1.0.6",
|
||||
"url-regex": "^5.0.0",
|
||||
"uuid": "^7.0.3",
|
||||
@@ -227,7 +226,6 @@
|
||||
"@types/source-map-support": "^0.5.1",
|
||||
"@types/stack-trace": "0.0.29",
|
||||
"@types/stack-utils": "^1.0.1",
|
||||
"@types/throng": "^4.0.2",
|
||||
"@types/uuid": "^7.0.2",
|
||||
"@types/verror": "^1.10.3",
|
||||
"@types/vinyl": "^2.0.4",
|
||||
|
||||
@@ -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