Skip to content

Commit

Permalink
fix(scheduler): upsert template when same pattern options are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Nov 28, 2024
1 parent 36b9973 commit 01ca2f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
6 changes: 4 additions & 2 deletions src/commands/addJobScheduler-2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ end
local prevMillis = rcall("ZSCORE", repeatKey, jobSchedulerId)
if prevMillis ~= false then
local delayedJobId = "repeat:" .. jobSchedulerId .. ":" .. prevMillis
local nextDelayedJobId = repeatKey .. ":" .. jobSchedulerId .. ":" .. nextMillis
local nextDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. nextMillis
local nextDelayedJobKey = repeatKey .. ":" .. jobSchedulerId .. ":" .. nextMillis

if rcall("ZSCORE", delayedKey, delayedJobId) ~= false
and rcall("EXISTS", nextDelayedJobId) ~= 1 then
and (rcall("EXISTS", nextDelayedJobKey) ~= 1
or delayedJobId == nextDelayedJobId) then
removeJob(delayedJobId, true, prefixKey, true --[[remove debounce key]])
rcall("ZREM", delayedKey, delayedJobId)
end
Expand Down
89 changes: 70 additions & 19 deletions tests/test_job_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,35 +1403,86 @@ describe('Job Scheduler', function () {
});
});

it('should keep only one delayed job if adding a new repeatable job with the same id', async function () {
const date = new Date('2017-02-07 9:24:00');
const key = 'mykey';
describe('when every option is provided', function () {
it('should keep only one delayed job if adding a new repeatable job with the same id', async function () {
const date = new Date('2017-02-07 9:24:00');
const key = 'mykey';

this.clock.setSystemTime(date);
this.clock.setSystemTime(date);

const nextTick = 2 * ONE_SECOND;
const nextTick = 2 * ONE_SECOND;

await queue.upsertJobScheduler(key, {
every: 10_000,
});
await queue.upsertJobScheduler(key, {
every: 10_000,
});

this.clock.tick(nextTick);
this.clock.tick(nextTick);

let jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);
let jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);

let delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);
let delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);

await queue.upsertJobScheduler(key, {
every: 35_160,
await queue.upsertJobScheduler(key, {
every: 35_160,
});

jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);

delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);
});
});

describe('when pattern option is provided', function () {
it('should keep only one delayed job if adding a new repeatable job with the same id', async function () {
const date = new Date('2017-02-07 9:24:00');
const key = 'mykey';

this.clock.setSystemTime(date);

const nextTick = 2 * ONE_SECOND;

jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);
await queue.upsertJobScheduler(
key,
{
pattern: '0 * 1 * *',
},
{ name: 'test1', data: { foo: 'bar' }, opts: { priority: 1 } },
);

this.clock.tick(nextTick);

let jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);

let delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);

await queue.upsertJobScheduler(
key,
{
pattern: '0 * 1 * *',
},
{ name: 'test2', data: { foo: 'baz' }, opts: { priority: 2 } },
);

jobs = await queue.getJobSchedulers();
expect(jobs).to.have.length(1);

delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);
delayedJobs = await queue.getDelayed();
expect(delayedJobs).to.have.length(1);

expect(delayedJobs[0].name).to.be.equal('test2');
expect(delayedJobs[0].data).to.deep.equal({
foo: 'baz',
});
expect(delayedJobs[0].opts).to.deep.include({
priority: 2,
});
});
});

// This test is flaky and too complex we need something simpler that tests the same thing
Expand Down

0 comments on commit 01ca2f6

Please sign in to comment.