const schedule = require('node-schedule'); const moment = require('moment') module.exports = function (app, opts) { const clearExpiredToken = schedule.scheduleJob( '0 0 4 * * *', // '*/8 * * * * *', 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 } } }) let redisKeys = await app.redis.keys('*') for (let key of redisKeys) { if (/^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$/.test(key)) { const expired = await app.redis.hget(key, 'expired'); if (moment(now).isAfter(moment(expired))) { await app.redisTools.hdelall(key) } } // plan b : redis token 全删,重新从数据库同步 } } catch (error) { app.fs.logger.error(`sechedule: clearExpiredToken, error: ${error}`); } }); return { clearExpiredToken } }