Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(move-to-finished): consider addition of prioritized jobs when processing last active job #2176

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/commands/moveToFinished-13.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,17 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists

jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])

-- If jobId is special ID 0:delay, then there is no job to process
if jobId then
if string.sub(jobId, 1, 2) == "0:" then
rcall("LREM", KEYS[2], 1, jobId)

-- If jobId is special ID 0:delay (delay grater than 0), then there is no job to process
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grater -> greater

-- but if ID is 0:0, then there is at least 1 prioritized job to process
if jobId == "0:0" then
jobId = moveJobFromPriorityToActive(KEYS[3], KEYS[2], KEYS[10])
return prepareJobForProcessing(KEYS, ARGV[8], target, jobId, timestamp,
maxJobs, expireTime, opts)
end
else
return prepareJobForProcessing(KEYS, ARGV[8], target, jobId, timestamp, maxJobs,
expireTime, opts)
Expand Down
44 changes: 37 additions & 7 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,10 @@ describe('workers', function () {
await worker.close();
});

it('should processes jobs by priority', async () => {
const normalPriority = [];
const mediumPriority = [];
const highPriority = [];
it('should process jobs by priority', async () => {
const normalPriority: Promise<Job>[] = [];
const mediumPriority: Promise<Job>[] = [];
const highPriority: Promise<Job>[] = [];

let processor;

Expand Down Expand Up @@ -669,14 +669,44 @@ describe('workers', function () {
const worker = new Worker(queueName, processor, { connection });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roggervalf I think that
await Promise.all([...normalPriority,...mediumPriority,...highPriority]);
should be added before creating a new worker, as you need to make sure that all jobs are entered before worker starts processing jobs.
When I run the test with this fixes the test failed as some jobs where added after the worker start processing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, probably in case of dragonfly its needed, ok let me add it back

await worker.waitUntilReady();

// wait for all jobs to enter the queue and then start processing
await Promise.all([normalPriority, mediumPriority, highPriority]);

await processing;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by awaiting this line is sufficient


await worker.close();
});

describe('when prioritized job is added while processing last active job', () => {
it('should process prioritized job whithout delay', async function () {
this.timeout(1000);
await queue.add('test1', { p: 2 }, { priority: 2 });
let counter = 0;
let processor;
const processing = new Promise<void>((resolve, reject) => {
processor = async (job: Job) => {
try {
if (job.name == 'test1')
{await queue.add('test', { p: 2 }, { priority: 2 });}

expect(job.id).to.be.ok;
expect(job.data.p).to.be.eql(2);
} catch (err) {
reject(err);
}

if (++counter === 2) {
resolve();
}
};
});

const worker = new Worker(queueName, processor, { connection });
await worker.waitUntilReady();

await processing;

await worker.close();
});
});

it('process several jobs serially', async () => {
let counter = 1;
const maxJobs = 35;
Expand Down