fix: resilient store — Redis/Mongo errors don't stop consumer

This commit is contained in:
2026-07-22 09:05:26 +04:00
parent 676ed97538
commit 9b5201332e
+7 -2
View File
@@ -41,6 +41,7 @@ async function getMongo() {
} }
async function store(event) { async function store(event) {
try {
const r = await getRedis(); const r = await getRedis();
await r.hSet("iot:latest", event.sensor_id, JSON.stringify({ await r.hSet("iot:latest", event.sensor_id, JSON.stringify({
value: event.value, unit: event.unit, location: event.location, timestamp: event.timestamp, value: event.value, unit: event.unit, location: event.location, timestamp: event.timestamp,
@@ -48,12 +49,16 @@ async function store(event) {
await r.hIncrBy("iot:counters", event.device_type, 1); await r.hIncrBy("iot:counters", event.device_type, 1);
await r.zAdd("iot:recent", [{ score: 0, value: JSON.stringify(event) }]); await r.zAdd("iot:recent", [{ score: 0, value: JSON.stringify(event) }]);
await r.zRemRangeByRank("iot:recent", 0, -1001); await r.zRemRangeByRank("iot:recent", 0, -1001);
} catch (e) { console.error("Redis error:", e.message); }
try {
const coll = await getMongo(); const coll = await getMongo();
await coll.insertOne({ await coll.insertOne({
event_id: event.event_id, sensor_id: event.sensor_id, event_id: event.event_id, sensor_id: event.sensor_id,
device_type: event.device_type, location: event.location, device_type: event.device_type, location: event.location,
value: event.value, unit: event.unit, timestamp: new Date(event.timestamp), value: event.value, unit: event.unit, timestamp: new Date(event.timestamp),
}); });
} catch (e) { console.error("Mongo error:", e.message); }
} }
async function start() { async function start() {
@@ -62,16 +67,16 @@ async function start() {
await ch.assertQueue(RMQ_QUEUE, { durable: true }); await ch.assertQueue(RMQ_QUEUE, { durable: true });
ch.prefetch(10); ch.prefetch(10);
await ch.consume(RMQ_QUEUE, async (msg) => { await ch.consume(RMQ_QUEUE, async (msg) => {
if (!msg) return;
try { try {
if (msg) {
const event = JSON.parse(msg.content.toString()); const event = JSON.parse(msg.content.toString());
await store(event); await store(event);
consumed++; consumed++;
ch.ack(msg); ch.ack(msg);
if (consumed % 10 === 0) console.log(`Consumed ${consumed} events`); if (consumed % 10 === 0) console.log(`Consumed ${consumed} events`);
}
} catch (e) { } catch (e) {
errors++; errors++;
ch.nack(msg, false, true);
console.error("Error:", e.message); console.error("Error:", e.message);
} }
}); });