status-sharding
Version:
Welcome to Status Sharding! This package is designed to provide an efficient and flexible solution for sharding Discord bots, allowing you to scale your bot across multiple processes or workers.
107 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChildClient = exports.Child = void 0;
const child_process_1 = require("child_process");
/** Child class. */
class Child {
file;
/** The child process. */
process = null;
/** The options for the child process. */
processOptions = {};
/** Creates an instance of Child. */
constructor(file, options) {
this.file = file;
this.processOptions = {};
this.processOptions = {
...this.processOptions,
cwd: options.cwd,
detached: options.detached,
execArgv: options.execArgv,
env: options.clusterData || options.env,
execPath: options.execPath,
gid: options.gid,
serialization: options.serialization,
signal: options.signal,
killSignal: options.killSignal,
silent: options.silent,
stdio: options.stdio,
uid: options.uid,
windowsVerbatimArguments: options.windowsVerbatimArguments,
timeout: options.timeout,
args: options.args,
};
}
/** Spawns the child process. */
spawn() {
this.process = (0, child_process_1.fork)(this.file, this.processOptions.args, this.processOptions);
return this.process;
}
/** Respawns the child process. */
async respawn() {
await this.kill();
return this.spawn();
}
/** Kills the child process. */
async kill() {
if (!this.process || !this.process.pid) {
console.warn('No process to kill.');
return false;
}
try {
this.process.kill();
return new Promise((resolve, reject) => {
this.process?.once('exit', () => {
this.process?.removeAllListeners?.();
resolve(true);
});
this.process?.once('error', (err) => {
console.error('Error with child process:', err);
reject(err);
});
});
}
catch (error) {
console.error('Child termination failed:', error);
return false;
}
}
/** Sends a message to the child process. */
send(message) {
return new Promise((resolve, reject) => {
this.process?.send(message, (err) => {
if (err)
reject(err);
else
resolve();
});
});
}
}
exports.Child = Child;
/** Child client class. */
class ChildClient {
/** The IPC process. */
ipc;
/** Creates an instance of ChildClient. */
constructor() {
this.ipc = process;
}
/** Sends a message to the child process. */
send(message) {
return new Promise((resolve, reject) => {
this.ipc.send?.(message, (err) => {
if (err)
reject(err);
else
resolve();
});
});
}
/** Gets the data of the child process. */
getData() {
return this.ipc.env;
}
}
exports.ChildClient = ChildClient;
//# sourceMappingURL=child.js.map