summaryrefslogtreecommitdiff
path: root/src/queue/processors/object-storage/clean-remote-files.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-07-01 21:12:14 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-07-01 21:12:14 +0900
commit03e999875a7678c0e5293b87397ab67040010eeb (patch)
tree6451269e48a08baf562e6e57909df33ae95b708a /src/queue/processors/object-storage/clean-remote-files.ts
parentSet job concurrency to reduce performance issue (diff)
downloadmisskey-03e999875a7678c0e5293b87397ab67040010eeb.tar.gz
misskey-03e999875a7678c0e5293b87397ab67040010eeb.tar.bz2
misskey-03e999875a7678c0e5293b87397ab67040010eeb.zip
リモートファイルの削除が重い問題を修正
Diffstat (limited to 'src/queue/processors/object-storage/clean-remote-files.ts')
-rw-r--r--src/queue/processors/object-storage/clean-remote-files.ts50
1 files changed, 50 insertions, 0 deletions
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<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.count({
+ userHost: Not(IsNull()),
+ isLink: false,
+ });
+
+ job.progress(deletedCount / total);
+ }
+
+ logger.succ(`All cahced remote files has been deleted.`);
+ done();
+}