Skip to content

Commit

Permalink
Improve performance by usually starting from earliest-added items
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpeterxu committed Nov 6, 2021
1 parent cc8447f commit 7fc3750
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/commands/cleanJobsInSet-3.lua
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/test_queue.js
Expand Up @@ -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 });
Expand Down

0 comments on commit 7fc3750

Please sign in to comment.