messaging cleanup (#19124)

This commit is contained in:
neo773
2026-03-30 19:51:00 +05:30
committed by GitHub
parent 30bdc24bf8
commit 2fccd29ec6
10 changed files with 444 additions and 5 deletions
@@ -1,14 +1,19 @@
import { Module } from '@nestjs/common';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { CalendarChannelDeletionCleanupJob } from 'src/modules/calendar/calendar-event-cleaner/jobs/calendar-channel-deletion-cleanup.job';
import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/calendar/calendar-event-cleaner/jobs/delete-connected-account-associated-calendar-data.job';
import { CalendarEventCleanerCalendarChannelListener } from 'src/modules/calendar/calendar-event-cleaner/listeners/calendar-event-cleaner-calendar-channel.listener';
import { CalendarEventCleanerConnectedAccountListener } from 'src/modules/calendar/calendar-event-cleaner/listeners/calendar-event-cleaner-connected-account.listener';
import { CalendarEventCleanerService } from 'src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service';
@Module({
imports: [],
imports: [FeatureFlagModule],
providers: [
CalendarEventCleanerService,
CalendarChannelDeletionCleanupJob,
DeleteConnectedAccountAssociatedCalendarDataJob,
CalendarEventCleanerCalendarChannelListener,
CalendarEventCleanerConnectedAccountListener,
],
exports: [CalendarEventCleanerService],
@@ -0,0 +1,54 @@
import { Logger, Scope } from '@nestjs/common';
import { FeatureFlagKey } from 'twenty-shared/types';
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { CalendarEventCleanerService } from 'src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service';
export type CalendarChannelDeletionCleanupJobData = {
workspaceId: string;
calendarChannelId: string;
};
@Processor({
queueName: MessageQueue.calendarQueue,
scope: Scope.REQUEST,
})
export class CalendarChannelDeletionCleanupJob {
private readonly logger = new Logger(CalendarChannelDeletionCleanupJob.name);
constructor(
private readonly calendarEventCleanerService: CalendarEventCleanerService,
private readonly featureFlagService: FeatureFlagService,
) {}
@Process(CalendarChannelDeletionCleanupJob.name)
async handle(data: CalendarChannelDeletionCleanupJobData): Promise<void> {
const isMigrated = await this.featureFlagService.isFeatureEnabled(
FeatureFlagKey.IS_CONNECTED_ACCOUNT_MIGRATED,
data.workspaceId,
);
if (!isMigrated) {
return;
}
this.logger.debug(
`WorkspaceId: ${data.workspaceId} Cleaning up calendar channel event associations for channel ${data.calendarChannelId}`,
);
await this.calendarEventCleanerService.deleteCalendarChannelEventAssociationsByChannelId(
{
workspaceId: data.workspaceId,
calendarChannelId: data.calendarChannelId,
},
);
await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents(
data.workspaceId,
);
}
}
@@ -0,0 +1,42 @@
import { Injectable } from '@nestjs/common';
import { type ObjectRecordDeleteEvent } from 'twenty-shared/database-events';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/types/workspace-event-batch.type';
import { type CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import {
CalendarChannelDeletionCleanupJob,
type CalendarChannelDeletionCleanupJobData,
} from 'src/modules/calendar/calendar-event-cleaner/jobs/calendar-channel-deletion-cleanup.job';
import { OnDatabaseBatchEvent } from 'src/engine/api/graphql/graphql-query-runner/decorators/on-database-batch-event.decorator';
import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
@Injectable()
export class CalendarEventCleanerCalendarChannelListener {
constructor(
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
) {}
@OnDatabaseBatchEvent('calendarChannel', DatabaseEventAction.DESTROYED)
async handleDestroyedEvent(
payload: WorkspaceEventBatch<
ObjectRecordDeleteEvent<CalendarChannelWorkspaceEntity>
>,
) {
await Promise.all(
payload.events.map((eventPayload) =>
this.calendarQueueService.add<CalendarChannelDeletionCleanupJobData>(
CalendarChannelDeletionCleanupJob.name,
{
workspaceId: payload.workspaceId,
calendarChannelId: eventPayload.recordId,
},
),
),
);
}
}
@@ -1,17 +1,82 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { Any, IsNull } from 'typeorm';
import { type WorkspaceEntityManager } from 'src/engine/twenty-orm/entity-manager/workspace-entity-manager';
import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { deleteUsingPagination } from 'src/modules/messaging/message-cleaner/utils/delete-using-pagination.util';
@Injectable()
export class CalendarEventCleanerService {
private readonly logger = new Logger(CalendarEventCleanerService.name);
constructor(
private readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
) {}
async deleteCalendarChannelEventAssociationsByChannelId({
workspaceId,
calendarChannelId,
}: {
workspaceId: string;
calendarChannelId: string;
}) {
const authContext = buildSystemAuthContext(workspaceId);
await this.globalWorkspaceOrmManager.executeInWorkspaceContext(async () => {
const calendarChannelEventAssociationRepository =
await this.globalWorkspaceOrmManager.getRepository(
workspaceId,
'calendarChannelEventAssociation',
);
const workspaceDataSource =
await this.globalWorkspaceOrmManager.getGlobalWorkspaceDataSource();
await workspaceDataSource.transaction(async (manager) => {
const transactionManager = manager as WorkspaceEntityManager;
await deleteUsingPagination(
workspaceId,
500,
async (
limit: number,
offset: number,
_workspaceId: string,
transactionManager?: WorkspaceEntityManager,
) => {
const associations =
await calendarChannelEventAssociationRepository.find(
{
where: { calendarChannelId },
take: limit,
skip: offset,
},
transactionManager,
);
return associations.map(({ id }) => id);
},
async (
ids: string[],
workspaceId: string,
transactionManager?: WorkspaceEntityManager,
) => {
this.logger.log(
`WorkspaceId: ${workspaceId} Deleting ${ids.length} calendar channel event associations for channel ${calendarChannelId}`,
);
await calendarChannelEventAssociationRepository.delete(
ids,
transactionManager,
);
},
transactionManager,
);
});
}, authContext);
}
public async cleanWorkspaceCalendarEvents(workspaceId: string) {
const authContext = buildSystemAuthContext(workspaceId);