Skip to content

Commit

Permalink
fix(testing): Refactor worker waiting logic during e2e
Browse files Browse the repository at this point in the history
Prevent deadlocks while waiting for the worker to process async jobs during e2e tests.
  • Loading branch information
SokratisVidros committed Jan 29, 2025
1 parent d2fd459 commit 0f6370d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions libs/testing/src/jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ export class JobsService {
unfinishedJobs?: number;
}) {
let runningJobs = 0;
let totalCount = 0;
unfinishedJobs = Math.max(unfinishedJobs, 0);

const workflowMatch = templateId
? { _templateId: Array.isArray(templateId) ? { $in: templateId } : templateId }
: {};
const workflowMatch = templateId ? { _templateId: { $in: [templateId].flat() } } : {};
const typeMatch = delay
? {
type: {
Expand All @@ -75,16 +73,18 @@ export class JobsService {
: {};

do {
totalCount = (await this.getQueueMetric()).totalCount;
runningJobs = await this.jobRepository.count({
_organizationId: organizationId,
...typeMatch,
...workflowMatch,
status: {
$in: [JobStatusEnum.PENDING, JobStatusEnum.QUEUED, JobStatusEnum.RUNNING],
},
});
} while (totalCount > 0 || runningJobs > unfinishedJobs);
runningJobs = Math.max(
await this.jobRepository.count({
_organizationId: organizationId,
...typeMatch,
...workflowMatch,
status: {
$in: [JobStatusEnum.PENDING, JobStatusEnum.QUEUED, JobStatusEnum.RUNNING],
},
}),
0
);
} while (runningJobs > unfinishedJobs);

return {
getDelayedTimestamp: async () => {
Expand Down

0 comments on commit 0f6370d

Please sign in to comment.