From 03e999875a7678c0e5293b87397ab67040010eeb Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 1 Jul 2019 21:12:14 +0900 Subject: リモートファイルの削除が重い問題を修正 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../object-storage/clean-remote-files.ts | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/queue/processors/object-storage/clean-remote-files.ts (limited to 'src/queue/processors/object-storage/clean-remote-files.ts') diff --git a/src/queue/processors/object-storage/clean-remote-files.ts b/src/queue/processors/object-storage/clean-remote-files.ts new file mode 100644 index 0000000000..7b34892e1f --- /dev/null +++ b/src/queue/processors/object-storage/clean-remote-files.ts @@ -0,0 +1,50 @@ +import * as Bull from 'bull'; + +import { queueLogger } from '../../logger'; +import { deleteFileSync } from '../../../services/drive/delete-file'; +import { DriveFiles } from '../../../models'; +import { MoreThan, Not, IsNull } from 'typeorm'; + +const logger = queueLogger.createSubLogger('clean-remote-files'); + +export default async function cleanRemoteFiles(job: Bull.Job, done: any): Promise { + 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.count({ + userHost: Not(IsNull()), + isLink: false, + }); + + job.progress(deletedCount / total); + } + + logger.succ(`All cahced remote files has been deleted.`); + done(); +} -- cgit v1.2.3-freya