866 refactor cron trigger only one cron each minutes triggers all cron triggers (#11809)

<img width="1123" alt="image"
src="https://github.com/user-attachments/assets/75447922-81dd-4cfc-805d-f511f73cc778"
/>
This commit is contained in:
martmull
2025-04-30 17:08:47 +02:00
committed by GitHub
parent 357649db95
commit 849a35955a
25 changed files with 595 additions and 116 deletions

View File

@ -0,0 +1,72 @@
import { InjectRepository } from '@nestjs/typeorm';
import { Command } from 'nest-commander';
import { Repository } from 'typeorm';
import {
ActiveOrSuspendedWorkspacesMigrationCommandRunner,
RunOnWorkspaceArgs,
} from 'src/database/commands/command-runners/active-or-suspended-workspaces-migration.command-runner';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import {
AutomatedTriggerType,
WorkflowAutomatedTriggerWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-automated-trigger.workspace-entity';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
@Command({
name: 'upgrade:0-53:migrate-workflow-event-listeners-to-automated-triggers',
description: 'Migrate workflow event listeners to automated triggers',
})
export class MigrateWorkflowEventListenersToAutomatedTriggersCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository, twentyORMGlobalManager);
}
override async runOnWorkspace({
index,
total,
workspaceId,
}: RunOnWorkspaceArgs): Promise<void> {
this.logger.log(
`Running command for workspace ${workspaceId} ${index + 1}/${total}`,
);
const workflowEventListenerRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowEventListenerWorkspaceEntity>(
workspaceId,
'workflowEventListener',
);
const workflowAutomatedTriggerRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowAutomatedTriggerWorkspaceEntity>(
workspaceId,
'workflowAutomatedTrigger',
);
const workflowEventListeners = await workflowEventListenerRepository.find();
await workflowAutomatedTriggerRepository.delete({
type: AutomatedTriggerType.DATABASE_EVENT,
});
for (const eventListener of workflowEventListeners) {
const { eventName, ...rest } = eventListener;
await workflowAutomatedTriggerRepository.save({
...rest,
type: AutomatedTriggerType.DATABASE_EVENT,
settings: { eventName },
});
}
this.logger.log(
`Migrated ${workflowEventListeners.length} workflow event listeners to automated triggers`,
);
}
}

View File

@ -0,0 +1,16 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { MigrateWorkflowEventListenersToAutomatedTriggersCommand } from 'src/database/commands/upgrade-version-command/0-53/0-53-migrate-workflow-event-listeners-to-automated-triggers.command';
@Module({
imports: [
TypeOrmModule.forFeature([Workspace], 'core'),
WorkspaceDataSourceModule,
],
providers: [MigrateWorkflowEventListenersToAutomatedTriggersCommand],
exports: [MigrateWorkflowEventListenersToAutomatedTriggersCommand],
})
export class V0_53_UpgradeVersionCommandModule {}

View File

@ -6,6 +6,7 @@ import { V0_44_UpgradeVersionCommandModule } from 'src/database/commands/upgrade
import { V0_50_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-50/0-50-upgrade-version-command.module';
import { V0_51_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-51/0-51-upgrade-version-command.module';
import { V0_52_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-52/0-52-upgrade-version-command.module';
import { V0_53_UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/0-53/0-53-upgrade-version-command.module';
import { UpgradeCommand } from 'src/database/commands/upgrade-version-command/upgrade.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.module';
@ -18,6 +19,7 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
V0_50_UpgradeVersionCommandModule,
V0_51_UpgradeVersionCommandModule,
V0_52_UpgradeVersionCommandModule,
V0_53_UpgradeVersionCommandModule,
WorkspaceSyncMetadataModule,
],
providers: [UpgradeCommand],

View File

@ -23,6 +23,7 @@ import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twent
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
import { MigrateWorkflowEventListenersToAutomatedTriggersCommand } from 'src/database/commands/upgrade-version-command/0-53/0-53-migrate-workflow-event-listeners-to-automated-triggers.command';
type VersionCommands = {
beforeSyncMetadata: ActiveOrSuspendedWorkspacesMigrationCommandRunner[];
@ -60,6 +61,9 @@ export class UpgradeCommand extends UpgradeCommandRunner {
// 0.52 Commands
protected readonly upgradeDateAndDateTimeFieldsSettingsJsonCommand: UpgradeDateAndDateTimeFieldsSettingsJsonCommand,
protected readonly migrateRelationsToFieldMetadataCommand: MigrateRelationsToFieldMetadataCommand,
// 0.53 Commands
protected readonly migrateWorkflowEventListenersToAutomatedTriggersCommand: MigrateWorkflowEventListenersToAutomatedTriggersCommand,
) {
super(
workspaceRepository,
@ -93,7 +97,7 @@ export class UpgradeCommand extends UpgradeCommandRunner {
afterSyncMetadata: [],
};
const commands_051: VersionCommands = {
const _commands_051: VersionCommands = {
beforeSyncMetadata: [this.upgradeCreatedByEnumCommand],
afterSyncMetadata: [],
};
@ -106,7 +110,14 @@ export class UpgradeCommand extends UpgradeCommandRunner {
afterSyncMetadata: [],
};
this.commands = commands_051;
const commands_053: VersionCommands = {
beforeSyncMetadata: [],
afterSyncMetadata: [
this.migrateWorkflowEventListenersToAutomatedTriggersCommand,
],
};
this.commands = commands_053;
}
override async runBeforeSyncMetadata(args: RunOnWorkspaceArgs) {