Files
upstream-twenty/packages/twenty-server/test/integration/utils/wait-for-all-jobs-to-finish.util.ts
upstream-mirror e7da97194a
Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 3s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

185 lines
5.0 KiB
TypeScript

import { Queue } from 'bullmq';
import IORedis from 'ioredis';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
const POLL_INTERVAL_MS = 25;
const REQUIRED_CONSECUTIVE_QUIET_CHECKS = 2;
const STALL_TIMEOUT_MS = 15_000;
const DELAYED_JOB_PAGE_SIZE = 50;
const MAX_PROMOTABLE_DELAY_MS = 60_000;
const HARD_TIMEOUT_MS = 120_000;
const PENDING_JOB_STATES = [
'waiting',
'active',
'prioritized',
'waiting-children',
'delayed',
] as const;
type PendingJobCounts = {
pending: number;
delayed: number;
};
let redisConnection: IORedis | null = null;
let queues: Queue[] | null = null;
const getQueues = (): Queue[] => {
if (!queues) {
redisConnection = new IORedis(
process.env.REDIS_QUEUE_URL ??
process.env.REDIS_URL ??
'redis://localhost:6379',
{ maxRetriesPerRequest: null },
);
queues = Object.values(MessageQueue).map(
(queueName) => new Queue(queueName, { connection: redisConnection! }),
);
}
return queues;
};
const getPendingJobCountsByQueue = async (): Promise<
Record<string, PendingJobCounts>
> => {
const countsByQueue = await Promise.all(
getQueues().map(async (queue) => {
const jobCounts = await queue.getJobCounts(...PENDING_JOB_STATES);
const pendingCount = Object.values(jobCounts).reduce(
(sum, count) => sum + count,
0,
);
return [
queue.name,
{ pending: pendingCount, delayed: jobCounts.delayed ?? 0 },
] as const;
}),
);
return Object.fromEntries(
countsByQueue.filter(([, { pending }]) => pending > 0),
);
};
const promoteDelayedJobs = async (queueName: string): Promise<number> => {
const queue = getQueues().find(({ name }) => name === queueName);
if (!queue) {
return 0;
}
const delayedJobs = await queue.getDelayed(0, DELAYED_JOB_PAGE_SIZE);
const promotableJobs = delayedJobs.filter(
(job) => (job.delay ?? 0) <= MAX_PROMOTABLE_DELAY_MS,
);
let promotedJobCount = 0;
for (const promotableJob of promotableJobs) {
const hasBeenPromoted = await promotableJob
.promote()
.then(() => true)
.catch(() => false);
if (hasBeenPromoted) {
promotedJobCount += 1;
}
}
return promotedJobCount;
};
const getActiveJobsFingerprint = async (
busyQueueNames: string[],
): Promise<string> => {
const activeJobIdsByQueue = await Promise.all(
getQueues()
.filter((queue) => busyQueueNames.includes(queue.name))
.map(async (queue) => {
const activeJobs = await queue.getActive(0, 50);
return `${queue.name}:${activeJobs.map((job) => job.id).join(',')}`;
}),
);
return activeJobIdsByQueue.join('|');
};
export const waitForAllJobsToFinish = async (): Promise<void> => {
const startedAt = Date.now();
let lastProgressAt = startedAt;
let lastBusyFingerprint = '';
let consecutiveQuietChecks = 0;
while (consecutiveQuietChecks < REQUIRED_CONSECUTIVE_QUIET_CHECKS) {
const pendingJobCountsByQueue = await getPendingJobCountsByQueue();
const pendingTotal = Object.values(pendingJobCountsByQueue).reduce(
(sum, { pending }) => sum + pending,
0,
);
const stalledQueueNames = Object.entries(pendingJobCountsByQueue)
.filter(([, { pending, delayed }]) => pending === delayed)
.map(([queueName]) => queueName);
if (pendingTotal === 0) {
consecutiveQuietChecks += 1;
} else {
consecutiveQuietChecks = 0;
const now = Date.now();
if (now - startedAt > HARD_TIMEOUT_MS) {
throw new Error(
`Message queues still busy after ${HARD_TIMEOUT_MS}ms, pending jobs: ${JSON.stringify(pendingJobCountsByQueue)}`,
);
}
let promotedJobCount = 0;
for (const stalledQueueName of stalledQueueNames) {
promotedJobCount += await promoteDelayedJobs(stalledQueueName);
}
if (promotedJobCount > 0) {
lastProgressAt = now;
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
continue;
}
const activeJobsFingerprint = await getActiveJobsFingerprint(
Object.keys(pendingJobCountsByQueue),
);
const busyFingerprint = `${pendingTotal}|${activeJobsFingerprint}`;
if (busyFingerprint !== lastBusyFingerprint) {
lastBusyFingerprint = busyFingerprint;
lastProgressAt = now;
}
if (now - lastProgressAt > STALL_TIMEOUT_MS) {
throw new Error(
`Message queues stalled, no progress for ${STALL_TIMEOUT_MS}ms, pending jobs: ${JSON.stringify(pendingJobCountsByQueue)}`,
);
}
}
if (consecutiveQuietChecks < REQUIRED_CONSECUTIVE_QUIET_CHECKS) {
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
}
}
};
export const closeQueueConnections = async (): Promise<void> => {
if (queues) {
await Promise.allSettled(queues.map((queue) => queue.close()));
queues = null;
}
if (redisConnection) {
await redisConnection.quit().catch(() => redisConnection?.disconnect());
redisConnection = null;
}
};