backend: update CleanRemoteFileProcessorService

This commit is contained in:
fly_mc 2024-11-19 00:04:19 +08:00
parent 9b0b716d6f
commit 8f366a39c8

View file

@ -33,6 +33,12 @@ export class CleanRemoteFilesProcessorService {
let deletedCount = 0;
let cursor: MiDriveFile['id'] | null = null;
let errorCount = 0;
const total = await this.driveFilesRepository.countBy({
userHost: Not(IsNull()),
isLink: false,
});
while (true) {
const files = await this.driveFilesRepository.find({
@ -41,7 +47,7 @@ export class CleanRemoteFilesProcessorService {
isLink: false,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 8,
take: 256,
order: {
id: 1,
},
@ -54,23 +60,21 @@ export class CleanRemoteFilesProcessorService {
cursor = files.at(-1)?.id ?? null;
try {
await Promise.all(files.map(file => this.driveService.deleteFileSync(file, true)));
} catch (err) {
// 打印错误日志,但不终止执行
this.logger.error('Failed to delete some cached remote files:' + (err as Error).toString());
}
// Handle deletion in a batch
const results = await Promise.allSettled(files.map(file => this.driveService.deleteFileSync(file, true)));
deletedCount += 8;
const total = await this.driveFilesRepository.countBy({
userHost: Not(IsNull()),
isLink: false,
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
deletedCount++;
} else {
this.logger.error(`Failed to delete file ID ${files[index].id}: ${result.reason}`);
errorCount++;
}
});
job.updateProgress(100 / total * deletedCount);
await job.updateProgress(100 / total * deletedCount);
}
this.logger.succ('All cached remote files has been deleted.');
this.logger.succ(`All cached remote files processed. Total deleted: ${deletedCount}, Failed: ${errorCount}.`);
}
}