diff --git a/lib/commands/cleanJobsInSet-3.lua b/lib/commands/cleanJobsInSet-3.lua index 0767885e3..98a98d906 100644 --- a/lib/commands/cleanJobsInSet-3.lua +++ b/lib/commands/cleanJobsInSet-3.lua @@ -36,8 +36,12 @@ local rangeEnd = -1 -- If we're only deleting _n_ items, avoid retrieving all items -- for faster performance +-- +-- Start from the tail of the list, since that's where oldest elements +-- are generally added for FIFO lists if limit > 0 then - rangeEnd = limit - 1 + rangeStart = -1 - limit + 1 + rangeEnd = -1 end local jobIds = rcall(command, setKey, rangeStart, rangeEnd) @@ -91,8 +95,8 @@ while ((limit <= 0 or deletedCount < limit) and next(jobIds, nil) ~= nil) do if deletedCount < limit then -- We didn't delete enough. Look for more to delete - rangeStart = rangeStart + limit - rangeEnd = rangeEnd + limit + rangeStart = rangeStart - limit + rangeEnd = rangeEnd - limit jobIds = rcall(command, setKey, rangeStart, rangeEnd) end end diff --git a/test/test_queue.js b/test/test_queue.js index 93203faf8..e703d600b 100644 --- a/test/test_queue.js +++ b/test/test_queue.js @@ -2853,6 +2853,18 @@ describe('Queue', () => { expect(len).to.be.eql(2); }); + it('shouldn\'t clean anything if all jobs are in grace period', async () => { + await queue.add({ some: 'data' }); + await queue.add({ some: 'data' }); + + const cleaned = await queue.clean(5000, 'wait', 1); + expect(cleaned.length).to.be.eql(0); + + const cleaned2 = await queue.clean(5000, 'wait'); + expect(cleaned2.length).to.be.eql(0); + expect(await queue.count()).to.be.eql(2); + }); + it('should properly clean jobs from the priority set', done => { const client = new redis(6379, '127.0.0.1', {}); queue.add({ some: 'data' }, { priority: 5 });