Skip to content

Commit

Permalink
Fix SIGKILL issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Sep 5, 2023
1 parent 765b8ea commit 33ae7a8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .changeset/hungry-beds-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-mesh/types': patch
'@graphql-mesh/utils': patch
'@graphql-mesh/cli': patch
---

Cleanup and fix SIGKILL issue
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"ajv": "^8.12.0",
"change-case": "^4.1.2",
"cosmiconfig": "^8.1.3",
"dnscache": "^1.0.2",
"dotenv": "^16.0.3",
"graphql-import-node": "^0.0.5",
"graphql-ws": "^5.12.1",
Expand Down
32 changes: 10 additions & 22 deletions packages/cli/src/commands/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* eslint-disable dot-notation */
import cluster from 'cluster';
import os from 'os';
import dnscache from 'dnscache';
import { execute, ExecutionArgs, subscribe } from 'graphql';
import { makeBehavior } from 'graphql-ws/lib/use/uWebSockets';
import open from 'open';
Expand Down Expand Up @@ -111,19 +110,25 @@ export async function serveMesh(
playgroundTitle = rawServeConfig?.playgroundTitle || cliParams.playgroundTitle;
}
if (!cluster.isWorker && forkNum > 0) {
let mainProcessKilled = false;
registerTerminateHandler(eventName => {
mainProcessKilled = true;
});
for (let i = 0; i < forkNum; i++) {
const worker = cluster.fork();
registerTerminateHandler(eventName => worker.kill(eventName));
}
logger.info(`${cliParams.serveMessage}: ${serverUrl} in ${forkNum} forks`);
cluster.on('exit', (worker, code, signal) => {
logger.error(`Worker ${worker.process.pid} died (${signal || code}). Restarting...`);
const newWorker = cluster.fork();
registerTerminateHandler(eventName => newWorker.kill(eventName));
if (!mainProcessKilled) {
logger.child(`Worker ${worker.id}`).error(`died with ${signal || code}. Restarting...`);
const newWorker = cluster.fork();
registerTerminateHandler(eventName => newWorker.kill(eventName));
}
});
} else {
if (cluster.isWorker) {
logger = logger.child(`Worker ${cluster.worker.id}`);
logger.addPrefix?.(`Worker ${cluster.worker?.id}`);
}
logger.info(`Starting GraphQL Mesh...`);

Expand All @@ -132,23 +137,6 @@ export async function serveMesh(
if (mesh.schema.getType('BigInt')) {
await import('json-bigint-patch');
}
dnscache({
enable: true,
cache: function CacheCtor({ ttl }: { ttl: number }) {
return {
get: (key: string, callback: CallableFunction) =>
mesh.cache
.get(key)
.then(value => callback(null, value))
.catch(e => callback(e)),
set: (key: string, value: string, callback: CallableFunction) =>
mesh.cache
.set(key, value, { ttl })
.then(() => callback())
.catch(e => callback(e)),
};
},
});
logger.info(`${cliParams.serveMessage}: ${serverUrl}`);
registerTerminateHandler(eventName => {
const eventLogger = logger.child(`${eventName} 💀`);
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export type Logger = {
error: (...args: any[]) => void;
debug: (...lazyArgs: LazyLoggerMessage[]) => void;
child: (name: string) => Logger;
addPrefix?: (prefix: string) => Logger;
};

export type SelectionSetParam = SelectionSetNode | DocumentNode | string | SelectionSetNode;
Expand Down
7 changes: 7 additions & 0 deletions packages/utils/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,11 @@ export class DefaultLogger implements Logger {
child(name: string): Logger {
return new DefaultLogger(this.name ? `${this.name} - ${name}` : name);
}

addPrefix(prefix: string): Logger {
if (!this.name?.includes(prefix)) {
this.name = this.name ? `${this.name} - ${prefix}` : prefix;
}
return this;
}
}
18 changes: 2 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7625,7 +7625,7 @@ as-table@^1.0.36:
dependencies:
printable-characters "^1.0.42"

asap@^2.0.0, asap@^2.0.6, asap@~2.0.3, asap@~2.0.6:
asap@^2.0.0, asap@~2.0.3, asap@~2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
integrity sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==
Expand Down Expand Up @@ -10186,14 +10186,6 @@ dns-packet@^5.2.2:
dependencies:
"@leichtgewicht/ip-codec" "^2.0.1"

dnscache@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/dnscache/-/dnscache-1.0.2.tgz#fd3c24d66c141625f594c77be7a8dafee2a66c8a"
integrity sha512-2FFKzmLGOnD+Y378bRKH+gTjRMuSpH7OKgPy31KjjfCoKZx7tU8Dmqfd/3fhG2d/4bppuN8/KtWMUZBAcUCRnQ==
dependencies:
asap "^2.0.6"
lodash.clone "^4.5.0"

doctrine@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d"
Expand Down Expand Up @@ -15225,11 +15217,6 @@ lodash.camelcase@^4.3.0:
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
integrity sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==

lodash.clone@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clone/-/lodash.clone-4.5.0.tgz#195870450f5a13192478df4bc3d23d2dea1907b6"
integrity sha512-GhrVeweiTD6uTmmn5hV/lzgCQhccwReIVRLHp7LT4SopOjqEZ5BbX8b5WWEtAKasjmy8hR7ZPwsYlxRCku5odg==

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
Expand Down Expand Up @@ -22025,9 +22012,8 @@ typical@^5.2.0:
resolved "https://registry.yarnpkg.com/typical/-/typical-5.2.0.tgz#4daaac4f2b5315460804f0acf6cb69c52bb93066"
integrity sha512-dvdQgNDNJo+8B2uBQoqdb11eUCE1JQXhvjC/CZtgvZseVd5TYMXnq0+vuUemXbd/Se29cTaUuPX3YIc2xgbvIg==

uWebSockets.js@uNetworking/uWebSockets.js#v20.31.0:
"uWebSockets.js@uNetworking/uWebSockets.js#semver:^20":
version "20.31.0"
uid "809b99d2d7d12e2cbf89b7135041e9b41ff84084"
resolved "https://codeload.github.com/uNetworking/uWebSockets.js/tar.gz/809b99d2d7d12e2cbf89b7135041e9b41ff84084"

ua-parser-js@^1.0.35:
Expand Down

0 comments on commit 33ae7a8

Please sign in to comment.