From 7718a37af09732436abc8ddb61fed746ca7a8aeb Mon Sep 17 00:00:00 2001 From: fatchan Date: Mon, 1 Jul 2019 09:57:55 +0000 Subject: [PATCH] schedules handle errors correctly and log when rebuild homepage --- helpers/build.js | 1 + schedules.js | 66 ++++++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/helpers/build.js b/helpers/build.js index d00c76b1..7d2519d9 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -131,6 +131,7 @@ console.log('multi building board pages', `${board._id}/ ${startpage === 1 ? 'in }, buildHomepage: async () => { +console.log('building homepage /index.html'); const boards = await Boards.find(); const yesterday = Math.floor((Date.now() - msTime.hour)/1000); const yesterdayObjectId = Mongo.ObjectId.createFromTime(yesterday); diff --git a/schedules.js b/schedules.js index f8709b5a..79e86ea8 100644 --- a/schedules.js +++ b/schedules.js @@ -1,46 +1,62 @@ 'use strict'; -const util = require('util') - , fs = require('fs') - , unlink = util.promisify(fs.unlink) - , stat = util.promisify(fs.stat) - , readdir = util.promisify(fs.readdir) +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() { - - try { - const files = await readdir(`${uploadDirectory}captcha/`); - if (files.length > 0) { - files.forEach(async (file) => { + 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).catch(e => console.error); - if (!stats) { - return; - } + const stats = await stat(filePath); const now = Date.now(); - const expiry = new Date(stats.ctime).getTime() + 6*1000*60; //6 minutes ahead + const expiry = new Date(stats.ctime).getTime()// + msTime.minute*5; if (now > expiry) { - await unlink(filePath).catch(e => console.error); + await remove(filePath); + console.log(`Deleted expired captcha ${filePath}`) } - }); - } - } catch (err) { - console.error(err); + } catch (e) { + /* + catching here to still get the error, but it wont reject the promise + returned by deleteCaptchas, since this is anon async function in the + foreach loop. this way we dont stop deleting captchas if only one fails + */ + console.error(e); + } + }); } - } (async () => { await Mongo.connect(); const { buildHomepage } = require(__dirname+'/helpers/build.js'); - buildHomepage(); - setInterval(buildHomepage, msTime.hour); //hourly rebuild homepage for posts/day - deleteCaptchas(); - setInterval(deleteCaptchas, msTime.minute*6); //delete files for expired captchas + console.log('Starting schedules'); + +buildHomepage() +deleteCaptchas(); + setInterval(async () => { + try { + await buildHomepage(); + } catch (e) { + console.error(e); + } + }, msTime.hour); //hourly rebuild homepage for posts/day + + setInterval(async () => { + try { + await deleteCaptchas(); + } catch (e) { + console.error(e); + } + }, msTime.minute*5); //delete files for expired captchas })();