Skip to content

Commit c74f030

Browse files
committed
[cisco-sg] fix stale lldp
1 parent ac793a7 commit c74f030

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

src/modules/cisco-sg/container/workers/worker-interfacelldp.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,47 @@ const main = async () => {
8080
}
8181
});
8282

83-
lldpByInterface.forEach(async (lldpObject, eachIndex) => {
84-
await interfacesCollection.updateOne(
85-
{ interfaceId: parseInt(eachIndex) },
86-
{
87-
$set: {
88-
lldp: lldpObject,
83+
const activeLldpEntries = lldpByInterface
84+
.map((lldpObject, eachIndex) => ({ lldpObject, eachIndex }))
85+
.filter(({ lldpObject }) => lldpObject && typeof lldpObject === "object");
86+
87+
const activeInterfaceIds = activeLldpEntries.map(({ eachIndex }) => parseInt(eachIndex));
88+
const clearFilter = {
89+
lldp: { $exists: true },
90+
};
91+
92+
if (activeInterfaceIds.length) {
93+
clearFilter.interfaceId = { $nin: activeInterfaceIds };
94+
}
95+
96+
const operations = [
97+
{
98+
updateMany: {
99+
filter: clearFilter,
100+
update: {
101+
$unset: {
102+
lldp: "",
103+
},
89104
},
90105
},
91-
{ upsert: false }
92-
);
93-
});
106+
},
107+
];
108+
109+
for (const { lldpObject, eachIndex } of activeLldpEntries) {
110+
operations.push({
111+
updateOne: {
112+
filter: { interfaceId: parseInt(eachIndex) },
113+
update: {
114+
$set: {
115+
lldp: lldpObject,
116+
},
117+
},
118+
upsert: false,
119+
},
120+
});
121+
}
122+
123+
await interfacesCollection.bulkWrite(operations, { ordered: false });
94124

95125
// every 30 seconds
96126
await delay(30000);

0 commit comments

Comments
 (0)