Skip to content

Commit

Permalink
feat(size): properly count only available jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed May 30, 2024
1 parent b3eb2f9 commit 264a9dd
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/PrismaQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class PrismaQueue<
continue;
}
// Query the queue size only when needed to reduce database load.
let estimatedQueueSize = await this.size();
let estimatedQueueSize = await this.size(true);
if (estimatedQueueSize === 0) {
await waitFor(pollInterval);
continue;
Expand Down Expand Up @@ -269,8 +269,8 @@ export class PrismaQueue<
SELECT id
FROM ${tableName}
WHERE (${tableName}."queue" = $1)
AND (${tableName}."runAt" < NOW())
AND (${tableName}."finishedAt" IS NULL)
AND (${tableName}."runAt" < NOW())
AND (${tableName}."notBefore" IS NULL OR ${tableName}."notBefore" < NOW())
ORDER BY ${tableName}."priority" ASC, ${tableName}."runAt" ASC
FOR UPDATE SKIP LOCKED
Expand Down Expand Up @@ -339,10 +339,16 @@ export class PrismaQueue<
return job;
}

public async size(): Promise<number> {
public async size(onlyAvailable?: boolean): Promise<number> {
const { name: queueName } = this;
const date = new Date();
const where: Prisma.QueueJobWhereInput = { queue: queueName, finishedAt: null };
if (onlyAvailable) {
where.runAt = { lte: date };
where.AND = { OR: [{ notBefore: { lte: date } }, { notBefore: null }] };
}
return await this.model.count({
where: { queue: queueName, finishedAt: null },
where: { queue: queueName, finishedAt: null, runAt: { lte: date } },
});
}
}

0 comments on commit 264a9dd

Please sign in to comment.