Fix staled run cron (#14044)

- Dequeue workflows when enqueuedAt is Null
- Rename cron cleaner + add it to the registered crons
This commit is contained in:
Thomas Trompette
2025-08-22 14:55:43 +02:00
committed by GitHub
parent 4ecd275f5e
commit 6c5a265a4e
5 changed files with 24 additions and 14 deletions
@@ -10,6 +10,7 @@ import { CalendarOngoingStaleCronCommand } from 'src/modules/calendar/calendar-e
import { MessagingMessageListFetchCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-message-list-fetch.cron.command'; import { MessagingMessageListFetchCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-message-list-fetch.cron.command';
import { MessagingMessagesImportCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-messages-import.cron.command'; import { MessagingMessagesImportCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-messages-import.cron.command';
import { MessagingOngoingStaleCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-ongoing-stale.cron.command'; import { MessagingOngoingStaleCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-ongoing-stale.cron.command';
import { WorkflowCleanWorkflowRunsCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-clean-workflow-runs.cron.command';
import { WorkflowHandleStaledRunsCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-handle-staled-runs.cron.command'; import { WorkflowHandleStaledRunsCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-handle-staled-runs.cron.command';
import { WorkflowRunEnqueueCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-run-enqueue.cron.command'; import { WorkflowRunEnqueueCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-run-enqueue.cron.command';
import { WorkflowCronTriggerCronCommand } from 'src/modules/workflow/workflow-trigger/automated-trigger/crons/commands/workflow-cron-trigger.cron.command'; import { WorkflowCronTriggerCronCommand } from 'src/modules/workflow/workflow-trigger/automated-trigger/crons/commands/workflow-cron-trigger.cron.command';
@@ -33,6 +34,7 @@ export class CronRegisterAllCommand extends CommandRunner {
private readonly checkCustomDomainValidRecordsCronCommand: CheckCustomDomainValidRecordsCronCommand, private readonly checkCustomDomainValidRecordsCronCommand: CheckCustomDomainValidRecordsCronCommand,
private readonly workflowRunEnqueueCronCommand: WorkflowRunEnqueueCronCommand, private readonly workflowRunEnqueueCronCommand: WorkflowRunEnqueueCronCommand,
private readonly workflowHandleStaledRunsCronCommand: WorkflowHandleStaledRunsCronCommand, private readonly workflowHandleStaledRunsCronCommand: WorkflowHandleStaledRunsCronCommand,
private readonly workflowCleanWorkflowRunsCronCommand: WorkflowCleanWorkflowRunsCommand,
) { ) {
super(); super();
} }
@@ -85,6 +87,10 @@ export class CronRegisterAllCommand extends CommandRunner {
name: 'WorkflowHandleStaledRuns', name: 'WorkflowHandleStaledRuns',
command: this.workflowHandleStaledRunsCronCommand, command: this.workflowHandleStaledRunsCronCommand,
}, },
{
name: 'WorkflowCleanWorkflowRuns',
command: this.workflowCleanWorkflowRunsCronCommand,
},
]; ];
let successCount = 0; let successCount = 0;
@@ -5,14 +5,14 @@ import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queu
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service'; import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { import {
CLEAN_WORKFLOW_RUN_CRON_PATTERN, CLEAN_WORKFLOW_RUN_CRON_PATTERN,
CleanWorkflowRunsJob, WorkflowCleanWorkflowRunsJob,
} from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/clean-workflow-runs.cron.job'; } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-clean-workflow-runs.cron.job';
@Command({ @Command({
name: 'cron:workflow:clean-workflow-runs', name: 'cron:workflow:clean-workflow-runs',
description: 'Clean workflow runs', description: 'Clean workflow runs',
}) })
export class CronCleanWorkflowRunsCommand extends CommandRunner { export class WorkflowCleanWorkflowRunsCommand extends CommandRunner {
constructor( constructor(
@InjectMessageQueue(MessageQueue.cronQueue) @InjectMessageQueue(MessageQueue.cronQueue)
private readonly messageQueueService: MessageQueueService, private readonly messageQueueService: MessageQueueService,
@@ -22,7 +22,7 @@ export class CronCleanWorkflowRunsCommand extends CommandRunner {
async run(): Promise<void> { async run(): Promise<void> {
await this.messageQueueService.addCron({ await this.messageQueueService.addCron({
jobName: CleanWorkflowRunsJob.name, jobName: WorkflowCleanWorkflowRunsJob.name,
data: undefined, data: undefined,
options: { options: {
repeat: { repeat: {
@@ -22,8 +22,8 @@ export const CLEAN_WORKFLOW_RUN_CRON_PATTERN = '0 0 * * *';
const NUMBER_OF_WORKFLOW_RUNS_TO_KEEP = 1000; const NUMBER_OF_WORKFLOW_RUNS_TO_KEEP = 1000;
@Processor(MessageQueue.cronQueue) @Processor(MessageQueue.cronQueue)
export class CleanWorkflowRunsJob { export class WorkflowCleanWorkflowRunsJob {
private readonly logger = new Logger(CleanWorkflowRunsJob.name); private readonly logger = new Logger(WorkflowCleanWorkflowRunsJob.name);
constructor( constructor(
@InjectRepository(Workspace, 'core') @InjectRepository(Workspace, 'core')
@@ -32,8 +32,11 @@ export class CleanWorkflowRunsJob {
private readonly twentyORMGlobalManager: TwentyORMGlobalManager, private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {} ) {}
@Process(CleanWorkflowRunsJob.name) @Process(WorkflowCleanWorkflowRunsJob.name)
@SentryCronMonitor(CleanWorkflowRunsJob.name, CLEAN_WORKFLOW_RUN_CRON_PATTERN) @SentryCronMonitor(
WorkflowCleanWorkflowRunsJob.name,
CLEAN_WORKFLOW_RUN_CRON_PATTERN,
)
async handle() { async handle() {
const activeWorkspaces = await this.workspaceRepository.find({ const activeWorkspaces = await this.workspaceRepository.find({
where: { where: {
@@ -1,4 +1,4 @@
import { LessThan } from 'typeorm'; import { IsNull, LessThan, Or } from 'typeorm';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator'; import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator'; import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
@@ -37,7 +37,7 @@ export class WorkflowHandleStaledRunsPerWorkspaceJob {
const staledWorkflowRuns = await workflowRunRepository.find({ const staledWorkflowRuns = await workflowRunRepository.find({
where: { where: {
status: WorkflowRunStatus.ENQUEUED, status: WorkflowRunStatus.ENQUEUED,
enqueuedAt: LessThan(oneHourAgo), enqueuedAt: Or(LessThan(oneHourAgo), IsNull()),
}, },
}); });
@@ -6,10 +6,10 @@ import { MessageQueueModule } from 'src/engine/core-modules/message-queue/messag
import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module'; import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { CronCleanWorkflowRunsCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/cron-clean-workflow-runs.cron.command'; import { WorkflowCleanWorkflowRunsCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-clean-workflow-runs.cron.command';
import { WorkflowHandleStaledRunsCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-handle-staled-runs.cron.command'; import { WorkflowHandleStaledRunsCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-handle-staled-runs.cron.command';
import { WorkflowRunEnqueueCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-run-enqueue.cron.command'; import { WorkflowRunEnqueueCronCommand } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/command/workflow-run-enqueue.cron.command';
import { CleanWorkflowRunsJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/clean-workflow-runs.cron.job'; import { WorkflowCleanWorkflowRunsJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-clean-workflow-runs.cron.job';
import { WorkflowHandleStaledRunsPerWorkspaceJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-handle-staled-runs-per-workspace.job'; import { WorkflowHandleStaledRunsPerWorkspaceJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-handle-staled-runs-per-workspace.job';
import { WorkflowHandleStaledRunsJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-handle-staled-runs.job'; import { WorkflowHandleStaledRunsJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-handle-staled-runs.job';
import { WorkflowRunEnqueuePerWorkspaceJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-run-enqueue-per-workspace.job'; import { WorkflowRunEnqueuePerWorkspaceJob } from 'src/modules/workflow/workflow-runner/workflow-run-queue/cron/jobs/workflow-run-enqueue-per-workspace.job';
@@ -32,13 +32,14 @@ import { WorkflowRunQueueWorkspaceService } from 'src/modules/workflow/workflow-
WorkflowHandleStaledRunsCronCommand, WorkflowHandleStaledRunsCronCommand,
WorkflowHandleStaledRunsJob, WorkflowHandleStaledRunsJob,
WorkflowHandleStaledRunsPerWorkspaceJob, WorkflowHandleStaledRunsPerWorkspaceJob,
CleanWorkflowRunsJob, WorkflowCleanWorkflowRunsJob,
CronCleanWorkflowRunsCommand, WorkflowCleanWorkflowRunsCommand,
], ],
exports: [ exports: [
WorkflowRunQueueWorkspaceService, WorkflowRunQueueWorkspaceService,
WorkflowRunEnqueueCronCommand, WorkflowRunEnqueueCronCommand,
WorkflowHandleStaledRunsCronCommand, WorkflowHandleStaledRunsCronCommand,
WorkflowCleanWorkflowRunsCommand,
], ],
}) })
export class WorkflowRunQueueModule {} export class WorkflowRunQueueModule {}