Skip to content

Commit

Permalink
moved cron jobs to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaps committed Nov 19, 2023
1 parent 759d771 commit b568278
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
23 changes: 9 additions & 14 deletions express.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const FileStore = require('session-file-store')(session);
const bodyParser = require('body-parser');
const flash = require('connect-flash');
const fs = require('fs');
const setupCronJobs = require('./scripts/cron-jobs');
const middleware = require('./routes/middleware');
const app = express();
const newsRouter = require('./routes/views/news');
Expand Down Expand Up @@ -170,23 +171,17 @@ 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++) {
setupCronJobs()

setInterval(() => {
try {
require(`./scripts/${requireRunArray[i]}`).run();
require(`./scripts/getRecentUsers`).run();

} catch (e) {
console.error(`Error running ${requireRunArray[i]} script. Make sure the API is available (will try again after interval).`, e);
console.error(`getRecentUsers script caused the error`, 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);
}
}, appConfig.playerCountInterval * 1000);


//404 Error Handlers
app.use(function (req, res) {
Expand Down
16 changes: 16 additions & 0 deletions scripts/cron-jobs.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b568278

Please sign in to comment.