From 5ce2b42a0e0a6dd343c7b3bb5d920c4656b3b169 Mon Sep 17 00:00:00 2001 From: fatchan Date: Sat, 17 Aug 2019 15:47:37 +0000 Subject: [PATCH] dont build homepage immediately on schedule start, and move delete captchas to separate file --- helpers/captcha/deletecaptchas.js | 25 +++++++++++++++++++++++++ models/pages/globalmanage.js | 4 +--- schedules.js | 29 +++-------------------------- 3 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 helpers/captcha/deletecaptchas.js diff --git a/helpers/captcha/deletecaptchas.js b/helpers/captcha/deletecaptchas.js new file mode 100644 index 00000000..c80f72a5 --- /dev/null +++ b/helpers/captcha/deletecaptchas.js @@ -0,0 +1,25 @@ +'use strict'; + +const { stat, remove, readdir } = require('fs-extra') + , uploadDirectory = require(__dirname+'/../files/uploadDirectory.js') + , msTime = require(__dirname+'/../mstime.js') + +module.exports = async () => { + const files = await readdir(`${uploadDirectory}captcha/`); + if (files.length > 0) { + files.forEach(async (file) => { + try { + const filePath = `${uploadDirectory}captcha/${file}`; + const stats = await stat(filePath); + const now = Date.now(); + const expiry = new Date(stats.ctime).getTime() + msTime.minute*5; + if (now > expiry) { + await remove(filePath); + console.log(`Deleted expired captcha ${filePath}`) + } + } catch (e) { + console.error(e); + } + }); + } +} diff --git a/models/pages/globalmanage.js b/models/pages/globalmanage.js index 38bb6d27..27c748e4 100644 --- a/models/pages/globalmanage.js +++ b/models/pages/globalmanage.js @@ -1,8 +1,6 @@ 'use strict'; -const Posts = require(__dirname+'/../../db/posts.js') - , Bans = require(__dirname+'/../../db/bans.js') - , News = require(__dirname+'/../../db/news.js'); +const { Bans, News, Posts } = require(__dirname+'/../../db/') module.exports = async (req, res, next) => { diff --git a/schedules.js b/schedules.js index 9139c8a7..52378c3d 100644 --- a/schedules.js +++ b/schedules.js @@ -4,30 +4,9 @@ process .on('uncaughtException', console.error) .on('unhandledRejection', console.error); -const { stat, remove, readdir } = require('fs-extra') - , uploadDirectory = require(__dirname+'/helpers/files/uploadDirectory.js') - , msTime = require(__dirname+'/helpers/mstime.js') - , Mongo = require(__dirname+'/db/db.js') - -async function deleteCaptchas() { - const files = await readdir(`${uploadDirectory}captcha/`); - if (files.length > 0) { - files.forEach(async (file) => { - try { - const filePath = `${uploadDirectory}captcha/${file}`; - const stats = await stat(filePath); - const now = Date.now(); - const expiry = new Date(stats.ctime).getTime() + msTime.minute*5; - if (now > expiry) { - await remove(filePath); - console.log(`Deleted expired captcha ${filePath}`) - } - } catch (e) { - console.error(e); - } - }); - } -} +const msTime = require(__dirname+'/helpers/mstime.js') + , deleteCaptchas = require(__dirname+'/helpers/captcha/deletecaptchas.js') + , Mongo = require(__dirname+'/db/db.js'); (async () => { @@ -37,7 +16,6 @@ async function deleteCaptchas() { console.log('Starting schedules'); - await buildHomepage(); setInterval(async () => { try { await buildHomepage(); @@ -46,7 +24,6 @@ async function deleteCaptchas() { } }, msTime.minute*5); //rebuild homepage for pph updates - //could make this use a db changefeed setInterval(async () => { try { await deleteCaptchas();