const moment = require('moment') const rimraf = require('rimraf'); const fs = require("fs"); const path = require("path") let TEST = false // TEST = true module.exports = function (app, opts) { const clearExpiredData = app.fs.scheduleInit( { interval: '42 24 4 */3 * *', immediate: TEST, proRun: !TEST, }, async () => { try { const { models } = app.fs.dc const now = moment().format('YYYY-MM-DD HH:mm:ss') await models.UserToken.destroy({ where: { expired: { $lt: now } } }) fs.readdir(path.join(__dirname, `../../downloadFiles`), function (err, files) { if (err) { return; } files.forEach((file) => { fs.stat(path.join(__dirname, `../../downloadFiles/${file}`), (err, stats) => { if (err) { //return; } else { rimraf.sync(path.join(__dirname, `../../downloadFiles/${file}`)); } }); }); }); } catch (error) { app.fs.logger.error(`sechedule: clearExpiredToken, error: ${error}`); } } ); return { clearExpiredData } }