forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warmup-remotejson.js
executable file
·71 lines (63 loc) · 2.1 KB
/
warmup-remotejson.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
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
// [start-readme]
//
// This calls a function directly that is used by our archived enterprise
// middleware. Namely, the `getRemoteJSON` function. That function is
// able to use the disk to cache responses quite aggressively. So when
// it's been run once, with the same disk, next time it can draw from disk
// rather than having to rely on network.
//
// We have this script to avoid excessive network fetches in production
// where, due to production deploys restarting new Node services, we
// can't rely on in-memory caching often enough.
//
// The list of URLs hardcoded in here is based on analyzing the URLs that
// were logged as tags in Datadog for entries that couldn't rely on
// in-memory cache.
//
// [end-readme]
import { program } from 'commander'
import semver from 'semver'
import getRemoteJSON from '../middleware/get-remote-json.js'
import {
deprecated,
firstReleaseStoredInBlobStorage,
lastVersionWithoutArchivedRedirectsFile,
} from '../lib/enterprise-server-releases.js'
program
.description(
"Visit a bunch of archived redirects.json URLs to warm up getRemoteJSON's disk cache",
)
.parse(process.argv)
main()
function version2url(version) {
const inBlobStorage = semver.gte(
semver.coerce(version).raw,
semver.coerce(firstReleaseStoredInBlobStorage).raw,
)
return inBlobStorage
? `https://githubdocs.azureedge.net/enterprise/${version}/redirects.json`
: `https://github.github.com/help-docs-archived-enterprise-versions/${version}/redirects.json`
}
function withArchivedRedirectsFile(version) {
return semver.eq(
semver.coerce(version).raw,
semver.coerce(lastVersionWithoutArchivedRedirectsFile).raw,
)
}
async function main() {
const urls = []
for (const version of deprecated) {
if (withArchivedRedirectsFile(version)) {
break
}
urls.push(version2url(version))
}
const config = {
retry: { limit: 3 },
timeout: { response: 1000 },
}
console.time(`Time to fetch ${urls.length} URLs`)
await Promise.all(urls.map((url) => getRemoteJSON(url, config)))
console.timeEnd(`Time to fetch ${urls.length} URLs`)
}