Skip to content

Commit

Permalink
fix: check if abort controller is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed May 10, 2022
1 parent 3ac1b6f commit a00d611
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16, 17]
node-version: [14, 16, 18]
mongodb-version: [3.6, 4.4, 5.0]
steps:
- name: Git checkout
Expand Down
14 changes: 10 additions & 4 deletions src/Job.ts
Expand Up @@ -9,9 +9,6 @@ import { IJobParameters, datefields, TJobDatefield } from './types/JobParameters
import { JobPriority, parsePriority } from './utils/priority';
import { computeFromInterval, computeFromRepeatAt } from './utils/nextRunAt';

const controller = new AbortController();
const { signal } = controller;

const log = debug('agenda:job');

/**
Expand Down Expand Up @@ -376,9 +373,18 @@ export class Job<DATA = unknown | void> {
Job.functionLocationCache[this.attrs.name] = location;
}
// console.log('location', location);
let controller: AbortController | undefined;
let signal: AbortSignal | undefined;
if (typeof AbortController !== 'undefined') {
controller = new AbortController();
({ signal } = controller);
} else {
console.warn('AbortController not supported!');
}

await new Promise<void>((resolve, reject) => {
let stillRunning = true;

const child = fork(
forkHelper.path,
[this.attrs.name, this.attrs._id!.toString(), location],
Expand Down Expand Up @@ -410,7 +416,7 @@ export class Job<DATA = unknown | void> {
const checkCancel = () =>
setTimeout(() => {
if (this.canceled) {
controller.abort(); // Stops the child process
controller?.abort(); // Stops the child process
} else if (stillRunning) {
setTimeout(checkCancel, 10000);
}
Expand Down

0 comments on commit a00d611

Please sign in to comment.