forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
failbot.js
59 lines (54 loc) · 1.38 KB
/
failbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import got from 'got'
import { Failbot, HTTPBackend } from '@github/failbot'
const HAYSTACK_APP = 'docs'
async function retryingGot(url, args) {
return got(
url,
Object.assign({}, args, {
// With the timeout at 3000 (milliseconds) and the retry.limit
// at 4 (times), the total worst-case is:
// 3000 * 4 + 1000 + 2000 + 3000 + 4000 + 8000 = 30 seconds
timeout: 3000,
retry: {
// This means it will wait...
// 1. 1000ms
// 2. 2000ms
// 3. 4000ms
// 4. 8000ms
// 5. give up!
//
// From the documentation:
//
// Delays between retries counts with function
// 1000 * Math.pow(2, retry - 1) + Math.random() * 100,
// where retry is attempt number (starts from 1).
//
limit: 4,
},
})
)
}
export async function report(error, metadata) {
// If there's no HAYSTACK_URL set, bail early
if (!process.env.HAYSTACK_URL) return
const backends = [
new HTTPBackend({
haystackURL: process.env.HAYSTACK_URL,
fetchFn: retryingGot,
}),
]
const failbot = new Failbot({
app: HAYSTACK_APP,
backends: backends,
})
return failbot.report(error, metadata)
}
// Kept for legacy so you can continue to do:
//
// import FailBot from './lib/failbot.js'
// ...
// FailBot.report(myError)
//
export default {
report,
}