yuan_yi
2 years ago
6 changed files with 97 additions and 2 deletions
@ -0,0 +1,35 @@ |
|||
const schedule = require('node-schedule'); |
|||
const moment = require('moment') |
|||
|
|||
module.exports = function (app, opts) { |
|||
const clearExpiredToken = schedule.scheduleJob( |
|||
'* * 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 |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
'use strict'; |
|||
|
|||
const fs = require('fs'); |
|||
// 将定时任务汇集未来可根据需要选取操作
|
|||
module.exports = async function (app, opts) { |
|||
fs.readdirSync(__dirname).forEach((filename) => { |
|||
if (!['index.js'].some(f => filename == f)) { |
|||
const schedule = require(`./${filename}`)(app, opts) |
|||
for (let k of Object.keys(schedule)) { |
|||
console.info(`定时任务 ${k} 启动`); |
|||
} |
|||
app.fs.utils = { |
|||
...app.fs.schedule, |
|||
...schedule, |
|||
} |
|||
} |
|||
}); |
|||
}; |
Loading…
Reference in new issue