67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
async function refreshApiSource(pool, common, apiSourceId, apiUrl, actorId) {
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
let responseDetails = null;
|
|
let pullError = '';
|
|
|
|
try {
|
|
responseDetails = await common.fetchApiSourceResponse(apiUrl);
|
|
} catch (error) {
|
|
pullError = String(error && error.message ? error.message : 'Unable to load API response.');
|
|
}
|
|
|
|
await connection.beginTransaction();
|
|
await connection.query(
|
|
'UPDATE 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, apiSourceId]
|
|
);
|
|
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
|
|
}; |