Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retries for webhook, test reduce interval #12

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading