From f4fda221b720e209163971ada199a63ea8b275f1 Mon Sep 17 00:00:00 2001 From: Weiko Date: Fri, 12 Apr 2024 14:43:03 +0200 Subject: [PATCH] Fix cron module structure (#4933) This PR introduces a new folder structure for business modules. Cron commands and jobs are now stored within the same module/folder at the root of the business module e.g: /modules/messaging/crons/commands instead of /modules/messaging/commands/crons Patterns are now inside their own cron-command files since they don't need to be exported Ideally cronJobs and cronCommands should have their logic within the same class but it's a bit harder than expected due to how commanderjs and our worker need both some class heritage check, hence the first approach is to move them in the same folder Also Messaging fullsync/partialsync V2 has been dropped since this is the only used version => Breaking change for ongoing jobs and crons. Jobs can be dropped but we will need to re-run our crons (only cron:messaging:gmail-fetch-messages-from-cache) --- .../src/command/command.module.ts | 10 +- .../auth/services/google-apis.service.ts | 44 ++++---- .../integrations/message-queue/jobs.module.ts | 100 +++++++++--------- ....module.ts => calendar-commands.module.ts} | 13 ++- .../commands/calendar-cron-commands.module.ts | 11 ++ .../google-calendar-sync.cron.command.ts} | 9 +- .../jobs}/google-calendar-sync.cron.job.ts | 0 .../google-calendar-sync.cron.pattern.ts | 1 - ...-fetch-messages-from-cache.cron.command.ts | 4 +- .../gmail-partial-sync.cron.command.ts | 7 +- .../messaging-cron-commands.module.ts} | 6 +- ...ail-fetch-messages-from-cache.cron.job.ts} | 2 +- .../jobs}/gmail-partial-sync.cron.job.ts | 10 +- ...ch-all-workspaces-messages.cron.pattern.ts | 1 - ...-sync-v2.job.ts => gmail-full-sync.job.ts} | 10 +- ...nc-v2.job.ts => gmail-partial-sync.job.ts} | 10 +- ...etch-message-content-from-cache.service.ts | 10 +- .../gmail-partial-sync-v2.service.ts | 10 +- 18 files changed, 136 insertions(+), 122 deletions(-) rename packages/twenty-server/src/modules/calendar/commands/{workspace-calendar-sync-commands.module.ts => calendar-commands.module.ts} (55%) create mode 100644 packages/twenty-server/src/modules/calendar/crons/commands/calendar-cron-commands.module.ts rename packages/twenty-server/src/modules/calendar/{commands/start-google-calendar-sync.cron.command.ts => crons/commands/google-calendar-sync.cron.command.ts} (73%) rename packages/twenty-server/src/modules/calendar/{jobs/crons => crons/jobs}/google-calendar-sync.cron.job.ts (100%) delete mode 100644 packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts rename packages/twenty-server/src/modules/messaging/{jobs/crons => crons/commands}/gmail-fetch-messages-from-cache.cron.command.ts (82%) rename packages/twenty-server/src/modules/messaging/{jobs/crons => crons/commands}/gmail-partial-sync.cron.command.ts (72%) rename packages/twenty-server/src/modules/messaging/{commands/messaging-command.module.ts => crons/commands/messaging-cron-commands.module.ts} (78%) rename packages/twenty-server/src/modules/messaging/{jobs/crons/fetch-all-messages-from-cache.cron.job.ts => crons/jobs/gmail-fetch-messages-from-cache.cron.job.ts} (98%) rename packages/twenty-server/src/modules/messaging/{jobs/crons => crons/jobs}/gmail-partial-sync.cron.job.ts (91%) delete mode 100644 packages/twenty-server/src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern.ts rename packages/twenty-server/src/modules/messaging/jobs/{gmail-full-sync-v2.job.ts => gmail-full-sync.job.ts} (84%) rename packages/twenty-server/src/modules/messaging/jobs/{gmail-partial-sync-v2.job.ts => gmail-partial-sync.job.ts} (83%) diff --git a/packages/twenty-server/src/command/command.module.ts b/packages/twenty-server/src/command/command.module.ts index 406a30230..fd7e9fd98 100644 --- a/packages/twenty-server/src/command/command.module.ts +++ b/packages/twenty-server/src/command/command.module.ts @@ -1,21 +1,23 @@ import { Module } from '@nestjs/common'; import { DatabaseCommandModule } from 'src/database/commands/database-command.module'; -import { MessagingCommandModule } from 'src/modules/messaging/commands/messaging-command.module'; import { WorkspaceHealthCommandModule } from 'src/engine/workspace-manager/workspace-health/commands/workspace-health-command.module'; import { WorkspaceCleanerModule } from 'src/engine/workspace-manager/workspace-cleaner/workspace-cleaner.module'; -import { WorkspaceCalendarSyncCommandsModule } from 'src/modules/calendar/commands/workspace-calendar-sync-commands.module'; +import { CalendarCronCommandsModule } from 'src/modules/calendar/crons/commands/calendar-cron-commands.module'; import { AppModule } from 'src/app.module'; import { WorkspaceMigrationRunnerCommandsModule } from 'src/engine/workspace-manager/workspace-migration-runner/commands/workspace-sync-metadata-commands.module'; import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module'; +import { MessagingCronCommandsModule } from 'src/modules/messaging/crons/commands/messaging-cron-commands.module'; +import { CalendarCommandsModule } from 'src/modules/calendar/commands/calendar-commands.module'; @Module({ imports: [ AppModule, WorkspaceSyncMetadataCommandsModule, DatabaseCommandModule, - MessagingCommandModule, - WorkspaceCalendarSyncCommandsModule, + MessagingCronCommandsModule, + CalendarCronCommandsModule, + CalendarCommandsModule, WorkspaceCleanerModule, WorkspaceHealthCommandModule, WorkspaceMigrationRunnerCommandsModule, diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts index b812c861d..9322f6e2f 100644 --- a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts +++ b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts @@ -1,43 +1,43 @@ -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable, Inject } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; +import { Repository, EntityManager } from 'typeorm'; import { v4 } from 'uuid'; -import { EntityManager, Repository } from 'typeorm'; -import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; import { TypeORMService } from 'src/database/typeorm/typeorm.service'; -import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; -import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { - GoogleCalendarSyncJob, - GoogleCalendarSyncJobData, -} from 'src/modules/calendar/jobs/google-calendar-sync.job'; -import { EnvironmentService } from 'src/engine/integrations/environment/environment.service'; import { FeatureFlagEntity, FeatureFlagKeys, } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; -import { - GmailFullSyncV2Job, - GmailFullSyncV2JobData, -} from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; +import { EnvironmentService } from 'src/engine/integrations/environment/environment.service'; +import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; +import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; +import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; +import { + GoogleCalendarSyncJobData, + GoogleCalendarSyncJob, +} from 'src/modules/calendar/jobs/google-calendar-sync.job'; +import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; +import { + CalendarChannelObjectMetadata, + CalendarChannelVisibility, +} from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; +import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { ConnectedAccountObjectMetadata, ConnectedAccountProvider, } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; +import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository'; import { MessageChannelObjectMetadata, MessageChannelType, MessageChannelVisibility, } from 'src/modules/messaging/standard-objects/message-channel.object-metadata'; -import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository'; import { - CalendarChannelObjectMetadata, - CalendarChannelVisibility, -} from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; -import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; + GmailFullSyncJobData, + GmailFullSyncJob, +} from 'src/modules/messaging/jobs/gmail-full-sync.job'; @Injectable() export class GoogleAPIsService { @@ -171,8 +171,8 @@ export class GoogleAPIsService { isCalendarEnabled: boolean, ) { if (this.environmentService.get('MESSAGING_PROVIDER_GMAIL_ENABLED')) { - await this.messageQueueService.add( - GmailFullSyncV2Job.name, + await this.messageQueueService.add( + GmailFullSyncJob.name, { workspaceId, connectedAccountId, diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 2f9c3e4b2..c6105a1fb 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -1,58 +1,58 @@ +import { HttpModule } from '@nestjs/axios'; import { Module } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; -import { HttpModule } from '@nestjs/axios'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { CallWebhookJobsJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/call-webhook-jobs.job'; -import { CallWebhookJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/call-webhook.job'; -import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; -import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module'; -import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; -import { CleanInactiveWorkspaceJob } from 'src/engine/workspace-manager/workspace-cleaner/crons/clean-inactive-workspace.job'; -import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; -import { EmailSenderJob } from 'src/engine/integrations/email/email-sender.job'; -import { UserModule } from 'src/engine/core-modules/user/user.module'; -import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module'; -import { MatchParticipantJob } from 'src/modules/connected-account/jobs/match-participant.job'; -import { GmailPartialSyncCronJob } from 'src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job'; -import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/jobs/messaging-create-company-and-contact-after-sync.job'; -import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module'; import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module'; import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job'; -import { DeleteConnectedAccountAssociatedMessagingDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-messaging-data.job'; -import { ThreadCleanerModule } from 'src/modules/messaging/services/thread-cleaner/thread-cleaner.module'; -import { UpdateSubscriptionJob } from 'src/engine/core-modules/billing/jobs/update-subscription.job'; -import { BillingModule } from 'src/engine/core-modules/billing/billing.module'; -import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module'; -import { StripeModule } from 'src/engine/core-modules/billing/stripe/stripe.module'; -import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; -import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; -import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; -import { GoogleCalendarSyncJob } from 'src/modules/calendar/jobs/google-calendar-sync.job'; -import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module'; +import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; +import { CallWebhookJobsJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/call-webhook-jobs.job'; +import { CallWebhookJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/call-webhook.job'; import { RecordPositionBackfillJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job'; -import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module'; -import { GoogleCalendarSyncModule } from 'src/modules/calendar/services/google-calendar-sync.module'; -import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module'; -import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module'; -import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; -import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata'; import { SaveEventToDbJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/save-event-to-db.job'; -import { CreateCompanyAndContactJob } from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job'; -import { EventObjectMetadata } from 'src/modules/event/standard-objects/event.object-metadata'; +import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module'; +import { BillingModule } from 'src/engine/core-modules/billing/billing.module'; +import { UpdateSubscriptionJob } from 'src/engine/core-modules/billing/jobs/update-subscription.job'; +import { StripeModule } from 'src/engine/core-modules/billing/stripe/stripe.module'; +import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; +import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module'; +import { UserModule } from 'src/engine/core-modules/user/user.module'; import { HandleWorkspaceMemberDeletedJob } from 'src/engine/core-modules/workspace/handle-workspace-member-deleted.job'; -import { GmailFullSynV2Module } from 'src/modules/messaging/services/gmail-full-sync-v2/gmail-full-sync.v2.module'; -import { GmailFetchMessageContentFromCacheModule } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.module'; -import { GmailFullSyncV2Job } from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; -import { GmailPartialSyncV2Job } from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; -import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; -import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; -import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; -import { UnmatchParticipantJob } from 'src/modules/connected-account/jobs/unmatch-participant.job'; +import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; +import { EmailSenderJob } from 'src/engine/integrations/email/email-sender.job'; +import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module'; +import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; +import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; +import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module'; +import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; +import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; +import { CleanInactiveWorkspaceJob } from 'src/engine/workspace-manager/workspace-cleaner/crons/clean-inactive-workspace.job'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/crons/jobs/google-calendar-sync.cron.job'; import { CalendarCreateCompanyAndContactAfterSyncJob } from 'src/modules/calendar/jobs/calendar-create-company-and-contact-after-sync.job'; import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/calendar/jobs/delete-connected-account-associated-calendar-data.job'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job'; +import { GoogleCalendarSyncJob } from 'src/modules/calendar/jobs/google-calendar-sync.job'; +import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module'; +import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; +import { GoogleCalendarSyncModule } from 'src/modules/calendar/services/google-calendar-sync.module'; +import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module'; +import { CreateCompanyAndContactJob } from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job'; +import { MatchParticipantJob } from 'src/modules/connected-account/jobs/match-participant.job'; +import { UnmatchParticipantJob } from 'src/modules/connected-account/jobs/unmatch-participant.job'; +import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module'; +import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; +import { EventObjectMetadata } from 'src/modules/event/standard-objects/event.object-metadata'; +import { GmailFetchMessagesFromCacheCronJob } from 'src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job'; +import { GmailPartialSyncCronJob } from 'src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job'; +import { DeleteConnectedAccountAssociatedMessagingDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-messaging-data.job'; +import { GmailFullSyncJob } from 'src/modules/messaging/jobs/gmail-full-sync.job'; +import { GmailPartialSyncJob } from 'src/modules/messaging/jobs/gmail-partial-sync.job'; +import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/jobs/messaging-create-company-and-contact-after-sync.job'; +import { GmailFetchMessageContentFromCacheModule } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.module'; +import { GmailFullSynV2Module } from 'src/modules/messaging/services/gmail-full-sync-v2/gmail-full-sync.v2.module'; +import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; +import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module'; +import { ThreadCleanerModule } from 'src/modules/messaging/services/thread-cleaner/thread-cleaner.module'; +import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata'; @Module({ imports: [ @@ -155,16 +155,16 @@ import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/cro useClass: SaveEventToDbJob, }, { - provide: FetchAllMessagesFromCacheCronJob.name, - useClass: FetchAllMessagesFromCacheCronJob, + provide: GmailFetchMessagesFromCacheCronJob.name, + useClass: GmailFetchMessagesFromCacheCronJob, }, { - provide: GmailFullSyncV2Job.name, - useClass: GmailFullSyncV2Job, + provide: GmailFullSyncJob.name, + useClass: GmailFullSyncJob, }, { - provide: GmailPartialSyncV2Job.name, - useClass: GmailPartialSyncV2Job, + provide: GmailPartialSyncJob.name, + useClass: GmailPartialSyncJob, }, { provide: GoogleCalendarSyncCronJob.name, diff --git a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts b/packages/twenty-server/src/modules/calendar/commands/calendar-commands.module.ts similarity index 55% rename from packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts rename to packages/twenty-server/src/modules/calendar/commands/calendar-commands.module.ts index c3fa4544f..d35fb03d3 100644 --- a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts +++ b/packages/twenty-server/src/modules/calendar/commands/calendar-commands.module.ts @@ -1,14 +1,17 @@ import { Module } from '@nestjs/common'; -import { GoogleCalendarSyncCommand } from 'src/modules/calendar/commands/google-calendar-sync.command'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; +import { GoogleCalendarSyncCommand } from 'src/modules/calendar/commands/google-calendar-sync.command'; +import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { StartGoogleCalendarSyncCronJobCommand } from 'src/modules/calendar/commands/start-google-calendar-sync.cron.command'; @Module({ imports: [ - ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), + ObjectMetadataRepositoryModule.forFeature([ + ConnectedAccountObjectMetadata, + CalendarChannelObjectMetadata, + ]), ], - providers: [GoogleCalendarSyncCommand, StartGoogleCalendarSyncCronJobCommand], + providers: [GoogleCalendarSyncCommand], }) -export class WorkspaceCalendarSyncCommandsModule {} +export class CalendarCommandsModule {} diff --git a/packages/twenty-server/src/modules/calendar/crons/commands/calendar-cron-commands.module.ts b/packages/twenty-server/src/modules/calendar/crons/commands/calendar-cron-commands.module.ts new file mode 100644 index 000000000..2151e8f13 --- /dev/null +++ b/packages/twenty-server/src/modules/calendar/crons/commands/calendar-cron-commands.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; + +import { GoogleCalendarSyncCommand } from 'src/modules/calendar/commands/google-calendar-sync.command'; +import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; +import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; +import { GoogleCalendarSyncCronCommand } from 'src/modules/calendar/crons/commands/google-calendar-sync.cron.command'; + +@Module({ + providers: [GoogleCalendarSyncCronCommand], +}) +export class CalendarCronCommandsModule {} diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/crons/commands/google-calendar-sync.cron.command.ts similarity index 73% rename from packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts rename to packages/twenty-server/src/modules/calendar/crons/commands/google-calendar-sync.cron.command.ts index a0f867659..90eeed4a0 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/crons/commands/google-calendar-sync.cron.command.ts @@ -4,14 +4,15 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; -import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/crons/jobs/google-calendar-sync.cron.job'; + +const GOOGLE_CALENDAR_SYNC_CRON_PATTERN = '*/5 * * * *'; @Command({ name: 'cron:calendar:google-calendar-sync', description: 'Starts a cron job to sync google calendar for all workspaces.', }) -export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { +export class GoogleCalendarSyncCronCommand extends CommandRunner { constructor( @Inject(MessageQueue.cronQueue) private readonly messageQueueService: MessageQueueService, @@ -24,7 +25,7 @@ export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { GoogleCalendarSyncCronJob.name, undefined, { - repeat: { pattern: googleCalendarSyncCronPattern }, + repeat: { pattern: GOOGLE_CALENDAR_SYNC_CRON_PATTERN }, }, ); } diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.job.ts b/packages/twenty-server/src/modules/calendar/crons/jobs/google-calendar-sync.cron.job.ts similarity index 100% rename from packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.job.ts rename to packages/twenty-server/src/modules/calendar/crons/jobs/google-calendar-sync.cron.job.ts diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts deleted file mode 100644 index 2a0d3f744..000000000 --- a/packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts +++ /dev/null @@ -1 +0,0 @@ -export const googleCalendarSyncCronPattern = '*/5 * * * *'; diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts b/packages/twenty-server/src/modules/messaging/crons/commands/gmail-fetch-messages-from-cache.cron.command.ts similarity index 82% rename from packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts rename to packages/twenty-server/src/modules/messaging/crons/commands/gmail-fetch-messages-from-cache.cron.command.ts index 9f0013f9c..d08eee18c 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/crons/commands/gmail-fetch-messages-from-cache.cron.command.ts @@ -4,7 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job'; +import { GmailFetchMessagesFromCacheCronJob } from 'src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job'; @Command({ name: 'cron:messaging:gmail-fetch-messages-from-cache', @@ -20,7 +20,7 @@ export class GmailFetchMessagesFromCacheCronCommand extends CommandRunner { async run(): Promise { await this.messageQueueService.addCron( - FetchAllMessagesFromCacheCronJob.name, + GmailFetchMessagesFromCacheCronJob.name, undefined, { repeat: { diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts b/packages/twenty-server/src/modules/messaging/crons/commands/gmail-partial-sync.cron.command.ts similarity index 72% rename from packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts rename to packages/twenty-server/src/modules/messaging/crons/commands/gmail-partial-sync.cron.command.ts index 2cfe7be30..3b122ef6d 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/crons/commands/gmail-partial-sync.cron.command.ts @@ -4,8 +4,9 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { GmailPartialSyncCronJob } from 'src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job'; -import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern'; +import { GmailPartialSyncCronJob } from 'src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job'; + +const GMAIL_PARTIAL_SYNC_CRON_PATTERN = '*/5 * * * *'; @Command({ name: 'cron:messaging:gmail-partial-sync', @@ -25,7 +26,7 @@ export class GmailPartialSyncCronCommand extends CommandRunner { GmailPartialSyncCronJob.name, undefined, { - repeat: { pattern: fetchAllWorkspacesMessagesCronPattern }, + repeat: { pattern: GMAIL_PARTIAL_SYNC_CRON_PATTERN }, }, ); } diff --git a/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts b/packages/twenty-server/src/modules/messaging/crons/commands/messaging-cron-commands.module.ts similarity index 78% rename from packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts rename to packages/twenty-server/src/modules/messaging/crons/commands/messaging-cron-commands.module.ts index 48e3c6636..70a16bc9b 100644 --- a/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts +++ b/packages/twenty-server/src/modules/messaging/crons/commands/messaging-cron-commands.module.ts @@ -2,8 +2,8 @@ import { Module } from '@nestjs/common'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { GmailFetchMessagesFromCacheCronCommand } from 'src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command'; -import { GmailPartialSyncCronCommand } from 'src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command'; +import { GmailFetchMessagesFromCacheCronCommand } from 'src/modules/messaging/crons/commands/gmail-fetch-messages-from-cache.cron.command'; +import { GmailPartialSyncCronCommand } from 'src/modules/messaging/crons/commands/gmail-partial-sync.cron.command'; @Module({ imports: [ ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), @@ -13,4 +13,4 @@ import { GmailPartialSyncCronCommand } from 'src/modules/messaging/jobs/crons/gm GmailFetchMessagesFromCacheCronCommand, ], }) -export class MessagingCommandModule {} +export class MessagingCronCommandsModule {} diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job.ts b/packages/twenty-server/src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job.ts similarity index 98% rename from packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job.ts rename to packages/twenty-server/src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job.ts index 8ed5ea610..a9c75fc5e 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job.ts +++ b/packages/twenty-server/src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job.ts @@ -13,7 +13,7 @@ import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-obj import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service'; @Injectable() -export class FetchAllMessagesFromCacheCronJob +export class GmailFetchMessagesFromCacheCronJob implements MessageQueueJob { constructor( diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job.ts b/packages/twenty-server/src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job.ts similarity index 91% rename from packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job.ts rename to packages/twenty-server/src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job.ts index 905a84d22..03b09c3ec 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job.ts +++ b/packages/twenty-server/src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job.ts @@ -13,9 +13,9 @@ import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-s import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; import { - GmailPartialSyncV2Job as GmailPartialSyncV2Job, - GmailPartialSyncV2JobData as GmailPartialSyncV2JobData, -} from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; + GmailPartialSyncJob as GmailPartialSyncJob, + GmailPartialSyncJobData as GmailPartialSyncJobData, +} from 'src/modules/messaging/jobs/gmail-partial-sync.job'; @Injectable() export class GmailPartialSyncCronJob implements MessageQueueJob { @@ -63,8 +63,8 @@ export class GmailPartialSyncCronJob implements MessageQueueJob { await this.connectedAccountRepository.getAll(workspaceId); for (const connectedAccount of connectedAccounts) { - await this.messageQueueService.add( - GmailPartialSyncV2Job.name, + await this.messageQueueService.add( + GmailPartialSyncJob.name, { workspaceId, connectedAccountId: connectedAccount.id, diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern.ts deleted file mode 100644 index 83d6b6db0..000000000 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern.ts +++ /dev/null @@ -1 +0,0 @@ -export const fetchAllWorkspacesMessagesCronPattern = '*/5 * * * *'; diff --git a/packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync-v2.job.ts b/packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync.job.ts similarity index 84% rename from packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync-v2.job.ts rename to packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync.job.ts index ac90589f5..427c6e141 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync-v2.job.ts +++ b/packages/twenty-server/src/modules/messaging/jobs/gmail-full-sync.job.ts @@ -5,23 +5,21 @@ import { MessageQueueJob } from 'src/engine/integrations/message-queue/interface import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service'; import { GmailFullSyncV2Service } from 'src/modules/messaging/services/gmail-full-sync-v2/gmail-full-sync.v2.service'; -export type GmailFullSyncV2JobData = { +export type GmailFullSyncJobData = { workspaceId: string; connectedAccountId: string; }; @Injectable() -export class GmailFullSyncV2Job - implements MessageQueueJob -{ - private readonly logger = new Logger(GmailFullSyncV2Job.name); +export class GmailFullSyncJob implements MessageQueueJob { + private readonly logger = new Logger(GmailFullSyncJob.name); constructor( private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService, private readonly gmailFullSyncV2Service: GmailFullSyncV2Service, ) {} - async handle(data: GmailFullSyncV2JobData): Promise { + async handle(data: GmailFullSyncJobData): Promise { this.logger.log( `gmail full-sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`, ); diff --git a/packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync-v2.job.ts b/packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync.job.ts similarity index 83% rename from packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync-v2.job.ts rename to packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync.job.ts index d327e0ebc..bc1e6c283 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync-v2.job.ts +++ b/packages/twenty-server/src/modules/messaging/jobs/gmail-partial-sync.job.ts @@ -5,23 +5,23 @@ import { MessageQueueJob } from 'src/engine/integrations/message-queue/interface import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service'; import { GmailPartialSyncV2Service } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.service'; -export type GmailPartialSyncV2JobData = { +export type GmailPartialSyncJobData = { workspaceId: string; connectedAccountId: string; }; @Injectable() -export class GmailPartialSyncV2Job - implements MessageQueueJob +export class GmailPartialSyncJob + implements MessageQueueJob { - private readonly logger = new Logger(GmailPartialSyncV2Job.name); + private readonly logger = new Logger(GmailPartialSyncJob.name); constructor( private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService, private readonly gmailPartialSyncV2Service: GmailPartialSyncV2Service, ) {} - async handle(data: GmailPartialSyncV2JobData): Promise { + async handle(data: GmailPartialSyncJobData): Promise { this.logger.log( `gmail partial-sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`, ); diff --git a/packages/twenty-server/src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service.ts b/packages/twenty-server/src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service.ts index e4e6d4cd1..21614a8a9 100644 --- a/packages/twenty-server/src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service.ts +++ b/packages/twenty-server/src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service.ts @@ -19,9 +19,9 @@ import { GMAIL_USERS_MESSAGES_GET_BATCH_SIZE } from 'src/modules/messaging/const import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { SaveMessageAndEmitContactCreationEventService } from 'src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.service'; import { - GmailFullSyncV2JobData, - GmailFullSyncV2Job, -} from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; + GmailFullSyncJobData, + GmailFullSyncJob, +} from 'src/modules/messaging/jobs/gmail-full-sync.job'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { GMAIL_ONGOING_SYNC_TIMEOUT } from 'src/modules/messaging/constants/gmail-ongoing-sync-timeout.constant'; @@ -265,8 +265,8 @@ export class GmailFetchMessageContentFromCacheService { workspaceId: string, connectedAccountId: string, ) { - await this.messageQueueService.add( - GmailFullSyncV2Job.name, + await this.messageQueueService.add( + GmailFullSyncJob.name, { workspaceId, connectedAccountId }, ); } diff --git a/packages/twenty-server/src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.service.ts b/packages/twenty-server/src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.service.ts index 3bba4c43c..cb64d9247 100644 --- a/packages/twenty-server/src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.service.ts +++ b/packages/twenty-server/src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.service.ts @@ -21,9 +21,9 @@ import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decora import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum'; import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { - GmailFullSyncV2Job, - GmailFullSyncV2JobData, -} from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; + GmailFullSyncJob, + GmailFullSyncJobData, +} from 'src/modules/messaging/jobs/gmail-full-sync.job'; @Injectable() export class GmailPartialSyncV2Service { @@ -330,8 +330,8 @@ export class GmailPartialSyncV2Service { workspaceId: string, connectedAccountId: string, ) { - await this.messageQueueService.add( - GmailFullSyncV2Job.name, + await this.messageQueueService.add( + GmailFullSyncJob.name, { workspaceId, connectedAccountId }, ); }