Skip to content

Commit 781b854

Browse files
committed
Space out retries before channel alert
1 parent 21b053d commit 781b854

1 file changed

Lines changed: 50 additions & 28 deletions

File tree

src/events/common.ts

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class AppDiscord {
3434
DEV_URL: string
3535
DEV_API_URL: string
3636
private scheduledJobs: schedule.Job[] = []
37+
private urlFailureCount: Map<string, number> = new Map()
3738
private webhooks: {
3839
devWiki: string
3940
devHiiq: string
@@ -226,54 +227,75 @@ export class AppDiscord {
226227
}
227228
}
228229

230+
private async checkSingleUrl(
231+
url: string,
232+
timeout: number,
233+
): Promise<{ url: string; status: number | string; success: boolean; error?: string }> {
234+
try {
235+
const response = await axios.get(url, {
236+
timeout,
237+
validateStatus: status => status < 500,
238+
})
239+
return { url, status: response.status, success: true }
240+
} catch (error: any) {
241+
const status = error.response?.status || 'TIMEOUT/NETWORK_ERROR'
242+
return { url, status, success: false, error: error.message }
243+
}
244+
}
245+
246+
private async sleep(ms: number): Promise<void> {
247+
return new Promise(resolve => setTimeout(resolve, ms))
248+
}
249+
229250
async checkWebpageStatus(
230251
urls: string[],
231252
webhookUrl: string,
232253
): Promise<void> {
233254
console.log(`🔍 Checking status of ${urls.length} websites...`)
234255

235256
const timeout = 30000
236-
const results = await Promise.allSettled(
257+
const maxRetries = 3
258+
const retryDelay = 30000 // 30 seconds
259+
260+
const finalResults = await Promise.all(
237261
urls.map(async url => {
238-
try {
239-
const response = await axios.get(url, {
240-
timeout,
241-
validateStatus: status => status < 500,
242-
})
243-
return { url, status: response.status, success: true }
244-
} catch (error: any) {
245-
const status = error.response?.status || 'TIMEOUT/NETWORK_ERROR'
246-
console.error(`❌ ${url} failed:`, status)
247-
return { url, status, success: false, error: error.message }
262+
let lastResult = await this.checkSingleUrl(url, timeout)
263+
264+
// Retry up to 3 times with 30 second delays if failed
265+
for (let attempt = 1; attempt < maxRetries && !lastResult.success; attempt++) {
266+
console.log(`⏳ ${url} failed (attempt ${attempt}/${maxRetries}), retrying in 30 seconds...`)
267+
await this.sleep(retryDelay)
268+
lastResult = await this.checkSingleUrl(url, timeout)
248269
}
249-
}),
250-
)
251270

252-
const processedResults = results.map((result, index) => {
253-
if (result.status === 'fulfilled') {
254-
return result.value
255-
} else {
256-
console.error(`❌ Promise rejected for ${urls[index]}:`, result.reason)
257-
return {
258-
url: urls[index],
259-
status: 'PROMISE_REJECTED',
260-
success: false,
261-
error: result.reason,
271+
if (!lastResult.success) {
272+
console.error(`❌ ${url} failed after ${maxRetries} attempts:`, lastResult.status)
262273
}
263-
}
264-
})
265274

266-
const failedResults = processedResults.filter(result => !result.success)
267-
const successfulResults = processedResults.filter(result => result.success)
275+
return lastResult
276+
}),
277+
)
278+
279+
const failedResults = finalResults.filter(result => !result.success)
280+
const successfulResults = finalResults.filter(result => result.success)
268281

269282
console.log(`✅ ${successfulResults.length}/${urls.length} websites are up`)
270283

284+
// Reset failure count for successful URLs
285+
successfulResults.forEach(result => {
286+
if (this.urlFailureCount.has(result.url)) {
287+
console.log(`✅ ${result.url} is back online, resetting failure count`)
288+
this.urlFailureCount.delete(result.url)
289+
}
290+
})
291+
271292
if (failedResults.length > 0) {
272-
console.log(`🚧 ${failedResults.length} websites are down:`)
293+
console.log(`🚧 ${failedResults.length} websites are down after ${maxRetries} retry attempts:`)
273294

274295
const notifications = failedResults.map(async result => {
275296
if (result) {
276297
console.log(` - ${result.url}: ${result.status}`)
298+
console.log(`🚨 Sending alert for ${result.url} after ${maxRetries} failed attempts`)
277299
await this.updates.sendUpdates({
278300
webhookUrl: webhookUrl,
279301
channelType: ChannelTypes.PROD,

0 commit comments

Comments
 (0)