Fixed shard IPC not working properly

This commit is contained in:
Erik 2023-12-08 13:20:05 +02:00
parent 601f7628b3
commit 5c61eb12e2
5 changed files with 830 additions and 831 deletions

2
@types/Shared.d.ts vendored
View File

@ -38,7 +38,7 @@ export type IPCMessage = {
respawnDelay: number, respawnDelay: number,
timeout: number timeout: number
}, },
_mEval?: string, _mEval?: boolean,
_mEvalResult?: boolean _mEvalResult?: boolean
_logger?: boolean, _logger?: boolean,
_api?: boolean, _api?: boolean,

View File

@ -269,10 +269,8 @@ class DiscordClient extends Client
script = `(${script})(this, ${JSON.stringify(options.context)})`; script = `(${script})(this, ${JSON.stringify(options.context)})`;
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
{ {
if (!process.send)
return reject(new Error('No parent process'));
this.#evals.set(script as string, { resolve, reject }); this.#evals.set(script as string, { resolve, reject });
process.send({ _mEval: true, script, debug: options.debug || process.env.NODE_ENV === 'development' }); this.#intercom.send('mEval', { script, debug: options.debug ?? process.env.NODE_ENV === 'development' });
}); });
} }

File diff suppressed because it is too large Load Diff

View File

@ -209,9 +209,9 @@ class Controller extends EventEmitter
if (message._logger) if (message._logger)
return; return;
// this.logger.debug(`New message from ${shard ? `${message._api ? 'api-' : ''}shard ${shard.id}`: 'manager'}: ${inspect(message)}`); // this.logger.debug(`New message from ${shard ? `${message._api ? 'api-' : ''}shard ${shard.id}`: 'manager'}: ${inspect(message)}`);
if (message._mEval) if (message._mEval)
return this.eval(shard, { script: message._mEval, debug: message.debug || false }); return this.eval(shard, { script: message.script!, debug: message.debug || false });
if (message._commands) if (message._commands)
return this.#slashCommandManager._handleMessage(message as CommandsDef); return this.#slashCommandManager._handleMessage(message as CommandsDef);
if (message._api) if (message._api)

View File

@ -55,7 +55,8 @@ class Shard extends EventEmitter
this.#env = { this.#env = {
...process.env, ...process.env,
SHARDING_MANAGER: true, SHARDING_MANAGER: true, // IMPORTANT, SHARD IPC WILL BREAK IF MISSING
SHARDING_MANAGER_MODE: 'process', // IMPORTANT, SHARD IPC WILL BREAK IF MISSING
SHARD_ID: this.#id, SHARD_ID: this.#id,
SHARD_COUNT: options.totalShards, SHARD_COUNT: options.totalShards,
DISCORD_TOKEN: options.token DISCORD_TOKEN: options.token
@ -170,7 +171,7 @@ class Shard extends EventEmitter
// When killing the process, give it an opportonity to gracefully shut down (i.e. clean up DB connections etc) // When killing the process, give it an opportonity to gracefully shut down (i.e. clean up DB connections etc)
// It simply has to respond with a shutdown message to the shutdown event // It simply has to respond with a shutdown message to the shutdown event
kill () kill ()
{ {
if (this.#process) if (this.#process)
{ {
@ -208,7 +209,7 @@ class Shard extends EventEmitter
return Promise.resolve(); return Promise.resolve();
} }
awaitShutdown () awaitShutdown ()
{ {
this.#respawn = false; this.#respawn = false;
return new Promise<void>((resolve) => return new Promise<void>((resolve) =>
@ -292,7 +293,7 @@ class Shard extends EventEmitter
} }
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
eval (script: string | Function, context?: object): Promise<unknown> eval (script: string | Function, context?: object): Promise<unknown>
{ {
// Stringify the script if it's a Function // Stringify the script if it's a Function
const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script; const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script;