-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Add cron for clean up unused files
- Loading branch information
1 parent
d44935e
commit 3912040
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { core_files, core_files_using } from '@/database/schema/files'; | ||
import { FilesHelperService } from '@/helpers/files/files-helper.service'; | ||
import { InternalDatabaseService } from '@/utils/database/internal_database.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
import { and, eq, isNull, lte } from 'drizzle-orm'; | ||
|
||
@Injectable() | ||
export class FilesCron { | ||
constructor( | ||
private readonly databaseService: InternalDatabaseService, | ||
private readonly filesService: FilesHelperService, | ||
) {} | ||
|
||
@Cron(CronExpression.EVERY_6_HOURS, { | ||
name: 'core_clear_unused_files', | ||
}) | ||
async clearUnusedFiles() { | ||
const twelveHoursAgo = new Date(Date.now() - 12 * 60 * 60 * 1000); | ||
const findFiles = await this.databaseService.db | ||
.select() | ||
.from(core_files) | ||
.leftJoin(core_files_using, eq(core_files.id, core_files_using.file_id)) | ||
.where( | ||
and( | ||
isNull(core_files_using.file_id), | ||
lte(core_files.created_at, twelveHoursAgo), | ||
), | ||
); | ||
|
||
await Promise.all( | ||
findFiles.map(async file => { | ||
await Promise.all([ | ||
this.filesService.delete({ | ||
dir_folder: file.core_files.dir_folder, | ||
file_name: file.core_files.file_name, | ||
}), | ||
this.databaseService.db | ||
.delete(core_files) | ||
.where(eq(core_files.id, file.core_files.id)), | ||
]); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { FilesCron } from './clean.core'; | ||
import { FilesController } from './files.controller'; | ||
import { DeleteFilesService } from './services/delete.service'; | ||
import { UploadFilesService } from './services/upload.service'; | ||
|
||
@Module({ | ||
providers: [UploadFilesService, DeleteFilesService], | ||
providers: [UploadFilesService, DeleteFilesService, FilesCron], | ||
controllers: [FilesController], | ||
}) | ||
export class FilesModule {} |