refactor: extract files listing out of cleanOldFailedQRScans()
This commit is contained in:
parent
806a2716f0
commit
a573419dd3
1 changed files with 22 additions and 12 deletions
|
|
@ -132,6 +132,18 @@ function updateCoinAtmRadar () {
|
||||||
.then(rates => coinAtmRadar.update(rates, settings()))
|
.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 cleanOldFailedQRScans = () => {
|
||||||
const old = new Date()
|
const old = new Date()
|
||||||
old.setDate(old.getDate() - 2) // 2 days ago
|
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$/, ''))
|
const then = new Date(path.basename(filepath).replace(/-[0-9]+\.jpg$/, ''))
|
||||||
return then < old
|
return then < old
|
||||||
}
|
}
|
||||||
const baseDirPath = path.join(OPERATOR_DATA_DIR, 'failedQRScans')
|
readdirRec(path.join(OPERATOR_DATA_DIR, 'failedQRScans'))
|
||||||
fs.readdir(baseDirPath)
|
.then(entries => Promise.all(
|
||||||
.then(machines => Promise.all(
|
entries
|
||||||
machines.map(
|
.filter(entry => entry.isFile())
|
||||||
machine => {
|
.map(entry => entry.path)
|
||||||
const machineDirPath = path.join(baseDirPath, machine)
|
.filter(isOld)
|
||||||
return fs.readdir(machineDirPath)
|
.map(fs.unlink)
|
||||||
.then(fnames => fnames.map(fname => path.join(machineDirPath, fname)))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
))
|
))
|
||||||
.then(_.flatten)
|
.catch(err => {
|
||||||
.then(filepaths => Promise.all(filepaths.filter(isOld).map(fs.unlink)))
|
console.log("Error cleaning up failed QR scans:", err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeEachSchema (schemas = ['public']) {
|
function initializeEachSchema (schemas = ['public']) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue