diff --git a/lib/poller.js b/lib/poller.js index ca002612..d58deb71 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -1,3 +1,5 @@ +const fs = require('fs/promises') +const path = require('path') const _ = require('lodash/fp') const Queue = require('queue-promise') const plugins = require('./plugins') @@ -31,6 +33,7 @@ const RADAR_UPDATE_INTERVAL = 5 * T.minutes const PRUNE_MACHINES_HEARTBEAT = 1 * T.day const TRANSACTION_BATCH_LIFECYCLE = 20 * T.minutes const TICKER_RATES_INTERVAL = 59 * T.seconds +const FAILED_SCANS_INTERVAL = 1 * T.day const CHECK_NOTIFICATION_INTERVAL = 20 * T.seconds const PENDING_INTERVAL = 10 * T.seconds @@ -39,6 +42,8 @@ const CACHE_ENTRY_TTL = 3600 // seconds const FAST_QUEUE_WAIT = 1 * T.seconds const SLOW_QUEUE_WAIT = 10 * T.seconds +const OPERATOR_DATA_DIR = process.env.OPERATOR_DATA_DIR + const FAST_QUEUE = new Queue({ concurrent: 600, interval: FAST_QUEUE_WAIT @@ -127,6 +132,28 @@ function updateCoinAtmRadar () { .then(rates => coinAtmRadar.update(rates, settings())) } +const cleanOldFailedScans = () => { + const old = new Date() + old.setDate(old.getDate() - 2) // 2 days ago + const isOld = filepath => { + 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))) + } + ) + )) + .then(_.flatten) + .then(filepaths => Promise.all(filepaths.filter(isOld).map(fs.unlink))) +} + function initializeEachSchema (schemas = ['public']) { // for each schema set "thread variables" and do polling return _.forEach(schema => { @@ -206,6 +233,7 @@ function doPolling (schema) { addToQueue(updateAndLoadSanctions, SANCTIONS_UPDATE_INTERVAL, schema, QUEUE.SLOW) addToQueue(updateCoinAtmRadar, RADAR_UPDATE_INTERVAL, schema, QUEUE.SLOW) addToQueue(pi().pruneMachinesHeartbeat, PRUNE_MACHINES_HEARTBEAT, schema, QUEUE.SLOW, settings) + addToQueue(cleanOldFailedScans, FAILED_SCANS_INTERVAL, schema, QUEUE.SLOW, settings) } function setup (schemasToAdd = [], schemasToRemove = []) {