From 4aecc2d9060f5ed5324fe7fffc9db85e14ad8ad9 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Wed, 30 Oct 2024 22:53:57 -0500 Subject: [PATCH] feat(scheduler): add getJobScheduler method --- src/classes/job-scheduler.ts | 18 +++++++++++++++++- src/classes/queue.ts | 9 +++++++++ src/interfaces/repeatable-job.ts | 3 ++- tests/test_job_scheduler.ts | 11 +++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/classes/job-scheduler.ts b/src/classes/job-scheduler.ts index 55c12289c9..29820c2eab 100644 --- a/src/classes/job-scheduler.ts +++ b/src/classes/job-scheduler.ts @@ -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 { @@ -190,6 +190,22 @@ export class JobScheduler extends QueueBase { }; } + async getJobScheduler(id: string): Promise { + 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, diff --git a/src/classes/queue.ts b/src/classes/queue.ts index 15a70bd8ce..715ffed480 100644 --- a/src/classes/queue.ts +++ b/src/classes/queue.ts @@ -425,6 +425,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 { + return (await this.jobScheduler).getJobScheduler(id); + } + /** * Get all Job Schedulers * diff --git a/src/interfaces/repeatable-job.ts b/src/interfaces/repeatable-job.ts index e5a2963bf7..0172d8ddea 100644 --- a/src/interfaces/repeatable-job.ts +++ b/src/interfaces/repeatable-job.ts @@ -1,3 +1,4 @@ +// TODO: remove this type in favor of JobSchedulerJson in next breaking change export type RepeatableJob = { key: string; name: string; @@ -6,5 +7,5 @@ export type RepeatableJob = { tz: string | null; pattern: string | null; every?: string | null; - next: number; + next?: number; }; diff --git a/tests/test_job_scheduler.ts b/tests/test_job_scheduler.ts index 490425721e..eeda764ead 100644 --- a/tests/test_job_scheduler.ts +++ b/tests/test_job_scheduler.ts @@ -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;