From a573419dd3b8eb4d37a468c1d1cab1130ea6b80d Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 3 Jul 2024 12:23:04 +0100 Subject: [PATCH] refactor: extract files listing out of `cleanOldFailedQRScans()` --- lib/poller.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/poller.js b/lib/poller.js index 75ffd6fd..2178255e 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -132,6 +132,18 @@ function updateCoinAtmRadar () { .then(rates => coinAtmRadar.update(rates, settings())) } +const readdir = dirpath => + fs.readdir(dirpath, { withFileTypes: true }) + .then(_.map(entry => _.set('path', path.join(dirpath, entry.name), entry))) + +const readdirRec = rootPath => + readdir(rootPath) + .then(entries => Promise.all( + entries.map(entry => entry.isDirectory() ? readdirRec(entry.path) : [entry]) + )) + .then(_.flatten) + +// @see lib/machine-loader.js:updateFailedQRScans() const cleanOldFailedQRScans = () => { const old = new Date() old.setDate(old.getDate() - 2) // 2 days ago @@ -139,19 +151,17 @@ const cleanOldFailedQRScans = () => { const then = new Date(path.basename(filepath).replace(/-[0-9]+\.jpg$/, '')) return then < old } - const baseDirPath = path.join(OPERATOR_DATA_DIR, 'failedQRScans') - fs.readdir(baseDirPath) - .then(machines => Promise.all( - machines.map( - machine => { - const machineDirPath = path.join(baseDirPath, machine) - return fs.readdir(machineDirPath) - .then(fnames => fnames.map(fname => path.join(machineDirPath, fname))) - } - ) + readdirRec(path.join(OPERATOR_DATA_DIR, 'failedQRScans')) + .then(entries => Promise.all( + entries + .filter(entry => entry.isFile()) + .map(entry => entry.path) + .filter(isOld) + .map(fs.unlink) )) - .then(_.flatten) - .then(filepaths => Promise.all(filepaths.filter(isOld).map(fs.unlink))) + .catch(err => { + console.log("Error cleaning up failed QR scans:", err) + }) } function initializeEachSchema (schemas = ['public']) {