From d327bc355c8583231e058731934cf51ab25d9ce5 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Sat, 21 Oct 2023 18:51:27 +0200 Subject: [PATCH] fix: delete unfinished shares after a day --- backend/src/jobs/jobs.service.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/src/jobs/jobs.service.ts b/backend/src/jobs/jobs.service.ts index d6dd1ff4..dc509254 100644 --- a/backend/src/jobs/jobs.service.ts +++ b/backend/src/jobs/jobs.service.ts @@ -61,6 +61,28 @@ export class JobsService { } } + @Cron("0 */6 * * *") + async deleteUnfinishedShares() { + const unfinishedShares = await this.prisma.share.findMany({ + where: { + createdAt: { lt: moment().subtract(1, "day").toDate() }, + uploadLocked: false, + }, + }); + + for (const unfinishedShare of unfinishedShares) { + await this.prisma.share.delete({ + where: { id: unfinishedShare.id }, + }); + + await this.fileService.deleteAllFiles(unfinishedShare.id); + } + + if (unfinishedShares.length > 0) { + this.logger.log(`Deleted ${unfinishedShares.length} unfinished shares`); + } + } + @Cron("0 0 * * *") deleteTemporaryFiles() { let filesDeleted = 0; @@ -93,7 +115,7 @@ export class JobsService { this.logger.log(`Deleted ${filesDeleted} temporary files`); } - @Cron("0 * * * *") + @Cron("1 * * * *") async deleteExpiredTokens() { const { count: refreshTokenCount } = await this.prisma.refreshToken.deleteMany({