Skip to content

Commit

Permalink
fix: add cleanup setInterval() (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Nov 14, 2021
1 parent 54a1994 commit 39a8da2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export class Queue extends EventEmitter {
constructor(options: QueueOptions) {
super();
this.concurrency = options.concurrency;
// It was noticed in test that setTimeout() could sometimes trigger an event
// moments before it was scheduled. This leads to a delta between timeToRun
// and Date.now(), and a link may never crawl. This setInterval() ensures
// these items are eventually processed.
setInterval(() => {
if (this.activeFunctions === 0) this.tick();
}, 2500).unref();
}

add(fn: AsyncFunction, options?: QueueItemOptions) {
Expand All @@ -53,15 +60,8 @@ export class Queue extends EventEmitter {
}
// grab the element at the front of the array
const item = this.q.shift()!;
// Depending on CPU load and other factors setTimeout() is not guranteed to run exactly
// when scheduled. This causes problems if there is only one item in the queue, as
// there's a chance it will never be processed. Allow for a small delta to address this:
const delta = 150;
const readyToExecute =
Math.abs(item.timeToRun - Date.now()) < delta ||
item.timeToRun < Date.now();
// make sure this element is ready to execute - if not, to the back of the stack
if (readyToExecute) {
if (item.timeToRun <= Date.now()) {
// this function is ready to go!
this.activeFunctions++;
item.fn().finally(() => {
Expand Down
3 changes: 2 additions & 1 deletion test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ describe('retries', () => {
const checkPromise = checker.check({
path: 'test/fixtures/basic',
retryErrors: true,
retryErrorsJitter: 10,
});
await promise;
await clock.tickAsync(5000);
Expand All @@ -272,7 +273,7 @@ describe('retries', () => {
path: 'test/fixtures/basic',
retryErrors: true,
retryErrorsCount: 1,
retryErrorsJitter: 1000,
retryErrorsJitter: 10,
});
await promise;
await clock.tickAsync(5000);
Expand Down

0 comments on commit 39a8da2

Please sign in to comment.