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)
This commit is contained in:
@ -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 {}
|
||||
@ -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 {}
|
||||
@ -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 },
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export const googleCalendarSyncCronPattern = '*/5 * * * *';
|
||||
@ -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<void> {
|
||||
await this.messageQueueService.addCron<undefined>(
|
||||
FetchAllMessagesFromCacheCronJob.name,
|
||||
GmailFetchMessagesFromCacheCronJob.name,
|
||||
undefined,
|
||||
{
|
||||
repeat: {
|
||||
@ -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 },
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -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 {}
|
||||
@ -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<undefined>
|
||||
{
|
||||
constructor(
|
||||
@ -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<undefined> {
|
||||
@ -63,8 +63,8 @@ export class GmailPartialSyncCronJob implements MessageQueueJob<undefined> {
|
||||
await this.connectedAccountRepository.getAll(workspaceId);
|
||||
|
||||
for (const connectedAccount of connectedAccounts) {
|
||||
await this.messageQueueService.add<GmailPartialSyncV2JobData>(
|
||||
GmailPartialSyncV2Job.name,
|
||||
await this.messageQueueService.add<GmailPartialSyncJobData>(
|
||||
GmailPartialSyncJob.name,
|
||||
{
|
||||
workspaceId,
|
||||
connectedAccountId: connectedAccount.id,
|
||||
@ -1 +0,0 @@
|
||||
export const fetchAllWorkspacesMessagesCronPattern = '*/5 * * * *';
|
||||
@ -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<GmailFullSyncV2JobData>
|
||||
{
|
||||
private readonly logger = new Logger(GmailFullSyncV2Job.name);
|
||||
export class GmailFullSyncJob implements MessageQueueJob<GmailFullSyncJobData> {
|
||||
private readonly logger = new Logger(GmailFullSyncJob.name);
|
||||
|
||||
constructor(
|
||||
private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService,
|
||||
private readonly gmailFullSyncV2Service: GmailFullSyncV2Service,
|
||||
) {}
|
||||
|
||||
async handle(data: GmailFullSyncV2JobData): Promise<void> {
|
||||
async handle(data: GmailFullSyncJobData): Promise<void> {
|
||||
this.logger.log(
|
||||
`gmail full-sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`,
|
||||
);
|
||||
@ -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<GmailPartialSyncV2JobData>
|
||||
export class GmailPartialSyncJob
|
||||
implements MessageQueueJob<GmailPartialSyncJobData>
|
||||
{
|
||||
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<void> {
|
||||
async handle(data: GmailPartialSyncJobData): Promise<void> {
|
||||
this.logger.log(
|
||||
`gmail partial-sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`,
|
||||
);
|
||||
@ -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<GmailFullSyncV2JobData>(
|
||||
GmailFullSyncV2Job.name,
|
||||
await this.messageQueueService.add<GmailFullSyncJobData>(
|
||||
GmailFullSyncJob.name,
|
||||
{ workspaceId, connectedAccountId },
|
||||
);
|
||||
}
|
||||
|
||||
@ -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<GmailFullSyncV2JobData>(
|
||||
GmailFullSyncV2Job.name,
|
||||
await this.messageQueueService.add<GmailFullSyncJobData>(
|
||||
GmailFullSyncJob.name,
|
||||
{ workspaceId, connectedAccountId },
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user