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

@@ -33,7 +33,7 @@
<!-- state label --> <!-- state label -->
<div class="my-auto"> <div class="my-auto">
<span v-if="isMessageFailed(message)">Failed</span> <span v-if="isMessageFailed(message)">Failed: {{ message.error }}</span>
<span v-else-if="isMessageDelivered(message)">Delivered</span> <span v-else-if="isMessageDelivered(message)">Delivered</span>
<span v-else>Sending</span> <span v-else>Sending</span>
</div> </div>

View File

@@ -175,8 +175,13 @@ class Connection {
// send message // send message
const message = await GlobalState.connection.sendTextMessage(publicKey, text); const message = await GlobalState.connection.sendTextMessage(publicKey, text);
// mark message as failed after estimated timeout
setTimeout(async () => {
await Database.Message.setMessageFailedByAckCode(message.expectedAckCrc, "timeout");
}, message.estTimeout);
// save to database // save to database
return await Database.Message.insert({ await Database.Message.insert({
status: "sending", status: "sending",
to: publicKey, to: publicKey,
from: GlobalState.selfInfo.publicKey, from: GlobalState.selfInfo.publicKey,

View File

@@ -112,7 +112,7 @@ class Message {
// find one latest message by ack code // find one latest message by ack code
// this will prevent updating older messages that might have the same 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: { selector: {
expected_ack_crc: { expected_ack_crc: {
$eq: ackCode, $eq: ackCode,
@@ -126,12 +126,48 @@ class Message {
}); });
// patch the message state // patch the message state
return await latestMessageByPacketId.incrementalPatch({ return await message.incrementalPatch({
status: "delivered", 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 // get all messages
static getAllMessages() { static getAllMessages() {
return database.messages.find(); return database.messages.find();