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:
@ -2,3 +2,4 @@
|
|||||||
.env
|
.env
|
||||||
node_modules
|
node_modules
|
||||||
.nx/cache
|
.nx/cache
|
||||||
|
packages/twenty-server/.env
|
||||||
|
|||||||
@ -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
|
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}
|
SERVER_URL: ${SERVER_URL}
|
||||||
REDIS_URL: ${REDIS_URL:-redis://redis:6379}
|
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_TYPE: ${STORAGE_TYPE}
|
||||||
STORAGE_S3_REGION: ${STORAGE_S3_REGION}
|
STORAGE_S3_REGION: ${STORAGE_S3_REGION}
|
||||||
@ -63,6 +65,7 @@ services:
|
|||||||
SERVER_URL: ${SERVER_URL}
|
SERVER_URL: ${SERVER_URL}
|
||||||
REDIS_URL: ${REDIS_URL:-redis://redis:6379}
|
REDIS_URL: ${REDIS_URL:-redis://redis:6379}
|
||||||
DISABLE_DB_MIGRATIONS: "true" # it already runs on the server
|
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_TYPE: ${STORAGE_TYPE}
|
||||||
STORAGE_S3_REGION: ${STORAGE_S3_REGION}
|
STORAGE_S3_REGION: ${STORAGE_S3_REGION}
|
||||||
|
|||||||
@ -28,7 +28,23 @@ setup_and_migrate_db() {
|
|||||||
yarn command:prod upgrade
|
yarn command:prod upgrade
|
||||||
echo "Successfully migrated DB!"
|
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
|
setup_and_migrate_db
|
||||||
|
register_background_jobs
|
||||||
|
|
||||||
# Continue with the original Docker command
|
# Continue with the original Docker command
|
||||||
exec "$@"
|
exec "$@"
|
||||||
|
|||||||
@ -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(', ')}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
import { Module } from '@nestjs/common';
|
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 { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command';
|
||||||
import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';
|
import { ConfirmationQuestion } from 'src/database/commands/questions/confirmation.question';
|
||||||
import { UpgradeVersionCommandModule } from 'src/database/commands/upgrade-version-command/upgrade-version-command.module';
|
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 { WorkspaceCacheStorageModule } from 'src/engine/workspace-cache-storage/workspace-cache-storage.module';
|
||||||
import { DevSeederModule } from 'src/engine/workspace-manager/dev-seeder/dev-seeder.module';
|
import { DevSeederModule } from 'src/engine/workspace-manager/dev-seeder/dev-seeder.module';
|
||||||
import { WorkspaceManagerModule } from 'src/engine/workspace-manager/workspace-manager.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({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
UpgradeVersionCommandModule,
|
UpgradeVersionCommandModule,
|
||||||
|
|
||||||
|
// Cron command dependencies
|
||||||
|
MessagingImportManagerModule,
|
||||||
|
CalendarEventImportManagerModule,
|
||||||
|
AutomatedTriggerModule,
|
||||||
|
|
||||||
// Only needed for the data seed command
|
// Only needed for the data seed command
|
||||||
TypeORMModule,
|
TypeORMModule,
|
||||||
FieldMetadataModule,
|
FieldMetadataModule,
|
||||||
@ -24,6 +33,10 @@ import { WorkspaceManagerModule } from 'src/engine/workspace-manager/workspace-m
|
|||||||
DataSourceModule,
|
DataSourceModule,
|
||||||
WorkspaceCacheStorageModule,
|
WorkspaceCacheStorageModule,
|
||||||
],
|
],
|
||||||
providers: [DataSeedWorkspaceCommand, ConfirmationQuestion],
|
providers: [
|
||||||
|
DataSeedWorkspaceCommand,
|
||||||
|
ConfirmationQuestion,
|
||||||
|
CronRegisterAllCommand,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class DatabaseCommandModule {}
|
export class DatabaseCommandModule {}
|
||||||
|
|||||||
@ -67,6 +67,12 @@ import { RefreshTokensManagerModule } from 'src/modules/connected-account/refres
|
|||||||
CalendarOngoingStaleCronCommand,
|
CalendarOngoingStaleCronCommand,
|
||||||
CalendarOngoingStaleJob,
|
CalendarOngoingStaleJob,
|
||||||
],
|
],
|
||||||
exports: [CalendarEventsImportService, CalendarFetchEventsService],
|
exports: [
|
||||||
|
CalendarEventsImportService,
|
||||||
|
CalendarFetchEventsService,
|
||||||
|
CalendarEventListFetchCronCommand,
|
||||||
|
CalendarEventsImportCronCommand,
|
||||||
|
CalendarOngoingStaleCronCommand,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class CalendarEventImportManagerModule {}
|
export class CalendarEventImportManagerModule {}
|
||||||
|
|||||||
@ -83,6 +83,11 @@ import { MessagingMonitoringModule } from 'src/modules/messaging/monitoring/mess
|
|||||||
MessagingCursorService,
|
MessagingCursorService,
|
||||||
MessagingSendMessageService,
|
MessagingSendMessageService,
|
||||||
],
|
],
|
||||||
exports: [MessagingSendMessageService],
|
exports: [
|
||||||
|
MessagingSendMessageService,
|
||||||
|
MessagingMessageListFetchCronCommand,
|
||||||
|
MessagingMessagesImportCronCommand,
|
||||||
|
MessagingOngoingStaleCronCommand,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class MessagingImportManagerModule {}
|
export class MessagingImportManagerModule {}
|
||||||
|
|||||||
@ -19,6 +19,6 @@ import { DatabaseEventTriggerListener } from 'src/modules/workflow/workflow-trig
|
|||||||
CronTriggerCronJob,
|
CronTriggerCronJob,
|
||||||
CronTriggerCronCommand,
|
CronTriggerCronCommand,
|
||||||
],
|
],
|
||||||
exports: [AutomatedTriggerWorkspaceService],
|
exports: [AutomatedTriggerWorkspaceService, CronTriggerCronCommand],
|
||||||
})
|
})
|
||||||
export class AutomatedTriggerModule {}
|
export class AutomatedTriggerModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user