Skip to content

Commit

Permalink
refactor(scheduler): change method name
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Nov 26, 2024
1 parent 039031b commit 16a8475
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/classes/job-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ export class JobScheduler extends QueueBase {
}
}

async getJobSchedulerTemplate(id: string): Promise<any[]> {
return this.scripts.getJobSchedulerTemplate(id);
async getJobTemplate(schedulerId: string): Promise<any[]> {
return this.scripts.getJobSchedulerTemplate(schedulerId);
}

async getJobSchedulers(
Expand Down
2 changes: 1 addition & 1 deletion src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
11 changes: 8 additions & 3 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,16 @@ export class Queue<
*/
async getJobSchedulerTemplate(
id: string,
): Promise<Job<DataType, ResultType, NameType>> {
): 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,
};
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/commands/getJobSchedulerTemplate-2.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}
16 changes: 13 additions & 3 deletions tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 16a8475

Please sign in to comment.