Skip to content

Commit

Permalink
feat(scheduler): expose template data in getJobSchedulers
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Dec 8, 2024
1 parent 06ed468 commit 88168a0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 53 deletions.
40 changes: 16 additions & 24 deletions src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Job } from './job';
import { QueueBase } from './queue-base';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { optsAsJSON, optsFromJSON } from '../utils';
import { array2obj, optsAsJSON, optsFromJSON } from '../utils';

export class JobScheduler extends QueueBase {
private repeatStrategy: RepeatStrategy;
Expand Down Expand Up @@ -209,6 +209,14 @@ export class JobScheduler extends QueueBase {
): Promise<JobSchedulerJson<D>> {
const jobData = await client.hgetall(this.toKey('repeat:' + key));

return this.transformSchedulerData<D>(key, jobData, next);
}

private async transformSchedulerData<D>(
key: string,
jobData: any,
next?: number,
): Promise<JobSchedulerJson<D>> {
if (jobData) {
return {
key,
Expand Down Expand Up @@ -244,30 +252,14 @@ export class JobScheduler extends QueueBase {
};
}

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

if (schedulerAttributes) {
return {
key: id,
name: schedulerAttributes.name,
endDate: parseInt(schedulerAttributes.endDate) || null,
tz: schedulerAttributes.tz || null,
pattern: schedulerAttributes.pattern || null,
every: schedulerAttributes.every || null,
...(schedulerAttributes.data || schedulerAttributes.opts
? {
template: this.getTemplateFromJSON<D>(
schedulerAttributes.data,
schedulerAttributes.opts,
),
}
: {}),
};
}
return this.transformSchedulerData<D>(
id,
rawJobData ? array2obj(rawJobData) : null,
next ? parseInt(next) : null,
);
}

private getTemplateFromJSON<D = any>(
Expand Down
2 changes: 1 addition & 1 deletion src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ export class Queue<
* @param id - identifier of scheduler.
*/
async getJobScheduler(id: string): Promise<JobSchedulerJson<DataType>> {
return (await this.jobScheduler).getJobScheduler<DataType>(id);
return (await this.jobScheduler).getScheduler<DataType>(id);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,20 @@ export class Scripts {
]);
}

getJobSchedulerArgs(id: string): string[] {
const keys: string[] = [this.queue.keys.repeat];

return keys.concat([id]);
}

async getJobScheduler(id: string): Promise<[any, string | null]> {
const client = await this.queue.client;

const args = this.getJobSchedulerArgs(id);

return this.execCommand(client, 'getJobScheduler', args);
}

retryJobArgs(
jobId: string,
lifo: boolean,
Expand Down
19 changes: 19 additions & 0 deletions src/commands/getJobScheduler-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--[[
Get job scheduler record.
Input:
KEYS[1] 'repeat' key
ARGV[1] id
]]

local rcall = redis.call
local jobSchedulerKey = KEYS[1] .. ":" .. ARGV[1]

local score = rcall("ZSCORE", KEYS[1], ARGV[1])

if score then
return {rcall("HGETALL", jobSchedulerKey), score} -- get job data
end

return {nil, nil}
26 changes: 0 additions & 26 deletions src/commands/updateJobOption-1.lua

This file was deleted.

6 changes: 5 additions & 1 deletion tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ describe('Job Scheduler', function () {
tz: null,
pattern: '*/2 * * * * *',
every: null,
next: 1486481042000,
template: {
data: {
foo: 'bar',
Expand Down Expand Up @@ -698,6 +699,7 @@ describe('Job Scheduler', function () {
key: 'rrule',
name: 'rrule',
endDate: null,
next: 1486481042000,
tz: null,
pattern: 'RRULE:FREQ=SECONDLY;INTERVAL=2;WKST=MO',
every: null,
Expand Down Expand Up @@ -1424,6 +1426,8 @@ describe('Job Scheduler', function () {

describe('when repeatable job fails', function () {
it('should continue repeating', async function () {
const date = new Date('2024-12-08 9:24:00');
this.clock.setSystemTime(date);
const repeatOpts = {
pattern: '0 * 1 * *',
};
Expand Down Expand Up @@ -1475,7 +1479,7 @@ describe('Job Scheduler', function () {
tz: null,
pattern: '0 * 1 * *',
every: null,
next: 25200000,
next: 1735714800000,
template: {
data: {
foo: 'bar',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('metrics', function () {
});

beforeEach(function () {
this.clock = sinon.useFakeTimers();
this.clock = sinon.useFakeTimers({ shouldClearNativeTimers: true });
});

beforeEach(async function () {
Expand Down

0 comments on commit 88168a0

Please sign in to comment.