diff --git a/src/classes/job-scheduler.ts b/src/classes/job-scheduler.ts index 241d3e67fc..e873a84e59 100644 --- a/src/classes/job-scheduler.ts +++ b/src/classes/job-scheduler.ts @@ -257,8 +257,8 @@ export class JobScheduler extends QueueBase { } } - async getJobSchedulerTemplate(id: string): Promise { - return this.scripts.getJobSchedulerTemplate(id); + async getJobTemplate(schedulerId: string): Promise { + return this.scripts.getJobSchedulerTemplate(schedulerId); } async getJobSchedulers( diff --git a/src/classes/job.ts b/src/classes/job.ts index b63e5cdf49..74487bf2e1 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -388,7 +388,7 @@ export class Job< this.scripts = new Scripts(this.queue); } - private static optsFromJSON(rawOpts?: string): JobsOptions { + static optsFromJSON(rawOpts?: string): JobsOptions { const opts = JSON.parse(rawOpts || '{}'); const optionEntries = Object.entries(opts) as Array< diff --git a/src/classes/queue.ts b/src/classes/queue.ts index 734f2b878e..211c632177 100644 --- a/src/classes/queue.ts +++ b/src/classes/queue.ts @@ -594,11 +594,16 @@ export class Queue< */ async getJobSchedulerTemplate( id: string, - ): Promise> { + ): Promise<{ data: DataType; opts: JobsOptions }> { const jobScheduler = await this.jobScheduler; - const [jobData, jobId] = await jobScheduler.getJobSchedulerTemplate(id); + const [jobData] = await jobScheduler.getJobTemplate(id); - return this.createJob(jobData, jobId); + const data = JSON.parse(jobData.data || '{}'); + const opts = this.Job.optsFromJSON(jobData.opts); + return { + data, + opts, + }; } /** diff --git a/src/commands/getJobSchedulerTemplate-2.lua b/src/commands/getJobSchedulerTemplate-2.lua index e84092d76a..f978330278 100644 --- a/src/commands/getJobSchedulerTemplate-2.lua +++ b/src/commands/getJobSchedulerTemplate-2.lua @@ -1,6 +1,7 @@ --[[ - Get job scheduler template + Get job scheduler template. Taking the last iterations job's data and options + TODO: return a stored template in the job scheduler itself Input: KEYS[1] repeat key KEYS[2] prefix key @@ -14,7 +15,7 @@ local millis = rcall("ZSCORE", KEYS[1], ARGV[1]) if millis ~= false then local templateJobId = "repeat:" .. ARGV[1] .. ":" .. millis - return {rcall("HGETALL", KEYS[2] .. templateJobId), templateJobId} -- get job data + return {rcall("HGETALL", KEYS[2] .. templateJobId)} -- get job data end return {0, 0} \ No newline at end of file diff --git a/tests/test_job_scheduler.ts b/tests/test_job_scheduler.ts index ce05fa39ba..6554a19954 100644 --- a/tests/test_job_scheduler.ts +++ b/tests/test_job_scheduler.ts @@ -359,12 +359,22 @@ describe('Job Scheduler', function () { every: null, }); - const jobSchedulerTemplate = await queue.getJobSchedulerTemplate('test'); + const { data, opts } = await queue.getJobSchedulerTemplate('test'); - expect(jobSchedulerTemplate.id?.startsWith('repeat:test')).to.be.true; - expect(jobSchedulerTemplate.data).to.deep.equal({ + expect(data).to.deep.equal({ foo: 'bar', }); + expect(opts).to.deep.equal({ + attempts: 0, + delay: 2000, + jobId: 'repeat:test:1486481042000', + prevMillis: 1486481042000, + repeat: { + count: 1, + pattern: '*/2 * * * * *', + }, + timestamp: 1486481040000, + }); this.clock.tick(nextTick);