const { refreshApiSource, refreshRssFeed } = require('../../data-source-refresh'); const TASK = { key: 'startup-data-source-refresh', category: 'data-source', }; function scheduleStartupDataSourceRefreshes(options) { const backgroundTaskQueue = options && options.backgroundTaskQueue; const pool = options && options.pool; const common = options && options.common; const dataSourceStartupRefreshStaggerMs = Math.max(100, Number(options && options.dataSourceStartupRefreshStaggerMs || 250)); if (!backgroundTaskQueue || !pool || !common) { throw new Error('scheduleStartupDataSourceRefreshes requires the startup refresh dependencies.'); } const staggerMs = Math.max(100, Number(dataSourceStartupRefreshStaggerMs || 250)); function buildStartupSource(type, id, name, run) { return { type: type, id: Number(id), name: name, run: run }; } return (async function () { const apiSourcesData = await common.fetchApiSourcesData(pool); const rssFeedsData = await common.fetchRssFeedsData(pool); const startupSources = []; (apiSourcesData.apiSources || []).forEach(function (apiSource) { startupSources.push(buildStartupSource('api-source', apiSource.id, apiSource.name, function () { return refreshApiSource(pool, common, apiSource, null); })); }); (rssFeedsData.rssFeeds || []).forEach(function (rssFeed) { startupSources.push(buildStartupSource('rss-feed', rssFeed.id, rssFeed.name, function () { return refreshRssFeed(pool, common, rssFeed.id, rssFeed.feed_url, rssFeed.item_limit, null); })); }); // Stagger startup refreshes to avoid a burst against the DB/player. startupSources.forEach(function (source, index) { const startupDelayMs = index * staggerMs; setTimeout(function () { backgroundTaskQueue.enqueueTask({ key: TASK.key + ':' + source.type + ':' + source.id + ':' + Date.now(), title: source.type === 'rss-feed' ? 'RSS feed refresh' : 'API source refresh', category: TASK.category, taskType: 'data-source-refresh', metadata: { sourceType: source.type, sourceId: source.id, sourceName: source.name, startupRefresh: true }, payload: { sourceType: source.type, sourceId: source.id, sourceName: source.name, startupRefresh: true } }).catch(function (error) { console.warn('Unable to queue startup refresh for ' + source.type + ' ' + source.id + ':', error); }); }, startupDelayMs); }); })(); } module.exports = { scheduleStartupDataSourceRefreshes };