Skip to content

Commit

Permalink
fix(repeat-strategy): add missing Promise return type (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Nov 28, 2023
1 parent cbd5057 commit 6f8f534
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<RepeatableJob[]> {
return (await this.repeat).getRepeatableJobs(start, end, asc);
}

Expand Down
15 changes: 11 additions & 4 deletions src/classes/repeat.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -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<RepeatableJob[]> {
const client = await this.client;

const key = this.keys.repeat;
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/repeatable-job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type RepeatableJob = {
key: string;
name: string;
id: string | null;
endDate: number | null;
tz: string | null;
pattern: string;
next: number;
};
2 changes: 1 addition & 1 deletion src/types/repeat-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export type RepeatStrategy = (
millis: number,
opts: RepeatOptions,
name?: string,
) => number;
) => number | undefined | Promise<number | undefined>;
8 changes: 4 additions & 4 deletions tests/test_repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('repeat', function () {
this.clock.tick(delay + 10);
delay = delay * 2;

if (job.attemptsMade === 10) {
if (job!.attemptsMade === 10) {
resolve();
}
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1518,7 +1518,7 @@ describe('repeat', function () {
let processor;
const processing = new Promise<void>((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'));
Expand Down

0 comments on commit 6f8f534

Please sign in to comment.