Start cron jobs from Node to avoid requiring external cron

master
Manuel Kasper 2022-08-15 11:13:54 +02:00
rodzic 8012964ad4
commit f8db795a1d
8 zmienionych plików z 54 dodań i 0 usunięć

Wyświetl plik

@ -81,3 +81,18 @@ config.sso = {
config.solardata = {
apiKey: process.env.SOLARDATA_API_KEY
};
config.cronjobs = [
{
script: 'importActivators',
schedule: '10 2 * * *'
},
{
script: 'updateSotaSummits',
schedule: '20 4 * * *'
},
{
script: 'updateSotaTrails',
schedule: '30 2 * * *'
}
];

20
cronjobs.js 100644
Wyświetl plik

@ -0,0 +1,20 @@
const cron = require('node-cron')
const child_process = require('child_process')
const config = require('./config')
module.exports = () => {
config.cronjobs.forEach(cronjob => {
cron.schedule(cronjob.schedule, () => {
console.log(`Running job '${cronjob.script}'`)
const start = Date.now()
const child = child_process.fork('jobs/' + cronjob.script)
child.on('error', err => {
console.error(`Job '${cronjob.script}' failed: ${err}`)
})
child.on('close', code => {
const end = Date.now()
console.log(`Job '${cronjob.script}' ended with code ${code} in ${end - start} ms`)
})
})
})
}

14
package-lock.json wygenerowano
Wyświetl plik

@ -31,6 +31,7 @@
"moment": "^2.24.0",
"mongodb": "^4.8.1",
"multer": "^1.4.2",
"node-cron": "^3.0.1",
"reconnect-net": "^1.1.1",
"sharp": "^0.26.3",
"togeojson": "^0.16.0",
@ -2895,6 +2896,14 @@
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.1.0.tgz",
"integrity": "sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw=="
},
"node_modules/node-cron": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.1.tgz",
"integrity": "sha512-RAWZTNn2M5KDIUV/389UX0EXsqvdFAwc9QwHQceh0Ga56dygqSRthqIjwpgZsoDspHGt2rkHdk9Z4RgfPMdALw==",
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/noop-logger": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/noop-logger/-/noop-logger-0.1.1.tgz",
@ -6604,6 +6613,11 @@
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.1.0.tgz",
"integrity": "sha512-flmrDNB06LIl5lywUz7YlNGZH/5p0M7W28k8hzd9Lshtdh1wshD2Y+U4h9LD6KObOy1f+fEVdgprPrEymjM5uw=="
},
"node-cron": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.1.tgz",
"integrity": "sha512-RAWZTNn2M5KDIUV/389UX0EXsqvdFAwc9QwHQceh0Ga56dygqSRthqIjwpgZsoDspHGt2rkHdk9Z4RgfPMdALw=="
},
"noop-logger": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/noop-logger/-/noop-logger-0.1.1.tgz",

Wyświetl plik

@ -31,6 +31,7 @@
"moment": "^2.24.0",
"mongodb": "^4.8.1",
"multer": "^1.4.2",
"node-cron": "^3.0.1",
"reconnect-net": "^1.1.1",
"sharp": "^0.26.3",
"togeojson": "^0.16.0",

Wyświetl plik

@ -18,6 +18,7 @@ const photos_router = require('./photos_router');
const tracks_router = require('./tracks_router');
const solardata = require('./solardata');
const maxmind = require('maxmind');
const cronjobs = require('./cronjobs');
let geoLookup;
import('geolite2-redist').then((geolite2) => {
@ -60,6 +61,8 @@ db.waitDb(() => {
rbnReceiver.start();
})
cronjobs();
app.get('/summits/search', (req, res) => {
let limit = 100;
if (req.query.limit) {
@ -258,6 +261,7 @@ app.get('/activators/search', (req, res) => {
cursor.skip(skip).limit(limit).toArray((err, activators) => {
res.json({activators, total: count});
cursor.close();
});
});
});