Skip to content

Commit

Permalink
feat(scheduler): add getJobScheduler method (#2877) ref #2875
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Nov 2, 2024
1 parent c97ed56 commit 956d98c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface JobSchedulerJson {
tz: string | null;
pattern: string | null;
every?: string | null;
next: number;
next?: number;
}

export class JobScheduler extends QueueBase {
Expand Down Expand Up @@ -190,6 +190,22 @@ export class JobScheduler extends QueueBase {
};
}

async getJobScheduler(id: string): Promise<JobSchedulerJson> {
const client = await this.client;
const jobData = await client.hgetall(this.toKey('repeat:' + id));

if (jobData) {
return {
key: id,
name: jobData.name,
endDate: parseInt(jobData.endDate) || null,
tz: jobData.tz || null,
pattern: jobData.pattern || null,
every: jobData.every || null,
};
}
}

async getJobSchedulers(
start = 0,
end = -1,
Expand Down
9 changes: 9 additions & 0 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ export class Queue<
return (await this.repeat).getRepeatableJobs(start, end, asc);
}

/**
* Get Job Scheduler by id
*
* @param id - identifier of scheduler.
*/
async getJobScheduler(id: string): Promise<RepeatableJob> {
return (await this.jobScheduler).getJobScheduler(id);
}

/**
* Get all Job Schedulers
*
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/repeatable-job.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: remove this type in favor of JobSchedulerJson in next breaking change
export type RepeatableJob = {
key: string;
name: string;
Expand All @@ -6,5 +7,5 @@ export type RepeatableJob = {
tz: string | null;
pattern: string | null;
every?: string | null;
next: number;
next?: number;
};
11 changes: 11 additions & 0 deletions tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ describe('Job Scheduler', function () {
{ data: { foo: 'bar' } },
);

const scheduler = await queue.getJobScheduler('test');

expect(scheduler).to.deep.equal({
key: 'test',
name: 'test',
endDate: null,
tz: null,
pattern: '*/2 * * * * *',
every: null,
});

this.clock.tick(nextTick);

let prev: any;
Expand Down

0 comments on commit 956d98c

Please sign in to comment.