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
This commit is contained in:
nitin
2025-06-24 00:35:01 +05:30
committed by GitHub
parent 00eb93463c
commit 6aee42ab22
8 changed files with 142 additions and 4 deletions

View File

@ -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}

View File

@ -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 "$@"

View File

@ -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<void> {
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(', ')}`);
}
}
}

View File

@ -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 {}

View File

@ -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 {}

View File

@ -83,6 +83,11 @@ import { MessagingMonitoringModule } from 'src/modules/messaging/monitoring/mess
MessagingCursorService,
MessagingSendMessageService,
],
exports: [MessagingSendMessageService],
exports: [
MessagingSendMessageService,
MessagingMessageListFetchCronCommand,
MessagingMessagesImportCronCommand,
MessagingOngoingStaleCronCommand,
],
})
export class MessagingImportManagerModule {}

View File

@ -19,6 +19,6 @@ import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trig
CronTriggerCronJob,
CronTriggerCronCommand,
],
exports: [AutomatedTriggerWorkspaceService],
exports: [AutomatedTriggerWorkspaceService, CronTriggerCronCommand],
})
export class AutomatedTriggerModule {}