Skip to content

Commit

Permalink
Bump package.json version to 1.8.5 and improve process termination ha…
Browse files Browse the repository at this point in the history
…ndling in Child and Worker classes
  • Loading branch information
Digital39999 committed Nov 17, 2024
1 parent 0157647 commit e6f530d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.4",
"version": "1.8.5",
"name": "status-sharding",
"author": "Digital39999",
"scripts": {
Expand Down
33 changes: 22 additions & 11 deletions src/classes/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,28 @@ export class Child {
* @returns {Promise<boolean>} If the child process was killed.
*/
public async kill(): Promise<boolean> {
// @ts-ignore
this.process?.removeAllListeners();
return new Promise<boolean>((resolve) => {
try {
process.kill(this.process?.pid as number, 'SIGKILL');
resolve(true);
} catch (error) {
console.error('Worker termination failed.');
throw error;
}
});
if (!this.process || !this.process.pid) {
console.warn('No process to kill.');
return false;
}

this.process.removeAllListeners?.();
try {
this.process.kill();

return new Promise((resolve, reject) => {
this.process?.once('exit', () => resolve(true));

this.process?.once('error', (err) => {
console.error('Error with child process:', err);
reject(err);
});
});
return true;
} catch (error) {
console.error('Child termination failed:', error);
return false;
}
}

/**
Expand Down
31 changes: 20 additions & 11 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,27 @@ export class Worker {
* @returns {Promise<boolean>} The promise.
*/
public async kill(): Promise<boolean> {
// @ts-ignore
this.process?.removeAllListeners();
if (!this.process || !this.process.threadId) {
console.warn('No process to kill.');
return false;
}

return new Promise<boolean>((resolve) => {
try {
process.kill(this.process?.threadId as number, 'SIGKILL');
resolve(true);
} catch (error) {
console.error('Worker termination failed.');
throw error;
}
});
this.process.removeAllListeners?.();
try {
this.process!.terminate();

return new Promise((resolve, reject) => {
this.process?.once('exit', () => resolve(true));

this.process?.once('error', (err) => {
console.error('Error with worker thread:', err);
reject(err);
});
});
} catch (error) {
console.error('Child termination failed:', error);
return false;
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/core/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ export class Cluster<
* @returns {void} The void promise.
*/
private _handleExit(exitCode: number): void {
// this.manager.heartbeat?.removeCluster(this.id);
this.emit('death', this, this.thread?.process);

this.manager._debug('[Death] [Cluster ' + this.id + '] Cluster died with exit code ' + exitCode + '.');
Expand Down

0 comments on commit e6f530d

Please sign in to comment.