Skip to content

Commit

Permalink
refactor: enhance getJobScheduler method
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Nov 29, 2024
1 parent 2f2bda7 commit 7143352
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
34 changes: 24 additions & 10 deletions src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Job } from './job';
import { QueueBase } from './queue-base';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { optsAsJSON } from '../utils';
import { optsAsJSON, optsFromJSON } from '../utils';

export interface JobSchedulerJson {
export interface JobSchedulerJson<D = any> {
key: string; // key is actually the job scheduler id
name: string;
id?: string | null;
Expand All @@ -16,6 +16,10 @@ export interface JobSchedulerJson {
pattern: string | null;
every?: string | null;
next?: number;
template?: {
data: D;
opts: JobsOptions;
};
}

export class JobScheduler extends QueueBase {
Expand Down Expand Up @@ -244,18 +248,28 @@ export class JobScheduler extends QueueBase {
};
}

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

if (jobData) {
if (schedulerAttributes) {
return {
key: id,
name: jobData.name,
endDate: parseInt(jobData.endDate) || null,
tz: jobData.tz || null,
pattern: jobData.pattern || null,
every: jobData.every || null,
name: schedulerAttributes.name,
endDate: parseInt(schedulerAttributes.endDate) || null,
tz: schedulerAttributes.tz || null,
pattern: schedulerAttributes.pattern || null,
every: schedulerAttributes.every || null,
...(schedulerAttributes.data
? {
template: {
data: JSON.parse(schedulerAttributes.data || '{}'),
opts: optsFromJSON(schedulerAttributes.opts),
},
}
: {}),
};
}
}
Expand Down
28 changes: 2 additions & 26 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { QueueGetters } from './queue-getters';
import { Repeat } from './repeat';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { JobScheduler } from './job-scheduler';
import { JobScheduler, JobSchedulerJson } from './job-scheduler';
import { version } from '../version';
import { optsFromJSON } from '../utils';

Expand Down Expand Up @@ -584,34 +584,10 @@ export class Queue<
*
* @param id - identifier of scheduler.
*/
async getJobScheduler(id: string): Promise<RepeatableJob> {
async getJobScheduler(id: string): Promise<JobSchedulerJson<DataType>> {
return (await this.jobScheduler).getJobScheduler(id);
}

/**
* Get Job Scheduler template by id
*
* @param id - identifier of scheduler.
*/
async getJobSchedulerTemplate(
id: string,
): Promise<{ data: DataType; opts: JobsOptions }> {
const client = await this.client;

const [templateData, templateOpts] = await client.hmget(
`${this.keys.repeat}:${id}`,
'data',
'opts',
);

const data = JSON.parse(templateData || '{}');
const opts = optsFromJSON(templateOpts);
return {
data,
opts,
};
}

/**
* Get all Job Schedulers
*
Expand Down
13 changes: 6 additions & 7 deletions tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,14 @@ describe('Job Scheduler', function () {
tz: null,
pattern: '*/2 * * * * *',
every: null,
template: {
data: {
foo: 'bar',
},
opts: {},
},
});

const { data, opts } = await queue.getJobSchedulerTemplate('test');

expect(data).to.deep.equal({
foo: 'bar',
});
expect(opts).to.deep.equal({});

this.clock.tick(nextTick);

let prev: any;
Expand Down

0 comments on commit 7143352

Please sign in to comment.