Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scheduler): add getJobScheduler method #2877

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading