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 f94dbfd commit aee623d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
20 changes: 3 additions & 17 deletions express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
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 aee623d

Please sign in to comment.