Skip to content

Commit

Permalink
Fix stale bridge notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Jul 26, 2024
1 parent 18175bd commit 9ddd4c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
22 changes: 8 additions & 14 deletions src/handlers/checkStaleBridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,29 @@ const checkStaleBridges = async () => {

console.log("Stale bridges report (3+ days stale):");
let hasStaleEntries = false;
let discordMessage = "Bridges stale for more than 3 days:\n";
let discordMessage = "Bridges stale for 3+ days:\n";

for (const [bridgeName, chains] of Object.entries(result)) {
let bridgeHasStaleEntries = false;
let bridgeMessage = `\nBridge: ${bridgeName}\n`;
let staleChains = [];

for (const [chain, staleDays] of Object.entries(chains)) {
if (staleDays >= 3 || staleDays === -1) {
if (staleDays >= 3) {
hasStaleEntries = true;
bridgeHasStaleEntries = true;
const statusMessage = staleDays === -1 ? "Error occurred" : `${staleDays} days stale`;
bridgeMessage += ` ${chain}: ${statusMessage}\n`;

if (staleDays > 3) {
discordMessage += `${bridgeName} on ${chain}: ${staleDays} days\n`;
}
staleChains.push(chain);
}
}

if (bridgeHasStaleEntries) {
console.log(bridgeMessage);
if (staleChains.length > 0) {
discordMessage += `${bridgeName}: [${staleChains.join(", ")}]\n`;
}
}

if (!hasStaleEntries) {
console.log("No bridges are 3 or more days stale.");
discordMessage = "No bridges are stale for 3+ days.";
}

if (discordMessage !== "Bridges stale for more than 3 days:\n") {
if (discordMessage !== "Bridges stale for 3+ days:\n") {
try {
await sendDiscordText(discordMessage);
console.log("Discord message prepared (not sent):", discordMessage);
Expand Down
17 changes: 12 additions & 5 deletions src/utils/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@ import fetch from "node-fetch";

export async function sendDiscordText(message: string): Promise<void> {
try {
if (message.length > 2000) {
console.warn("Discord message exceeds 2000 characters. Truncating...");
message = message.substring(0, 1997) + "...";
}

const res = await fetch(process.env.DISCORD_WEBHOOK as string, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: message }),
});

if (!res.ok) {
console.error("Failed to send message to Discord:", res.statusText);
const errorText = await res.text();
console.error(`Failed to send message to Discord: Status ${res.status} ${res.statusText}`);
console.error("Error details:", errorText);
}
if (res.ok) {
console.log("Message sent to Discord:", message);
}
} catch (error) {

console.log("Message sent to Discord successfully");
} catch (error: any) {
console.error("Failed to send message to Discord:", error);
}
}

0 comments on commit 9ddd4c3

Please sign in to comment.