Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const optionsList = [
{
name: "mqtt-broker-url",
type: String,
description: "MQTT Broker URL (e.g: mqtt://mqtt.meshtastic.org)",
description: "MQTT Broker URL (e.g: mqtt://103.197.58.223)",
},
{
name: "mqtt-username",
Expand Down Expand Up @@ -213,7 +213,7 @@ if(options.help){

// get options and fallback to default values
const protobufsPath = options["protobufs-path"] ?? path.join(path.dirname(__filename), "external/protobufs");
const mqttBrokerUrl = options["mqtt-broker-url"] ?? "mqtt://mqtt.meshtastic.org";
const mqttBrokerUrl = options["mqtt-broker-url"] ?? "mqtt://103.197.58.223:1883";
const mqttUsername = options["mqtt-username"] ?? "meshdev";
const mqttPassword = options["mqtt-password"] ?? "large4cats";
const mqttClientId = options["mqtt-client-id"] ?? null;
Expand Down Expand Up @@ -948,8 +948,14 @@ client.on("message", async (topic, message) => {
});
}

let attempt = 0;
while (attempt < maxRetries) {
attempt += 1;

// create or update node in db
try {
await prisma.$transaction(
async (tx) => {
await prisma.node.upsert({
where: {
node_id: envelope.packet.from,
Expand All @@ -969,11 +975,28 @@ client.on("message", async (topic, message) => {
is_licensed: user.isLicensed === true,
role: user.role,
},
});
});
},
{
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
maxWait: 5000,
timeout: 10000,
}
);
return; // Success—exit
} catch (e) {
console.error(e);
if (
e instanceof Prisma.PrismaClientKnownRequestError &&
(e.code === 'P2002' || e.code === 'P2034')
) {
console.warn(`Attempt ${attempt} failed (${e.code}). Retrying...`); continue; // Retry
}
// Other errors => abort
throw e;
}

}
throw new Error(`safeUpsertNode failed after ${maxRetries} attempts`);
}

else if(portnum === 8) {
Expand Down Expand Up @@ -1253,9 +1276,9 @@ client.on("message", async (topic, message) => {
});
} catch (e) {
console.error(e);
}
}

}
}
throw new Error(`safeUpsertNode failed after ${maxRetries} attempts`);
}

else if(portnum === 70) {
Expand Down Expand Up @@ -1414,3 +1437,4 @@ client.on("message", async (topic, message) => {
// ignore errors
}
});