fix: resilient store — Redis/Mongo errors don't stop consumer
This commit is contained in:
@@ -41,19 +41,24 @@ async function getMongo() {
|
||||
}
|
||||
|
||||
async function store(event) {
|
||||
const r = await getRedis();
|
||||
await r.hSet("iot:latest", event.sensor_id, JSON.stringify({
|
||||
value: event.value, unit: event.unit, location: event.location, timestamp: event.timestamp,
|
||||
}));
|
||||
await r.hIncrBy("iot:counters", event.device_type, 1);
|
||||
await r.zAdd("iot:recent", [{ score: 0, value: JSON.stringify(event) }]);
|
||||
await r.zRemRangeByRank("iot:recent", 0, -1001);
|
||||
const coll = await getMongo();
|
||||
await coll.insertOne({
|
||||
event_id: event.event_id, sensor_id: event.sensor_id,
|
||||
device_type: event.device_type, location: event.location,
|
||||
value: event.value, unit: event.unit, timestamp: new Date(event.timestamp),
|
||||
});
|
||||
try {
|
||||
const r = await getRedis();
|
||||
await r.hSet("iot:latest", event.sensor_id, JSON.stringify({
|
||||
value: event.value, unit: event.unit, location: event.location, timestamp: event.timestamp,
|
||||
}));
|
||||
await r.hIncrBy("iot:counters", event.device_type, 1);
|
||||
await r.zAdd("iot:recent", [{ score: 0, value: JSON.stringify(event) }]);
|
||||
await r.zRemRangeByRank("iot:recent", 0, -1001);
|
||||
} catch (e) { console.error("Redis error:", e.message); }
|
||||
|
||||
try {
|
||||
const coll = await getMongo();
|
||||
await coll.insertOne({
|
||||
event_id: event.event_id, sensor_id: event.sensor_id,
|
||||
device_type: event.device_type, location: event.location,
|
||||
value: event.value, unit: event.unit, timestamp: new Date(event.timestamp),
|
||||
});
|
||||
} catch (e) { console.error("Mongo error:", e.message); }
|
||||
}
|
||||
|
||||
async function start() {
|
||||
@@ -62,16 +67,16 @@ async function start() {
|
||||
await ch.assertQueue(RMQ_QUEUE, { durable: true });
|
||||
ch.prefetch(10);
|
||||
await ch.consume(RMQ_QUEUE, async (msg) => {
|
||||
if (!msg) return;
|
||||
try {
|
||||
if (msg) {
|
||||
const event = JSON.parse(msg.content.toString());
|
||||
await store(event);
|
||||
consumed++;
|
||||
ch.ack(msg);
|
||||
if (consumed % 10 === 0) console.log(`Consumed ${consumed} events`);
|
||||
}
|
||||
const event = JSON.parse(msg.content.toString());
|
||||
await store(event);
|
||||
consumed++;
|
||||
ch.ack(msg);
|
||||
if (consumed % 10 === 0) console.log(`Consumed ${consumed} events`);
|
||||
} catch (e) {
|
||||
errors++;
|
||||
ch.nack(msg, false, true);
|
||||
console.error("Error:", e.message);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user