Skip to content

Commit

Permalink
refactor: save template attributes if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Dec 2, 2024
1 parent 38d39a2 commit 4e14b53
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
41 changes: 21 additions & 20 deletions src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import { parseExpression } from 'cron-parser';
import { RedisClient, RepeatBaseOptions, RepeatOptions } from '../interfaces';
import { JobsOptions, RepeatStrategy } from '../types';
import { Job } from './job';
import { JobSchedulerJson, JobSchedulerTemplateJson } from '../interfaces';
import { QueueBase } from './queue-base';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { optsAsJSON, optsFromJSON } from '../utils';

export interface JobSchedulerJson<D = any> {
key: string; // key is actually the job scheduler id
name: string;
id?: string | null;
endDate: number | null;
tz: string | null;
pattern: string | null;
every?: string | null;
next?: number;
template?: {
data: D;
opts: JobsOptions;
};
}

export class JobScheduler extends QueueBase {
private repeatStrategy: RepeatStrategy;

Expand Down Expand Up @@ -262,18 +248,33 @@ export class JobScheduler extends QueueBase {
tz: schedulerAttributes.tz || null,
pattern: schedulerAttributes.pattern || null,
every: schedulerAttributes.every || null,
...(schedulerAttributes.data
...(schedulerAttributes.data || schedulerAttributes.opts
? {
template: {
data: JSON.parse(schedulerAttributes.data || '{}'),
opts: optsFromJSON(schedulerAttributes.opts),
},
template: this.getTemplateFromJSON<D>(
schedulerAttributes.data,
schedulerAttributes.opts,
),
}
: {}),
};
}
}

private getTemplateFromJSON<D = any>(
rawData?: string,
rawOpts?: string,
): JobSchedulerTemplateJson<D> {
console.log(typeof rawOpts);
const template: JobSchedulerTemplateJson<D> = {};
if (rawData) {
template.data = JSON.parse(rawData);
}
if (rawOpts) {
template.opts = optsFromJSON(rawOpts);
}
return template;
}

async getJobSchedulers(
start = 0,
end = -1,
Expand Down
5 changes: 2 additions & 3 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BaseJobOptions,
BulkJobOptions,
IoredisListener,
JobJsonRaw,
JobSchedulerJson,
QueueOptions,
RepeatableJob,
RepeatOptions,
Expand All @@ -14,9 +14,8 @@ import { QueueGetters } from './queue-getters';
import { Repeat } from './repeat';
import { RedisConnection } from './redis-connection';
import { SpanKind, TelemetryAttributes } from '../enums';
import { JobScheduler, JobSchedulerJson } from './job-scheduler';
import { JobScheduler } from './job-scheduler';
import { version } from '../version';
import { optsFromJSON } from '../utils';

export interface ObliterateOpts {
/**
Expand Down
14 changes: 12 additions & 2 deletions src/commands/addJobScheduler-2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ local function storeRepeatableJob(schedulerId, repeatKey, nextMillis, rawOpts, t
end

local jsonTemplateOpts = cjson.encode(templateOpts)
rcall("HMSET", repeatKey .. ":" .. schedulerId, "name", opts['name'], "data", templateData,
"opts", jsonTemplateOpts, unpack(optionalValues))
if jsonTemplateOpts and jsonTemplateOpts ~= '{}' then
table.insert(optionalValues, "opts")
table.insert(optionalValues, jsonTemplateOpts)
end

if templateData and templateData ~= '{}' then
table.insert(optionalValues, "data")
table.insert(optionalValues, templateData)
end

rcall("HMSET", repeatKey .. ":" .. schedulerId, "name", opts['name'],
unpack(optionalValues))
end

-- If we are overriding a repeatable job we must delete the delayed job for
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './debounce-options';
export * from './flow-job';
export * from './ioredis-events';
export * from './job-json';
export * from './job-scheduler-json';
export * from './keep-jobs';
export * from './metrics-options';
export * from './metrics';
Expand Down
18 changes: 18 additions & 0 deletions src/interfaces/job-scheduler-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { JobsOptions } from '../types';

export interface JobSchedulerTemplateJson<D = any> {
data?: D;
opts?: Omit<JobsOptions, 'jobId' | 'repeat' | 'delay'>;
}

export interface JobSchedulerJson<D = any> {
key: string; // key is actually the job scheduler id
name: string;
id?: string | null;
endDate: number | null;
tz: string | null;
pattern: string | null;
every?: string | null;
next?: number;
template?: JobSchedulerTemplateJson<D>;
}
12 changes: 11 additions & 1 deletion tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ describe('Job Scheduler', function () {
data: {
foo: 'bar',
},
opts: {},
},
});

Expand Down Expand Up @@ -693,6 +692,17 @@ describe('Job Scheduler', function () {
name: 'rrule',
});

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

expect(scheduler).to.deep.equal({
key: 'rrule',
name: 'rrule',
endDate: null,
tz: null,
pattern: 'RRULE:FREQ=SECONDLY;INTERVAL=2;WKST=MO',
every: null,
});

this.clock.tick(nextTick);

let prev: any;
Expand Down

0 comments on commit 4e14b53

Please sign in to comment.