|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// [start-readme] |
| 4 | +// |
| 5 | +// This calls a function directly that is used by our archived enterprise |
| 6 | +// middleware. Namely, the `getRemoteJSON` function. That function is |
| 7 | +// able to use the disk to cache responses quite aggressively. So when |
| 8 | +// it's been run once, with the same disk, next time it can draw from disk |
| 9 | +// rather than having to rely on network. |
| 10 | +// |
| 11 | +// We have this script to avoid excessive network fetches in production |
| 12 | +// where, due to production deploys restarting new Node services, we |
| 13 | +// can't rely on in-memory caching often enough. |
| 14 | +// |
| 15 | +// The list of URLs hardcoded in here is based on analyzing the URLs that |
| 16 | +// were logged as tags in Datadog for entries that couldn't rely on |
| 17 | +// in-memory cache. |
| 18 | +// |
| 19 | +// [end-readme] |
| 20 | + |
| 21 | +import { program } from 'commander' |
| 22 | +import semver from 'semver' |
| 23 | + |
| 24 | +import getRemoteJSON from '../middleware/get-remote-json.js' |
| 25 | +import { |
| 26 | + deprecated, |
| 27 | + firstReleaseStoredInBlobStorage, |
| 28 | + lastVersionWithoutArchivedRedirectsFile, |
| 29 | +} from '../lib/enterprise-server-releases.js' |
| 30 | + |
| 31 | +program |
| 32 | + .description( |
| 33 | + "Visit a bunch of archived redirects.json URLs to warm up getRemoteJSON's disk cache" |
| 34 | + ) |
| 35 | + .parse(process.argv) |
| 36 | + |
| 37 | +main() |
| 38 | + |
| 39 | +function version2url(version) { |
| 40 | + const inBlobStorage = semver.gte( |
| 41 | + semver.coerce(version).raw, |
| 42 | + semver.coerce(firstReleaseStoredInBlobStorage).raw |
| 43 | + ) |
| 44 | + return inBlobStorage |
| 45 | + ? `https://githubdocs.azureedge.net/enterprise/${version}/redirects.json` |
| 46 | + : `https://github.github.com/help-docs-archived-enterprise-versions/${version}/redirects.json` |
| 47 | +} |
| 48 | + |
| 49 | +function withArchivedRedirectsFile(version) { |
| 50 | + return semver.eq( |
| 51 | + semver.coerce(version).raw, |
| 52 | + semver.coerce(lastVersionWithoutArchivedRedirectsFile).raw |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +async function main() { |
| 57 | + const urls = [] |
| 58 | + for (const version of deprecated) { |
| 59 | + if (withArchivedRedirectsFile(version)) { |
| 60 | + break |
| 61 | + } |
| 62 | + urls.push(version2url(version)) |
| 63 | + } |
| 64 | + const config = { |
| 65 | + retry: { limit: 3 }, |
| 66 | + timeout: { response: 1000 }, |
| 67 | + } |
| 68 | + console.time(`Time to fetch ${urls.length} URLs`) |
| 69 | + await Promise.all(urls.map((url) => getRemoteJSON(url, config))) |
| 70 | + console.timeEnd(`Time to fetch ${urls.length} URLs`) |
| 71 | +} |
0 commit comments