Skip to content

Commit

Permalink
fix: add error message to forked child and give it a chance to die
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Dec 9, 2022
1 parent af90bcb commit 7cf3c57
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 99 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1148,13 +1148,9 @@ childWorker.ts
```ts
import 'reflect-metadata';

process.on('message', message => {
if (message === 'cancel') {
process.exit(2);
} else {
console.log('got message', message);
}
});
function isCancelMessage(message): message is { type: 'cancel'; error: string } {
return message !== null && typeof message === 'object' && message.type === 'cancel';
}

(async () => {
const mongooseConnection = /** connect to database */
Expand Down Expand Up @@ -1200,7 +1196,21 @@ process.on('message', message => {
}

// run this job now
await agenda.runForkedJob(jobId);
const job = await agenda.getForkedJob(jobId);

process.on('message', message => {
if (isCancelMessage(message)) {
job.cancel(message.error);
setTimeout(() => {
// kill it after 10 seconds
process.exit(2);
}, 10000);
} else {
console.log('got message', message);
}
});

await job.runJob();

// disconnect database and exit
process.exit(0);
Expand Down
136 changes: 68 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,26 @@
"mongodb": "^4"
},
"devDependencies": {
"eslint": "^8.27.0",
"prettier": "^2.7.1",
"@hokify/eslint-config": "^2.3.6",
"eslint": "^8.29.0",
"prettier": "^2.8.1",
"@hokify/eslint-config": "^2.3.8",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.4",
"@types/debug": "^4.1.7",
"@types/human-interval": "^1.0.0",
"@types/luxon": "^3.1.0",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.9",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.12",
"@types/sinon": "^10.0.13",
"chai": "^4.3.7",
"delay": "5.0.0",
"mocha": "10.1.0",
"mongodb-memory-server": "^8.10.0",
"mongodb-memory-server": "^8.10.1",
"nyc": "^15.1.0",
"sinon": "14.0.2",
"sinon": "15.0.0",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"typedoc": "^0.23.21",
"typescript": "^4.9.3"
"typescript": "^4.9.4"
}
}
11 changes: 7 additions & 4 deletions src/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Job<DATA = unknown | void> {
* you can use it for long running tasks to periodically check if canceled is true,
* also touch will check if and throws that the job got canceled
*/
private canceled?: Error | true;
private canceled?: Error | string | true;

getCanceledMessage() {
return typeof this.canceled === 'object'
Expand All @@ -30,14 +30,17 @@ export class Job<DATA = unknown | void> {

private forkedChild?: ChildProcess;

cancel(error?: Error) {
cancel(error?: Error | string) {
this.agenda.emit(`cancel:${this.attrs.name}`, this);
this.canceled = error || true;
if (this.forkedChild) {
try {
this.forkedChild.send('cancel');
this.forkedChild.send({
type: 'cancel',
error: this.canceled instanceof Error ? this.canceled.message : this.canceled
});
// eslint-disable-next-line no-console
console.info('canceled child', this.attrs.name, this.attrs._id);
console.info('send canceled child', this.attrs.name, this.attrs._id);
} catch (err) {
// eslint-disable-next-line no-console
console.log('cannot send cancel to child');
Expand Down
Loading

0 comments on commit 7cf3c57

Please sign in to comment.