diff --git a/code/api/app/lib/index.js b/code/api/app/lib/index.js index 2784ef4..14d8b1a 100644 --- a/code/api/app/lib/index.js +++ b/code/api/app/lib/index.js @@ -4,6 +4,7 @@ const routes = require('./routes'); const authenticator = require('./middlewares/authenticator'); // const apiLog = require('./middlewares/api-log'); const redisConnect = require('./service/redis') +const schedule = require('./schedule') module.exports.entry = function (app, router, opts) { app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.'); @@ -13,7 +14,7 @@ module.exports.entry = function (app, router, opts) { app.fs.api.logAttr = app.fs.api.logAttr || {}; redisConnect(app, opts) - + schedule(app, opts) router.use(authenticator(app, opts)); // router.use(apiLog(app, opts)); diff --git a/code/api/app/lib/schedule/clearExpiredToken.js b/code/api/app/lib/schedule/clearExpiredToken.js new file mode 100644 index 0000000..09459d3 --- /dev/null +++ b/code/api/app/lib/schedule/clearExpiredToken.js @@ -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 + } +} \ No newline at end of file diff --git a/code/api/app/lib/schedule/index.js b/code/api/app/lib/schedule/index.js new file mode 100644 index 0000000..1b9b587 --- /dev/null +++ b/code/api/app/lib/schedule/index.js @@ -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, + } + } + }); +}; diff --git a/code/api/package.json b/code/api/package.json index 741b53c..ac8bdd3 100644 --- a/code/api/package.json +++ b/code/api/package.json @@ -21,6 +21,7 @@ "koa-convert": "^1.2.0", "koa-proxy": "^0.9.0", "moment": "^2.24.0", + "node-schedule": "^2.1.0", "path": "^0.12.7", "path-to-regexp": "^3.0.0", "pg": "^7.9.0", diff --git a/code/api/yarn.lock b/code/api/yarn.lock index 9c8c880..b776abd 100644 --- a/code/api/yarn.lock +++ b/code/api/yarn.lock @@ -535,6 +535,14 @@ crc@~3.4.0: resolved "http://10.8.30.22:7000/crc/-/crc-3.4.4.tgz#9da1e980e3bd44fc5c93bf5ab3da3378d85e466b" integrity sha1-naHpgOO9RPxck79as9ozeNheRms= +cron-parser@^3.5.0: + version "3.5.0" + resolved "http://10.8.30.22:7000/cron-parser/-/cron-parser-3.5.0.tgz#b1a9da9514c0310aa7ef99c2f3f1d0f8c235257c" + integrity sha1-sanalRTAMQqn75nC8/HQ+MI1JXw= + dependencies: + is-nan "^1.3.2" + luxon "^1.26.0" + crypto-js@^4.0.0: version "4.1.1" resolved "http://10.8.30.22:7000/crypto-js/-/crypto-js-4.1.1.tgz#9e485bcf03521041bd85844786b83fb7619736cf" @@ -1440,6 +1448,14 @@ is-generator-function@^1.0.3, is-generator-function@^1.0.7: dependencies: has-tostringtag "^1.0.0" +is-nan@^1.3.2: + version "1.3.2" + resolved "http://10.8.30.22:7000/is-nan/-/is-nan-1.3.2.tgz#043a54adea31748b55b6cd4e09aadafa69bd9e1d" + integrity sha1-BDpUreoxdItVts1OCara+mm9nh0= + dependencies: + call-bind "^1.0.0" + define-properties "^1.1.3" + is-negative-zero@^2.0.1: version "2.0.2" resolved "http://10.8.30.22:7000/is-negative-zero/-/is-negative-zero-2.0.2.tgz#7bf6f03a28003b8b3965de3ac26f664d765f3150" @@ -1768,6 +1784,11 @@ log-symbols@2.2.0: dependencies: chalk "^2.0.1" +long-timeout@0.1.1: + version "0.1.1" + resolved "http://10.8.30.22:7000/long-timeout/-/long-timeout-0.1.1.tgz#9721d788b47e0bcb5a24c2e2bee1a0da55dab514" + integrity sha1-lyHXiLR+C8taJMLivuGg2lXatRQ= + lru-cache@^5.1.1: version "5.1.1" resolved "http://10.8.30.22:7000/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920" @@ -1782,6 +1803,11 @@ lru-cache@^6.0.0: dependencies: yallist "^4.0.0" +luxon@^1.26.0: + version "1.28.0" + resolved "http://10.8.30.22:7000/luxon/-/luxon-1.28.0.tgz#e7f96daad3938c06a62de0fb027115d251251fbf" + integrity sha1-5/ltqtOTjAamLeD7AnEV0lElH78= + media-typer@0.3.0: version "0.3.0" resolved "http://10.8.30.22:7000/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" @@ -1955,6 +1981,15 @@ node-environment-flags@1.0.5: object.getownpropertydescriptors "^2.0.3" semver "^5.7.0" +node-schedule@^2.1.0: + version "2.1.0" + resolved "http://10.8.30.22:7000/node-schedule/-/node-schedule-2.1.0.tgz#068ae38d7351c330616f7fe7cdb05036f977cbaf" + integrity sha512-nl4JTiZ7ZQDc97MmpTq9BQjYhq7gOtoh7SiPH069gBFBj0PzD8HI7zyFs6rzqL8Y5tTiEEYLxgtbx034YPrbyQ== + dependencies: + cron-parser "^3.5.0" + long-timeout "0.1.1" + sorted-array-functions "^1.3.0" + oauth-sign@~0.9.0: version "0.9.0" resolved "http://10.8.30.22:7000/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" @@ -2643,6 +2678,11 @@ socks@^2.3.3: ip "^1.1.5" smart-buffer "^4.2.0" +sorted-array-functions@^1.3.0: + version "1.3.0" + resolved "http://10.8.30.22:7000/sorted-array-functions/-/sorted-array-functions-1.3.0.tgz#8605695563294dffb2c9796d602bd8459f7a0dd5" + integrity sha1-hgVpVWMpTf+yyXltYCvYRZ96DdU= + source-map@~0.6.1: version "0.6.1" resolved "http://10.8.30.22:7000/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" diff --git a/code/web/client/src/sections/auth/containers/cross.jsx b/code/web/client/src/sections/auth/containers/cross.jsx index 2833247..aab308e 100644 --- a/code/web/client/src/sections/auth/containers/cross.jsx +++ b/code/web/client/src/sections/auth/containers/cross.jsx @@ -37,7 +37,7 @@ const Cross = ({ dispatch, actions }) => { } return () => { window.removeEventListener('message', messageListen); - window.removeEventListener('setItemEvent', messageListen); + window.removeEventListener('storage', storageListen); } }, [])