Skip to content

Commit

Permalink
retries for webhook, test reduce interval (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggnine-jito authored Jan 9, 2024
1 parent 4327f71 commit 69e666b
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions scripts/governance-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ import { formatNumber } from '@utils/formatNumber'
const fiveMinutesSeconds = 5 * 60
const toleranceSeconds = 30

const maxRetries = 3
const retryDelay = 5000

async function sendWebhook(webhookUrl, data, retries = maxRetries) {
try {
await axios.post(webhookUrl, data)
console.log('Webhook Triggered Successfully')
} catch (error) {
console.error('Webhook Trigger Failed:', error.message)
if (retries > 0) {
console.log(`Retrying... Attempts left: ${retries}`)
await new Promise((resolve) => setTimeout(resolve, retryDelay))
await sendWebhook(webhookUrl, data, retries - 1)
} else {
console.error('All retries failed')
}
}
}

if (!process.env.MAINNET_RPC) {
console.error('Please set MAINNET_RPC to a rpc node of choice!')
process.exit(1)
Expand Down Expand Up @@ -125,7 +144,9 @@ export async function runNotifier() {

console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
sendWebhook(process.env.WEBHOOK_URL, {
content: msg,
})
webhookTriggered = true
}
}
Expand Down Expand Up @@ -160,7 +181,9 @@ export async function runNotifier() {

console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
sendWebhook(process.env.WEBHOOK_URL, {
content: msg,
})
webhookTriggered = true
}
}
Expand Down Expand Up @@ -198,23 +221,22 @@ export async function runNotifier() {

console.log(msg)
if (process.env.WEBHOOK_URL) {
axios.post(process.env.WEBHOOK_URL, { content: msg })
sendWebhook(process.env.WEBHOOK_URL, {
content: msg,
})
webhookTriggered = true
}
}
}
}

const summary = `countOpenForVotingSinceSomeTime: ${countOpenForVotingSinceSomeTime}, countJustOpenedForVoting: ${countJustOpenedForVoting}, countVotingNotStartedYet: ${countVotingNotStartedYet}, countClosed: ${countClosed}, countCancelled: ${countCancelled}`

if (!webhookTriggered && process.env.WEBHOOK_URL) {
console.log('Nothing urgent to Report')
axios
.post(process.env.WEBHOOK_URL, {
content: 'Nothing urgent to Report: ' + summary,
})
.then(() => {
console.log('Nothing Webhook Triggered')
})
sendWebhook(process.env.WEBHOOK_URL, {
content: 'Nothing urgent to Report: ' + summary,
})
}

console.log(summary)
Expand Down

0 comments on commit 69e666b

Please sign in to comment.