Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved cron jobs to another file #477

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 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,7 @@ 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);
}
setupCronJobs()

//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)
}