FoundKey/packages/backend/src/queue/processors/object-storage/clean-remote-files.ts
Johann150 f245b6e517
Some checks failed
ci/woodpecker/push/lint-backend Pipeline failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline failed
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/test Pipeline failed
server: remove direct chalk dependency
Colouring of logs is not important to me because if I view them in journalctl
there are no colours anyway.
2024-04-01 19:10:55 +02:00

51 lines
1.1 KiB
TypeScript

import Bull from 'bull';
import { MoreThan, Not, IsNull } from 'typeorm';
import { DriveFiles } from '@/models/index.js';
import { deleteFileSync } from '@/services/drive/delete-file.js';
import { queueLogger } from '@/queue/logger.js';
const logger = queueLogger.createSubLogger('clean-remote-files');
export default async function cleanRemoteFiles(job: Bull.Job<Record<string, unknown>>, done: any): Promise<void> {
logger.info('Deleting cached remote files...');
let deletedCount = 0;
let cursor: any = null;
while (true) {
const files = await DriveFiles.find({
where: {
userHost: Not(IsNull()),
isLink: false,
...(cursor ? { id: MoreThan(cursor) } : {}),
},
take: 8,
order: {
id: 1,
},
});
if (files.length === 0) {
job.progress(100);
break;
}
cursor = files[files.length - 1].id;
await Promise.all(files.map(file => deleteFileSync(file, true)));
deletedCount += 8;
const total = await DriveFiles.countBy({
userHost: Not(IsNull()),
isLink: false,
});
job.progress(deletedCount / total);
}
logger.succ('All cached remote files have been deleted.');
done();
}