76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
async function refreshApiSource(pool, common, apiSourceOrId, actorId) {
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
const apiSource = apiSourceOrId && typeof apiSourceOrId === 'object'
|
|
? apiSourceOrId
|
|
: typeof common.fetchApiSourceById === 'function'
|
|
? await common.fetchApiSourceById(pool, Number(apiSourceOrId))
|
|
: null;
|
|
if (!apiSource) {
|
|
throw new Error('API source not found.');
|
|
}
|
|
|
|
let responseDetails = null;
|
|
let pullError = '';
|
|
|
|
try {
|
|
responseDetails = await common.fetchApiSourceResponse(apiSource);
|
|
} catch (error) {
|
|
pullError = String(error && error.message ? error.message : 'Unable to load API response.');
|
|
}
|
|
|
|
await connection.beginTransaction();
|
|
await connection.query(
|
|
'UPDATE i_api_sources SET last_pulled_at = ?, last_pull_error = ?, last_response_status = ?, last_response_content_type = ?, last_response_json = ?, modified_by = ? WHERE id = ?',
|
|
[new Date(), pullError || null, responseDetails ? responseDetails.responseStatus : null, responseDetails ? responseDetails.responseContentType : null, responseDetails ? responseDetails.responseJson : null, actorId, apiSource.id]
|
|
);
|
|
await connection.commit();
|
|
} catch (error) {
|
|
try {
|
|
await connection.rollback();
|
|
} catch (_rollbackError) {
|
|
// Ignore rollback failures and surface the original error.
|
|
}
|
|
throw error;
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
}
|
|
|
|
async function refreshRssFeed(pool, common, rssFeedId, feedUrl, itemLimit, actorId) {
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
let updatedItems = [];
|
|
let pullError = '';
|
|
|
|
try {
|
|
updatedItems = await common.fetchRssFeedItems(feedUrl, itemLimit);
|
|
} catch (error) {
|
|
pullError = String(error && error.message ? error.message : 'Unable to load feed items.');
|
|
}
|
|
|
|
await connection.beginTransaction();
|
|
if (typeof common.replaceRssFeedItems === 'function') {
|
|
await common.replaceRssFeedItems(connection, rssFeedId, updatedItems);
|
|
}
|
|
await connection.commit();
|
|
|
|
if (pullError) {
|
|
console.error('[data-source-refresh] RSS feed refresh completed with an error for feed ' + rssFeedId + ': ' + pullError);
|
|
}
|
|
} catch (error) {
|
|
try {
|
|
await connection.rollback();
|
|
} catch (_rollbackError) {
|
|
// Ignore rollback failures and surface the original error.
|
|
}
|
|
throw error;
|
|
} finally {
|
|
connection.release();
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
refreshApiSource: refreshApiSource,
|
|
refreshRssFeed: refreshRssFeed
|
|
}; |