diff --git a/express.js b/express.js index 8e83154c..a8ce5ff8 100644 --- a/express.js +++ b/express.js @@ -7,6 +7,7 @@ const cookieParser = require('cookie-parser'); const bodyParser = require('body-parser'); const flash = require('connect-flash'); const fs = require('fs'); +const cronJobs = require('./scripts/cron-jobs'); const middleware = require('./routes/middleware'); const app = express(); const newsRouter = require('./routes/views/news'); @@ -196,23 +197,8 @@ app.get('/account/checkUsername', require('./routes/views/checkUsername')); app.get('/password_resetted', require(routes + 'account/get/requestPasswordReset')); app.get('/report_submitted', require(routes + 'account/get/report')); -// Run scripts initially on startup -let requireRunArray = ['extractor']; -for (let i = 0; i < requireRunArray.length; i++) { - try { - require(`./scripts/${requireRunArray[i]}`).run(); - } catch (e) { - console.error(`Error running ${requireRunArray[i]} script. Make sure the API is available (will try again after interval).`, e); - } -// Interval for scripts - setInterval(() => { - try { - require(`./scripts/${requireRunArray[i]}`).run(); - } catch (e) { - console.error(`${requireRunArray[i]} caused the error`, e); - } - }, appConfig.extractorInterval * 60 * 1000); -} +cronJobs() + setInterval(() => { try { require(`./scripts/getRecentUsers`).run(); diff --git a/scripts/cron-jobs.js b/scripts/cron-jobs.js new file mode 100644 index 00000000..53995244 --- /dev/null +++ b/scripts/cron-jobs.js @@ -0,0 +1,16 @@ +const appConfig = require("../config/app") + +module.exports = () => { + try { + require(`./extractor`).run() + } catch (e) { + console.error(`Error running extractor script. Make sure the API is available (will try again after interval).`, e) + } + setInterval(() => { + try { + require(`./extractor`).run() + } catch (e) { + console.error(`extractor caused the error`, e) + } + }, appConfig.extractorInterval * 60 * 1000) +}