Reset workspaces to delete between executions (#3625)

This commit is contained in:
martmull
2024-01-25 18:06:53 +01:00
committed by GitHub
parent e0405edb38
commit ca6250286a

View File

@ -39,7 +39,6 @@ export class CleanInactiveWorkspaceJob
private readonly logger = new Logger(CleanInactiveWorkspaceJob.name); private readonly logger = new Logger(CleanInactiveWorkspaceJob.name);
private readonly inactiveDaysBeforeDelete; private readonly inactiveDaysBeforeDelete;
private readonly inactiveDaysBeforeEmail; private readonly inactiveDaysBeforeEmail;
private workspacesToDelete: WorkspaceToDeleteData[] = [];
constructor( constructor(
private readonly dataSourceService: DataSourceService, private readonly dataSourceService: DataSourceService,
@ -149,50 +148,6 @@ export class CleanInactiveWorkspaceJob
}); });
} }
async deleteWorkspace(
dataSource: DataSourceEntity,
daysSinceInactive: number,
isDryRun: boolean,
): Promise<void> {
this.logger.log(
`${this.getDryRunLogHeader(isDryRun)}Sending email to delete workspace ${
dataSource.workspaceId
} inactive since ${daysSinceInactive} days`,
);
if (isDryRun) {
return;
}
const emailData = {
daysSinceInactive: daysSinceInactive,
workspaceId: `${dataSource.workspaceId}`,
};
this.workspacesToDelete.push(emailData);
}
async processWorkspace(
dataSource: DataSourceEntity,
objectsMetadata: ObjectMetadataEntity[],
isDryRun: boolean,
): Promise<void> {
const mostRecentUpdatedAt = await this.getMostRecentUpdatedAt(
dataSource,
objectsMetadata,
);
const daysSinceInactive = Math.floor(
(new Date().getTime() - mostRecentUpdatedAt.getTime()) /
MILLISECONDS_IN_ONE_DAY,
);
if (daysSinceInactive > this.inactiveDaysBeforeDelete) {
await this.deleteWorkspace(dataSource, daysSinceInactive, isDryRun);
} else if (daysSinceInactive > this.inactiveDaysBeforeEmail) {
await this.warnWorkspaceUsers(dataSource, daysSinceInactive, isDryRun);
}
}
async isWorkspaceCleanable(dataSource: DataSourceEntity): Promise<boolean> { async isWorkspaceCleanable(dataSource: DataSourceEntity): Promise<boolean> {
const workspaceFeatureFlags = await this.featureFlagRepository.find({ const workspaceFeatureFlags = await this.featureFlagRepository.find({
where: { workspaceId: dataSource.workspaceId }, where: { workspaceId: dataSource.workspaceId },
@ -223,11 +178,23 @@ export class CleanInactiveWorkspaceJob
return chunkedArray; return chunkedArray;
} }
async sendDeleteWorkspaceEmail(isDryRun: boolean): Promise<void> { async sendDeleteWorkspaceEmail(
if (isDryRun || this.workspacesToDelete.length === 0) { workspacesToDelete: WorkspaceToDeleteData[],
isDryRun: boolean,
): Promise<void> {
this.logger.log(
`${this.getDryRunLogHeader(
isDryRun,
)}Sending email to delete workspaces "${workspacesToDelete
.map((workspaceToDelete) => workspaceToDelete.workspaceId)
.join('", "')}"`,
);
if (isDryRun || workspacesToDelete.length === 0) {
return; return;
} }
const emailTemplate = DeleteInactiveWorkspaceEmail(this.workspacesToDelete);
const emailTemplate = DeleteInactiveWorkspaceEmail(workspacesToDelete);
const html = render(emailTemplate, { const html = render(emailTemplate, {
pretty: true, pretty: true,
}); });
@ -247,6 +214,8 @@ export class CleanInactiveWorkspaceJob
async handle(data: CleanInactiveWorkspacesCommandOptions): Promise<void> { async handle(data: CleanInactiveWorkspacesCommandOptions): Promise<void> {
const isDryRun = data.dryRun || false; const isDryRun = data.dryRun || false;
const workspacesToDelete: WorkspaceToDeleteData[] = [];
this.logger.log(`${this.getDryRunLogHeader(isDryRun)}Job running...`); this.logger.log(`${this.getDryRunLogHeader(isDryRun)}Job running...`);
if (!this.inactiveDaysBeforeDelete && !this.inactiveDaysBeforeEmail) { if (!this.inactiveDaysBeforeDelete && !this.inactiveDaysBeforeEmail) {
this.logger.log( this.logger.log(
@ -289,11 +258,32 @@ export class CleanInactiveWorkspaceJob
dataSource.workspaceId dataSource.workspaceId
}`, }`,
); );
await this.processWorkspace(dataSource, objectsMetadata, isDryRun);
const mostRecentUpdatedAt = await this.getMostRecentUpdatedAt(
dataSource,
objectsMetadata,
);
const daysSinceInactive = Math.floor(
(new Date().getTime() - mostRecentUpdatedAt.getTime()) /
MILLISECONDS_IN_ONE_DAY,
);
if (daysSinceInactive > this.inactiveDaysBeforeDelete) {
workspacesToDelete.push({
daysSinceInactive: daysSinceInactive,
workspaceId: `${dataSource.workspaceId}`,
});
} else if (daysSinceInactive > this.inactiveDaysBeforeEmail) {
await this.warnWorkspaceUsers(
dataSource,
daysSinceInactive,
isDryRun,
);
}
} }
} }
await this.sendDeleteWorkspaceEmail(isDryRun); await this.sendDeleteWorkspaceEmail(workspacesToDelete, isDryRun);
this.logger.log(`${this.getDryRunLogHeader(isDryRun)}job done!`); this.logger.log(`${this.getDryRunLogHeader(isDryRun)}job done!`);
} }