From 6f8f5342cc8aa03f596d9ed5b8831f96a1d4c736 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Tue, 28 Nov 2023 07:41:33 -0600 Subject: [PATCH] fix(repeat-strategy): add missing Promise return type (#2301) --- src/classes/queue.ts | 8 ++++++-- src/classes/repeat.ts | 15 +++++++++++---- src/interfaces/index.ts | 1 + src/interfaces/repeatable-job.ts | 9 +++++++++ src/types/repeat-strategy.ts | 2 +- tests/test_repeat.ts | 8 ++++---- 6 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 src/interfaces/repeatable-job.ts diff --git a/src/classes/queue.ts b/src/classes/queue.ts index a6f1c6f346..2cd47e5d5c 100644 --- a/src/classes/queue.ts +++ b/src/classes/queue.ts @@ -5,10 +5,10 @@ import { BulkJobOptions, IoredisListener, QueueOptions, + RepeatableJob, RepeatOptions, } from '../interfaces'; import { FinishedStatus, JobsOptions, MinimalQueue } from '../types'; -import { isRedisInstance } from '../utils'; import { Job } from './job'; import { QueueGetters } from './queue-getters'; import { Repeat } from './repeat'; @@ -301,7 +301,11 @@ export class Queue< * @param asc - Determine the order in which jobs are returned based on their * next execution time. */ - async getRepeatableJobs(start?: number, end?: number, asc?: boolean) { + async getRepeatableJobs( + start?: number, + end?: number, + asc?: boolean, + ): Promise { return (await this.repeat).getRepeatableJobs(start, end, asc); } diff --git a/src/classes/repeat.ts b/src/classes/repeat.ts index 0925c80aad..1bfe97b4ea 100644 --- a/src/classes/repeat.ts +++ b/src/classes/repeat.ts @@ -1,6 +1,6 @@ import { parseExpression } from 'cron-parser'; import { createHash } from 'crypto'; -import { RepeatBaseOptions, RepeatOptions } from '../interfaces'; +import { RepeatBaseOptions, RepeatableJob, RepeatOptions } from '../interfaces'; import { JobsOptions, RepeatStrategy } from '../types'; import { Job } from './job'; import { QueueBase } from './queue-base'; @@ -169,7 +169,7 @@ export class Repeat extends QueueBase { return this.scripts.removeRepeatable(repeatJobId, repeatJobKey); } - private keyToData(key: string, next?: number) { + private keyToData(key: string, next?: number): RepeatableJob { const data = key.split(':'); const pattern = data.slice(4).join(':') || null; @@ -184,7 +184,11 @@ export class Repeat extends QueueBase { }; } - async getRepeatableJobs(start = 0, end = -1, asc = false) { + async getRepeatableJobs( + start = 0, + end = -1, + asc = false, + ): Promise { const client = await this.client; const key = this.keys.repeat; @@ -231,7 +235,10 @@ function getRepeatKey(name: string, repeat: RepeatOptions) { return `${name}:${jobId}:${endDate}:${tz}:${suffix}`; } -export const getNextMillis = (millis: number, opts: RepeatOptions): number => { +export const getNextMillis = ( + millis: number, + opts: RepeatOptions, +): number | undefined => { const pattern = opts.pattern; if (pattern && opts.every) { throw new Error( diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index d09707097f..ace81ce3ff 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -16,6 +16,7 @@ export * from './queue-options'; export * from './rate-limiter-options'; export * from './redis-options'; export * from './redis-streams'; +export * from './repeatable-job'; export * from './repeat-options'; export * from './sandboxed-job-processor'; export * from './sandboxed-job'; diff --git a/src/interfaces/repeatable-job.ts b/src/interfaces/repeatable-job.ts new file mode 100644 index 0000000000..1e1e45bd03 --- /dev/null +++ b/src/interfaces/repeatable-job.ts @@ -0,0 +1,9 @@ +export type RepeatableJob = { + key: string; + name: string; + id: string | null; + endDate: number | null; + tz: string | null; + pattern: string; + next: number; +}; diff --git a/src/types/repeat-strategy.ts b/src/types/repeat-strategy.ts index cef79824b2..54c163b0e1 100644 --- a/src/types/repeat-strategy.ts +++ b/src/types/repeat-strategy.ts @@ -4,4 +4,4 @@ export type RepeatStrategy = ( millis: number, opts: RepeatOptions, name?: string, -) => number; +) => number | undefined | Promise; diff --git a/tests/test_repeat.ts b/tests/test_repeat.ts index a26a83829e..2a70b520ca 100644 --- a/tests/test_repeat.ts +++ b/tests/test_repeat.ts @@ -87,7 +87,7 @@ describe('repeat', function () { this.clock.tick(delay + 10); delay = delay * 2; - if (job.attemptsMade === 10) { + if (job!.attemptsMade === 10) { resolve(); } }); @@ -593,7 +593,7 @@ describe('repeat', function () { async job => { this.clock.tick(nextTick); - if (job.opts.repeat.count == 5) { + if (job.opts.repeat!.count == 5) { const removed = await queue.removeRepeatable('rrule', repeat); expect(removed).to.be.true; } @@ -1178,7 +1178,7 @@ describe('repeat', function () { const removed = await queue.removeRepeatableByKey(createdJob.repeatJobKey); const delayedCount = await queue.getJobCountByTypes('delayed'); expect(delayedCount).to.be.equal(0); - expect(job.repeatJobKey).to.not.be.undefined; + expect(job!.repeatJobKey).to.not.be.undefined; expect(removed).to.be.true; const repeatableJobsAfterRemove = await queue.getRepeatableJobs(); expect(repeatableJobsAfterRemove).to.have.length(0); @@ -1518,7 +1518,7 @@ describe('repeat', function () { let processor; const processing = new Promise((resolve, reject) => { processor = async (job: Job) => { - if (job.opts.repeat.count === 1) { + if (job.opts.repeat!.count === 1) { resolve(); } else { reject(new Error('repeatable job got the wrong repeat count'));