Files
pulse-signage/src/web/lib/background-tasks/index.js
T
2026-08-01 21:41:44 +01:00

99 lines
3.3 KiB
JavaScript

// Shared loader for task folders under background-tasks.
const fs = require('fs');
const path = require('path');
function getTaskExport(taskModule) {
return typeof taskModule === 'function'
? taskModule
: taskModule && typeof taskModule === 'object'
? Object.values(taskModule).find(function (value) {
return typeof value === 'function';
})
: null;
}
function loadTaskModules(directory, options) {
const taskFiles = fs.readdirSync(directory)
.filter(function (fileName) {
return fileName.toLowerCase().endsWith('.js') && fileName !== 'index.js';
})
.sort(function (left, right) {
return left.localeCompare(right);
});
return Promise.all(taskFiles.map(function (fileName) {
const taskModule = require(path.join(directory, fileName));
const exported = getTaskExport(taskModule);
if (typeof exported === 'function') {
return exported(options);
}
return null;
}));
}
// Register ad hoc task handlers that execute queued background work.
function registerAdhocTasks(options) {
const backgroundTaskDirectory = path.join(__dirname, 'tasks-adhoc');
if (!options || !options.pool || !options.common || !options.backgroundTaskQueue || !options.captureSlideThumbnail || !options.uploadSyncService || !String(options.mediaDir || '').trim()) {
throw new Error('registerAdhocTasks requires the background task dependencies.');
}
loadTaskModules(backgroundTaskDirectory, options);
}
// Register one-time startup work that should run during boot only.
function registerStartupTasks(options) {
const backgroundTaskDirectory = path.join(__dirname, 'tasks-startup');
if (!options || !options.pool || !options.common || !options.backgroundTaskQueue || !String(options.mediaDir || '').trim()) {
throw new Error('registerStartupTasks requires the startup task dependencies.');
}
async function initialize() {
await loadTaskModules(backgroundTaskDirectory, options);
}
return {
initialize: initialize
};
}
// Register recurring maintenance and data-source refresh tasks.
function registerScheduledTasks(options) {
const backgroundTaskDirectory = path.join(__dirname, 'tasks-scheduled');
if (!options || !options.pool || !options.common || !options.backgroundTaskQueue || !options.uploadSyncService || !String(options.mediaDir || '').trim()) {
throw new Error('registerScheduledTasks requires the background task dependencies.');
}
async function initialize() {
await loadTaskModules(backgroundTaskDirectory, options);
}
return {
initialize: initialize
};
}
async function initializeBackgroundTasks(options) {
registerAdhocTasks(options);
const backgroundTaskStartup = registerStartupTasks(options);
if (backgroundTaskStartup && typeof backgroundTaskStartup.initialize === 'function') {
await backgroundTaskStartup.initialize();
}
const backgroundTaskScheduling = registerScheduledTasks(options);
if (backgroundTaskScheduling && typeof backgroundTaskScheduling.initialize === 'function') {
await backgroundTaskScheduling.initialize();
}
}
module.exports = {
registerAdhocTasks: registerAdhocTasks,
registerStartupTasks: registerStartupTasks,
registerScheduledTasks: registerScheduledTasks,
initializeBackgroundTasks: initializeBackgroundTasks
};