mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 11:37:08 +00:00
This code implements the backend processing side of the ASAQC queue, i.e., the bit that communicates with the remote API. Its expected use it to have it running at regular intervals, e.g., via cron. The entry point is: lib/www/server/queues/asaqc/index.js That file is executable and can be run directly from the shell or within a script. Read the comments in that file for further instructions.
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());
|
|
}
|