Files
D. Berge 5c190e5554 Add ASAQC queue processor.
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.
2021-10-04 02:21:00 +02:00

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());
}