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(worker): process job when closing and it was moved to active #3042

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export class Worker<

private stalledCheckStopper?: () => void;
private waiting: Promise<number> | null = null;
private waitingRun: Promise<void> = Promise.resolve();
private _repeat: Repeat; // To be deprecated in v6 in favor of Job Scheduler

protected _jobScheduler: JobScheduler;
Expand Down Expand Up @@ -431,6 +432,12 @@ export class Worker<
}

async run() {
this.waitingRun = this.waitRun();

await this.waitingRun;
}

async waitRun() {
if (!this.processFn) {
throw new Error('No process function is defined.');
}
Expand Down Expand Up @@ -535,8 +542,8 @@ export class Worker<
}
}

await asyncFifoQueue.waitAll();
this.running = false;
return await asyncFifoQueue.waitAll();
} catch (error) {
this.running = false;
throw error;
Expand Down Expand Up @@ -819,7 +826,7 @@ will never work with more accuracy than 1ms. */
fetchNextCallback = () => true,
jobsInProgress: Set<{ job: Job; ts: number }>,
): Promise<void | Job<DataType, ResultType, NameType>> {
if (!job || this.closing || this.paused) {
if (!job) {
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed, as the signature of this method already guarantees that the job cannot be undefined or null

return;
}

Expand Down Expand Up @@ -1164,7 +1171,7 @@ will never work with more accuracy than 1ms. */
}

if (this.asyncFifoQueue) {
await this.asyncFifoQueue.waitAll();
await this.waitingRun;
}

reconnect && (await this.blockingConnection.reconnect());
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addDelayedJob-6.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ else
end
end

local deduplicationJobId = deduplicateJob(args[1], opts['de'],
local deduplicationJobId = deduplicateJob(opts['de'],
Copy link
Contributor

Choose a reason for hiding this comment

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

unrelated fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unused variable, not a fix, just re formatting

jobId, deduplicationKey, eventsKey, maxEvents)
if deduplicationJobId then
return deduplicationJobId
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addParentJob-4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ else
end
end

local deduplicationJobId = deduplicateJob(args[1], opts['de'],
local deduplicationJobId = deduplicateJob(opts['de'],
jobId, deduplicationKey, eventsKey, maxEvents)
if deduplicationJobId then
return deduplicationJobId
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addPrioritizedJob-8.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ else
end
end

local deduplicationJobId = deduplicateJob(args[1], opts['de'],
local deduplicationJobId = deduplicateJob(opts['de'],
jobId, deduplicationKey, eventsKey, maxEvents)
if deduplicationJobId then
return deduplicationJobId
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addStandardJob-8.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ else
end
end

local deduplicationJobId = deduplicateJob(args[1], opts['de'],
local deduplicationJobId = deduplicateJob(opts['de'],
jobId, deduplicationKey, eventsKey, maxEvents)
if deduplicationJobId then
return deduplicationJobId
Expand Down
18 changes: 9 additions & 9 deletions src/commands/includes/deduplicateJob.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
--[[
Function to debounce a job.
Function to deduplicate a job.
]]

local function deduplicateJob(prefixKey, deduplicationOpts, jobId, deduplicationKey, eventsKey, maxEvents)
local function deduplicateJob(deduplicationOpts, jobId, deduplicationKey, eventsKey, maxEvents)
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems to be an unrelated fix.

local deduplicationId = deduplicationOpts and deduplicationOpts['id']
if deduplicationId then
local ttl = deduplicationOpts['ttl']
local deduplicationKeyExists
if ttl then
deduplicationKeyExists = not rcall('SET', deduplicationKey, jobId, 'PX', ttl, 'NX')
deduplicationKeyExists = rcall('SET', deduplicationKey, jobId, 'PX', ttl, 'NX')
else
deduplicationKeyExists = not rcall('SET', deduplicationKey, jobId, 'NX')
deduplicationKeyExists = rcall('SET', deduplicationKey, jobId, 'NX')
end
if deduplicationKeyExists then
local currentDebounceJobId = rcall('GET', deduplicationKey)
if deduplicationKeyExists == false then
local currentDeduplicatedJobId = rcall('GET', deduplicationKey)
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
"debounced", "jobId", currentDebounceJobId, "debounceId", deduplicationId)
"debounced", "jobId", currentDeduplicatedJobId, "debounceId", deduplicationId)
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
"deduplicated", "jobId", currentDebounceJobId, "deduplicationId", deduplicationId)
return currentDebounceJobId
"deduplicated", "jobId", currentDeduplicatedJobId, "deduplicationId", deduplicationId)
return currentDeduplicatedJobId
end
end
end
18 changes: 16 additions & 2 deletions tests/test_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,15 @@ describe('events', function () {
);

let debouncedCounter = 0;
queueEvents.on('debounced', ({ jobId }) => {
debouncedCounter++;
const debounced = new Promise<void>(resolve => {
queueEvents.on('debounced', () => {
debouncedCounter++;
if (debouncedCounter == 2) {
resolve();
}
});
});

await job.remove();

await queue.add(testName, { foo: 'bar' }, { debounce: { id: 'a1' } });
Expand All @@ -602,6 +608,11 @@ describe('events', function () {
{ debounce: { id: 'a1' } },
);
await secondJob.remove();
await debounced;

const getDeboundedJobId = await queue.getDebounceJobId('a1');

expect(getDeboundedJobId).to.be.null;

expect(debouncedCounter).to.be.equal(2);
});
Expand Down Expand Up @@ -822,6 +833,9 @@ describe('events', function () {
await secondJob.remove();
await deduplication;

const getDeduplicationJobId = await queue.getDeduplicationJobId('a1');

expect(getDeduplicationJobId).to.be.null;
expect(deduplicatedCounter).to.be.equal(2);
});
});
Expand Down
Loading