mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 11:37:08 +00:00
Run some tasks periodically from the main process.
This reduces reliance on crontab jobs.
This commit is contained in:
@@ -11,6 +11,7 @@ async function main () {
|
||||
try {
|
||||
const api = require('./api');
|
||||
const ws = require('./ws');
|
||||
const periodicTasks = require('./periodic-tasks').init();
|
||||
|
||||
const { fork } = require('child_process');
|
||||
|
||||
@@ -22,6 +23,7 @@ async function main () {
|
||||
|
||||
INFO("Versions:", versions);
|
||||
|
||||
periodicTasks.start();
|
||||
|
||||
const eventManagerPath = [__dirname, "events"].join("/");
|
||||
const eventManager = fork(eventManagerPath, /*{ stdio: 'ignore' }*/);
|
||||
|
||||
38
lib/www/server/periodic-tasks/index.js
Normal file
38
lib/www/server/periodic-tasks/index.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const tasks = require('./tasks');
|
||||
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
||||
|
||||
function init () {
|
||||
const iids = [];
|
||||
|
||||
function start () {
|
||||
INFO("Initialising %d periodic tasks", tasks.length);
|
||||
for (let t of tasks) {
|
||||
const iid = setInterval(t.task, t.timeout);
|
||||
iids.push(iid);
|
||||
}
|
||||
return iids;
|
||||
};
|
||||
|
||||
function stop () {
|
||||
INFO("Stopping %d periodic tasks", iids.length);
|
||||
for (let iid of iids) {
|
||||
clearInterval(iid);
|
||||
}
|
||||
}
|
||||
|
||||
async function cleanup () {
|
||||
stop();
|
||||
DEBUG("Cleaning up %d periodic tasks", tasks.length);
|
||||
for (let t of tasks) {
|
||||
if (t.cleanup) {
|
||||
await t.cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { start, stop, cleanup, iids };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init
|
||||
};
|
||||
4
lib/www/server/periodic-tasks/tasks/index.js
Normal file
4
lib/www/server/periodic-tasks/tasks/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
module.exports = [
|
||||
require('./purge-notifications')
|
||||
];
|
||||
20
lib/www/server/periodic-tasks/tasks/purge-notifications.js
Normal file
20
lib/www/server/periodic-tasks/tasks/purge-notifications.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const { purge } = require('../../lib/db/notify');
|
||||
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
||||
|
||||
const timeout = 120*1000; // 2 minutes
|
||||
|
||||
function task () {
|
||||
DEBUG("Running task");
|
||||
purge();
|
||||
}
|
||||
|
||||
async function cleanup () {
|
||||
DEBUG("Running cleanup");
|
||||
await purge();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
task,
|
||||
timeout,
|
||||
cleanup
|
||||
};
|
||||
Reference in New Issue
Block a user