mark messages as failed if they timeout

This commit is contained in:
liamcottle
2025-02-13 16:16:00 +13:00
parent e4ba9b6609
commit b62f45d150
3 changed files with 45 additions and 4 deletions

View File

@@ -112,7 +112,7 @@ class Message {
// find one latest message by ack code
// this will prevent updating older messages that might have the same ack code
const latestMessageByPacketId = database.messages.findOne({
const message = database.messages.findOne({
selector: {
expected_ack_crc: {
$eq: ackCode,
@@ -126,12 +126,48 @@ class Message {
});
// patch the message state
return await latestMessageByPacketId.incrementalPatch({
return await message.incrementalPatch({
status: "delivered",
});
}
// mark a message as failed by its ack code
static async setMessageFailedByAckCode(ackCode, reason) {
// find one latest message by ack code
// this will prevent updating older messages that might have the same ack code
const message = await database.messages.findOne({
selector: {
expected_ack_crc: {
$eq: ackCode,
},
},
sort: [
{
timestamp: "desc",
},
],
}).exec();
// do nothing if message not found
if(!message){
return;
}
// do nothing if already delivered
if(message.status === "delivered"){
return;
}
// patch the message state
await message.patch({
status: "failed",
error: reason,
});
}
// get all messages
static getAllMessages() {
return database.messages.find();