Add automatic retry to fetching product text for filtering

This commit is contained in:
Christopher Cookman 2024-05-22 13:48:14 -06:00
parent 9393bf8f94
commit a31573b7c4
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -421,6 +421,7 @@ xmpp.on("stanza", (stanza) => {
if (!channel) return console.log(`${colors.red("[ERROR]")} Channel ${row.channelid} not found`); if (!channel) return console.log(`${colors.red("[ERROR]")} Channel ${row.channelid} not found`);
// fetch the product text // fetch the product text
trySend = () => {
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => { fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
// If neither the body nor the product text contains the filter, ignore it // If neither the body nor the product text contains the filter, ignore it
res.text().then((text) => { res.text().then((text) => {
@ -433,7 +434,13 @@ xmpp.on("stanza", (stanza) => {
if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost(); if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost();
}); });
}); });
}).catch((err) => {
setTimeout(() => {
console.log(`${colors.red("[ERROR]")} Failed to fetch product text, retrying... ${err}`)
trySend();
})
}); });
}
}); });
}); });
@ -460,6 +467,7 @@ xmpp.on("stanza", (stanza) => {
if (!user) return console.log(`${colors.red("[ERROR]")} User ${row.userid} not found`); if (!user) return console.log(`${colors.red("[ERROR]")} User ${row.userid} not found`);
// fetch the product text // fetch the product text
trySend = () => {
fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => { fetch(`https://mesonet.agron.iastate.edu/api/1/nwstext/${product_id_raw}`).then((res) => {
// If neither the body nor the product text contains the filter, ignore it // If neither the body nor the product text contains the filter, ignore it
res.text().then((text) => { res.text().then((text) => {
@ -470,8 +478,13 @@ xmpp.on("stanza", (stanza) => {
console.error(err); console.error(err);
}); });
}); });
}); }).catch((err) => {
setTimeout(() => {
console.log(`${colors.red("[ERROR]")} Failed to fetch product text, retrying... ${err}`)
trySend();
})
});;
}
}); });
}); });
} }