From f9fd0dc2c31221e920e3eecc553eb520a862ed12 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Thu, 23 May 2019 13:47:12 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B1=D0=B0=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/ReaderWorker.js | 48 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/server/core/ReaderWorker.js b/server/core/ReaderWorker.js index 46b758f3..1921fcef 100644 --- a/server/core/ReaderWorker.js +++ b/server/core/ReaderWorker.js @@ -131,32 +131,34 @@ class ReaderWorker { return `file://${hash}`; } - async periodicCleanDir(dir, maxSize, timeout) { - const list = await fs.readdir(dir); + async periodicCleanDir(dir, maxSize, timeout) { + try { + const list = await fs.readdir(dir); - let size = 0; - let files = []; - for (const name of list) { - const stat = await fs.stat(`${dir}/${name}`); - if (!stat.isDirectory()) { - size += stat.size; - files.push({name, stat}); + let size = 0; + let files = []; + for (const name of list) { + const stat = await fs.stat(`${dir}/${name}`); + if (!stat.isDirectory()) { + size += stat.size; + files.push({name, stat}); + } } + + files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs); + + let i = 0; + while (i < files.length && size > maxSize) { + const file = files[i]; + await fs.remove(`${dir}/${file.name}`); + size -= file.stat.size; + i++; + } + } finally { + setTimeout(() => { + this.periodicCleanDir(dir, maxSize, timeout); + }, timeout); } - - files.sort((a, b) => a.stat.mtimeMs - b.stat.mtimeMs); - - let i = 0; - while (i < files.length && size > maxSize) { - const file = files[i]; - await fs.remove(`${dir}/${file.name}`); - size -= file.stat.size; - i++; - } - - setTimeout(() => { - this.periodicCleanDir(dir, maxSize, timeout); - }, timeout); } }