mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:27:07 +00:00
45 lines
1005 B
JavaScript
45 lines
1005 B
JavaScript
|
|
|
||
|
|
const getPayloads = require('./payloads');
|
||
|
|
const despatchPayload = require('./despatch');
|
||
|
|
const {fetchItems, markFailed, markSent, rescheduleFailed} = require('./db');
|
||
|
|
|
||
|
|
|
||
|
|
function passed (result) {
|
||
|
|
return "id" in result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Process the queue.
|
||
|
|
*
|
||
|
|
* Try to send up to a certain number of
|
||
|
|
* items from the queue.
|
||
|
|
* Reschedule any failed items.
|
||
|
|
*/
|
||
|
|
async function processQueue () {
|
||
|
|
|
||
|
|
const items = await fetchItems({status: "queued"});
|
||
|
|
for (const item of items) {
|
||
|
|
const payloads = await getPayloads(item, (digestInfo) => {item.digest = digestInfo});
|
||
|
|
const results = [];
|
||
|
|
|
||
|
|
for (const {payload, digest} of payloads) {
|
||
|
|
const response = await despatchPayload(payload);
|
||
|
|
results.push({response, digest});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (results.some(result => !passed(result.response))) {
|
||
|
|
await markFailed(item, results);
|
||
|
|
} else {
|
||
|
|
await markSent(item, results);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
await rescheduleFailed(items);
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = processQueue;
|
||
|
|
|
||
|
|
if (!module.parent) {
|
||
|
|
processQueue().then(() => process.exit());
|
||
|
|
}
|