From 6aee42ab22d1877bf121125aaa161db6f03310a0 Mon Sep 17 00:00:00 2001 From: nitin <142569587+ehconitin@users.noreply.github.com> Date: Tue, 24 Jun 2025 00:35:01 +0530 Subject: [PATCH] register all cron jobs in entrypoint (#12791) closes https://github.com/twentyhq/core-team-issues/issues/1113 Three things I am not sure about - - Should we have a variable just like DISABLE_DB_MIGRATIONS to control cron job registration behavior, i.e., DISABLE_CRON_JOBS_REGISTRATION? - The location of the command ie, folder placement - https://github.com/twentyhq/twenty/pull/12791/files#r2161734131 --- .dockerignore | 1 + packages/twenty-docker/docker-compose.yml | 3 + packages/twenty-docker/twenty/entrypoint.sh | 16 ++++ .../commands/cron-register-all.command.ts | 94 +++++++++++++++++++ .../commands/database-command.module.ts | 15 ++- .../calendar-event-import-manager.module.ts | 8 +- .../messaging-import-manager.module.ts | 7 +- .../automated-trigger.module.ts | 2 +- 8 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 packages/twenty-server/src/database/commands/cron-register-all.command.ts diff --git a/.dockerignore b/.dockerignore index f9fc785f9..93efb8a60 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,4 @@ .env node_modules .nx/cache +packages/twenty-server/.env diff --git a/packages/twenty-docker/docker-compose.yml b/packages/twenty-docker/docker-compose.yml index 617ce3c67..e95fde62f 100644 --- a/packages/twenty-docker/docker-compose.yml +++ b/packages/twenty-docker/docker-compose.yml @@ -12,6 +12,8 @@ services: PG_DATABASE_URL: postgres://${PG_DATABASE_USER:-postgres}:${PG_DATABASE_PASSWORD:-postgres}@${PG_DATABASE_HOST:-db}:${PG_DATABASE_PORT:-5432}/default SERVER_URL: ${SERVER_URL} REDIS_URL: ${REDIS_URL:-redis://redis:6379} + DISABLE_DB_MIGRATIONS: ${DISABLE_DB_MIGRATIONS} + DISABLE_CRON_JOBS_REGISTRATION: ${DISABLE_CRON_JOBS_REGISTRATION} STORAGE_TYPE: ${STORAGE_TYPE} STORAGE_S3_REGION: ${STORAGE_S3_REGION} @@ -63,6 +65,7 @@ services: SERVER_URL: ${SERVER_URL} REDIS_URL: ${REDIS_URL:-redis://redis:6379} DISABLE_DB_MIGRATIONS: "true" # it already runs on the server + DISABLE_CRON_JOBS_REGISTRATION: "true" # it already runs on the server STORAGE_TYPE: ${STORAGE_TYPE} STORAGE_S3_REGION: ${STORAGE_S3_REGION} diff --git a/packages/twenty-docker/twenty/entrypoint.sh b/packages/twenty-docker/twenty/entrypoint.sh index 5b523fe77..51c2ebac8 100755 --- a/packages/twenty-docker/twenty/entrypoint.sh +++ b/packages/twenty-docker/twenty/entrypoint.sh @@ -28,7 +28,23 @@ setup_and_migrate_db() { yarn command:prod upgrade echo "Successfully migrated DB!" } + +register_background_jobs() { + if [ "${DISABLE_CRON_JOBS_REGISTRATION}" = "true" ]; then + echo "Cron job registration is disabled, skipping..." + return + fi + + echo "Registering background sync jobs..." + if yarn command:prod cron:register:all; then + echo "Successfully registered all background sync jobs!" + else + echo "Warning: Failed to register background jobs, but continuing startup..." + fi +} + setup_and_migrate_db +register_background_jobs # Continue with the original Docker command exec "$@" diff --git a/packages/twenty-server/src/database/commands/cron-register-all.command.ts b/packages/twenty-server/src/database/commands/cron-register-all.command.ts new file mode 100644 index 000000000..c0975f7bf --- /dev/null +++ b/packages/twenty-server/src/database/commands/cron-register-all.command.ts @@ -0,0 +1,94 @@ +import { Logger } from '@nestjs/common'; + +import { Command, CommandRunner } from 'nest-commander'; + +import { CalendarEventListFetchCronCommand } from 'src/modules/calendar/calendar-event-import-manager/crons/commands/calendar-event-list-fetch.cron.command'; +import { CalendarEventsImportCronCommand } from 'src/modules/calendar/calendar-event-import-manager/crons/commands/calendar-import.cron.command'; +import { CalendarOngoingStaleCronCommand } from 'src/modules/calendar/calendar-event-import-manager/crons/commands/calendar-ongoing-stale.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 { MessagingOngoingStaleCronCommand } from 'src/modules/messaging/message-import-manager/crons/commands/messaging-ongoing-stale.cron.command'; +import { CronTriggerCronCommand } from 'src/modules/workflow/workflow-trigger/automated-trigger/crons/commands/cron-trigger.cron.command'; + +@Command({ + name: 'cron:register:all', + description: 'Register all background sync cron jobs', +}) +export class CronRegisterAllCommand extends CommandRunner { + private readonly logger = new Logger(CronRegisterAllCommand.name); + + constructor( + private readonly messagingMessagesImportCronCommand: MessagingMessagesImportCronCommand, + private readonly messagingMessageListFetchCronCommand: MessagingMessageListFetchCronCommand, + private readonly messagingOngoingStaleCronCommand: MessagingOngoingStaleCronCommand, + private readonly calendarEventListFetchCronCommand: CalendarEventListFetchCronCommand, + private readonly calendarEventsImportCronCommand: CalendarEventsImportCronCommand, + private readonly calendarOngoingStaleCronCommand: CalendarOngoingStaleCronCommand, + private readonly cronTriggerCronCommand: CronTriggerCronCommand, + ) { + super(); + } + + async run(): Promise { + this.logger.log('Registering all background sync cron jobs...'); + + const commands = [ + { + name: 'MessagingMessagesImport', + command: this.messagingMessagesImportCronCommand, + }, + { + name: 'MessagingMessageListFetch', + command: this.messagingMessageListFetchCronCommand, + }, + { + name: 'MessagingOngoingStale', + command: this.messagingOngoingStaleCronCommand, + }, + { + name: 'CalendarEventListFetch', + command: this.calendarEventListFetchCronCommand, + }, + { + name: 'CalendarEventsImport', + command: this.calendarEventsImportCronCommand, + }, + { + name: 'CalendarOngoingStale', + command: this.calendarOngoingStaleCronCommand, + }, + { name: 'CronTrigger', command: this.cronTriggerCronCommand }, + ]; + + let successCount = 0; + let failureCount = 0; + const failures: string[] = []; + const successes: string[] = []; + + for (const { name, command } of commands) { + try { + this.logger.log(`Registering ${name} cron job...`); + await command.run(); + this.logger.log(`Successfully registered ${name} cron job`); + successCount++; + successes.push(name); + } catch (error) { + this.logger.error(`Failed to register ${name} cron job:`, error); + failureCount++; + failures.push(name); + } + } + + this.logger.log( + `Cron job registration completed: ${successCount} successful, ${failureCount} failed`, + ); + + if (failures.length > 0) { + this.logger.warn(`Failed commands: ${failures.join(', ')}`); + } + + if (successCount > 0) { + this.logger.log(`Successful commands: ${successes.join(', ')}`); + } + } +} diff --git a/packages/twenty-server/src/database/commands/database-command.module.ts b/packages/twenty-server/src/database/commands/database-command.module.ts index 38c1c23a1..34576bf23 100644 --- a/packages/twenty-server/src/database/commands/database-command.module.ts +++ b/packages/twenty-server/src/database/commands/database-command.module.ts @@ -1,5 +1,6 @@ import { Module } from '@nestjs/common'; +import { CronRegisterAllCommand } from 'src/database/commands/cron-register-all.command'; import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command'; import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question'; import { UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/upgrade-version-command.module'; @@ -10,11 +11,19 @@ import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadat import { WorkspaceCacheStorageModule } from 'src/engine/workspace-cache-storage/workspace-cache-storage.module'; import { DevSeederModule } from 'src/engine/workspace-manager/dev-seeder/dev-seeder.module'; import { WorkspaceManagerModule } from 'src/engine/workspace-manager/workspace-manager.module'; +import { CalendarEventImportManagerModule } from 'src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module'; +import { MessagingImportManagerModule } from 'src/modules/messaging/message-import-manager/messaging-import-manager.module'; +import { AutomatedTriggerModule } from 'src/modules/workflow/workflow-trigger/automated-trigger/automated-trigger.module'; @Module({ imports: [ UpgradeVersionCommandModule, + // Cron command dependencies + MessagingImportManagerModule, + CalendarEventImportManagerModule, + AutomatedTriggerModule, + // Only needed for the data seed command TypeORMModule, FieldMetadataModule, @@ -24,6 +33,10 @@ import { WorkspaceManagerModule } from 'src/engine/workspace-manager/workspace-m DataSourceModule, WorkspaceCacheStorageModule, ], - providers: [DataSeedWorkspaceCommand, ConfirmationQuestion], + providers: [ + DataSeedWorkspaceCommand, + ConfirmationQuestion, + CronRegisterAllCommand, + ], }) export class DatabaseCommandModule {} diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module.ts index 37c618fbb..7f5ba22da 100644 --- a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module.ts +++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module.ts @@ -67,6 +67,12 @@ import { RefreshTokensManagerModule } from 'src/modules/connected-account/refres CalendarOngoingStaleCronCommand, CalendarOngoingStaleJob, ], - exports: [CalendarEventsImportService, CalendarFetchEventsService], + exports: [ + CalendarEventsImportService, + CalendarFetchEventsService, + CalendarEventListFetchCronCommand, + CalendarEventsImportCronCommand, + CalendarOngoingStaleCronCommand, + ], }) export class CalendarEventImportManagerModule {} diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/messaging-import-manager.module.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/messaging-import-manager.module.ts index e227af8a0..a516efd29 100644 --- a/packages/twenty-server/src/modules/messaging/message-import-manager/messaging-import-manager.module.ts +++ b/packages/twenty-server/src/modules/messaging/message-import-manager/messaging-import-manager.module.ts @@ -83,6 +83,11 @@ import { MessagingMonitoringModule } from 'src/modules/messaging/monitoring/mess MessagingCursorService, MessagingSendMessageService, ], - exports: [MessagingSendMessageService], + exports: [ + MessagingSendMessageService, + MessagingMessageListFetchCronCommand, + MessagingMessagesImportCronCommand, + MessagingOngoingStaleCronCommand, + ], }) export class MessagingImportManagerModule {} diff --git a/packages/twenty-server/src/modules/workflow/workflow-trigger/automated-trigger/automated-trigger.module.ts b/packages/twenty-server/src/modules/workflow/workflow-trigger/automated-trigger/automated-trigger.module.ts index bfb0d2ca8..fa2b83cb4 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-trigger/automated-trigger/automated-trigger.module.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-trigger/automated-trigger/automated-trigger.module.ts @@ -19,6 +19,6 @@ import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trig CronTriggerCronJob, CronTriggerCronCommand, ], - exports: [AutomatedTriggerWorkspaceService], + exports: [AutomatedTriggerWorkspaceService, CronTriggerCronCommand], }) export class AutomatedTriggerModule {}