Files
pulse-signage/src/web/lib/background-tasks/tasks-adhoc/data-source-refresh.js
T
2026-08-28 02:53:59 +01:00

53 lines
2.3 KiB
JavaScript

const { refreshApiSource, refreshRssFeed, refreshWeatherLocation } = require('../../data-source-refresh');
const TASK = {
taskType: 'data-source-refresh'
};
function registerDataSourceRefreshTask(options) {
const backgroundTaskQueue = options && options.backgroundTaskQueue;
const pool = options && options.pool;
const common = options && options.common;
if (!backgroundTaskQueue || !pool || !common) {
throw new Error('registerDataSourceRefreshTask requires the data source refresh dependencies.');
}
backgroundTaskQueue.setTaskHandler(TASK.taskType, async function (task) {
const payload = task && task.payload ? task.payload : {};
const sourceType = String(payload.sourceType || '').trim();
const sourceId = Number(payload.sourceId || 0);
// Resolve the source record at execution time so stale queued tasks fail cleanly.
if (sourceType === 'api-source') {
const apiSource = await common.fetchApiSourceById(pool, sourceId);
if (!apiSource) {
throw new Error('API source not found.');
}
if (apiSource.enabled === 0 || apiSource.enabled === false) return { skipped: true, reason: 'disabled' };
return refreshApiSource(pool, common, apiSource, Number(payload.actorId) || null, options.notifyPlayerScreens);
}
if (sourceType === 'rss-feed') {
const rssFeed = await common.fetchRssFeedById(pool, sourceId);
if (!rssFeed) {
throw new Error('RSS feed not found.');
}
if (rssFeed.enabled === 0 || rssFeed.enabled === false) return { skipped: true, reason: 'disabled' };
return refreshRssFeed(pool, common, rssFeed.id, rssFeed.feed_url, rssFeed.item_limit, Number(payload.actorId) || null, options.notifyPlayerScreens);
}
if (sourceType === 'weather-location') {
const location = await common.fetchWeatherLocationById(pool, sourceId);
if (!location) {
throw new Error('Weather location not found.');
}
if (location.enabled === 0 || location.enabled === false) return { skipped: true, reason: 'disabled' };
return refreshWeatherLocation(pool, common, location, Number(payload.actorId) || null, options.notifyPlayerScreens);
}
throw new Error('Unsupported data source refresh task.');
});
}
module.exports = { registerDataSourceRefreshTask };