Skip to content

Commit a00d611

Browse files
committed
fix: check if abort controller is supported
1 parent 3ac1b6f commit a00d611

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
node-version: [14, 16, 17]
9+
node-version: [14, 16, 18]
1010
mongodb-version: [3.6, 4.4, 5.0]
1111
steps:
1212
- name: Git checkout

src/Job.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import { IJobParameters, datefields, TJobDatefield } from './types/JobParameters
99
import { JobPriority, parsePriority } from './utils/priority';
1010
import { computeFromInterval, computeFromRepeatAt } from './utils/nextRunAt';
1111

12-
const controller = new AbortController();
13-
const { signal } = controller;
14-
1512
const log = debug('agenda:job');
1613

1714
/**
@@ -376,9 +373,18 @@ export class Job<DATA = unknown | void> {
376373
Job.functionLocationCache[this.attrs.name] = location;
377374
}
378375
// console.log('location', location);
376+
let controller: AbortController | undefined;
377+
let signal: AbortSignal | undefined;
378+
if (typeof AbortController !== 'undefined') {
379+
controller = new AbortController();
380+
({ signal } = controller);
381+
} else {
382+
console.warn('AbortController not supported!');
383+
}
379384

380385
await new Promise<void>((resolve, reject) => {
381386
let stillRunning = true;
387+
382388
const child = fork(
383389
forkHelper.path,
384390
[this.attrs.name, this.attrs._id!.toString(), location],
@@ -410,7 +416,7 @@ export class Job<DATA = unknown | void> {
410416
const checkCancel = () =>
411417
setTimeout(() => {
412418
if (this.canceled) {
413-
controller.abort(); // Stops the child process
419+
controller?.abort(); // Stops the child process
414420
} else if (stillRunning) {
415421
setTimeout(checkCancel, 10000);
416422
}

0 commit comments

Comments
 (0)