Skip to content

Commit 5f6994d

Browse files
authored
Instrument the warmServer method more precisely (#16698)
* Instrument the `warmServer` method more precisely This way we only report on it if we have to actually prime it vs. with every call to get the cached results * Skip the extra variable * Use Boolean to make intent more explicit
1 parent 7b3513d commit 5f6994d

1 file changed

Lines changed: 52 additions & 12 deletions

File tree

lib/warm-server.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,69 @@
11
const statsd = require('./statsd')
22
const fetchEarlyAccessPaths = require('./fetch-early-access-paths')
3+
const loadPages = require('./pages')
4+
const loadRedirects = require('./redirects/precompile')
5+
const loadSiteData = require('./site-data')
6+
const loadSiteTree = require('./site-tree')
7+
8+
// For local caching
39
let pages, site, redirects, siteTree, earlyAccessPaths
410

11+
function isFullyWarmed () {
12+
return Boolean(pages && site && earlyAccessPaths && redirects && siteTree)
13+
}
14+
15+
function getWarmedCache () {
16+
return {
17+
pages,
18+
site,
19+
redirects,
20+
siteTree,
21+
earlyAccessPaths
22+
}
23+
}
24+
525
async function warmServer () {
6-
if (!pages) {
7-
if (process.env.NODE_ENV !== 'test') {
8-
console.log('Priming context information')
9-
}
26+
const startTime = Date.now()
27+
28+
if (process.env.NODE_ENV !== 'test') {
29+
console.log('Priming context information...')
30+
}
1031

32+
if (!pages || !site || !earlyAccessPaths) {
1133
// Promise.all is used to load multiple things in parallel
1234
[pages, site, earlyAccessPaths] = await Promise.all([
13-
require('./pages')(),
14-
require('./site-data')(),
15-
fetchEarlyAccessPaths()
35+
pages || loadPages(),
36+
site || loadSiteData(),
37+
earlyAccessPaths || fetchEarlyAccessPaths()
1638
])
39+
}
1740

18-
redirects = await require('./redirects/precompile')(pages)
19-
siteTree = await require('./site-tree')(pages, site, redirects)
41+
if (!redirects) {
42+
redirects = await loadRedirects(pages)
2043
}
2144

22-
return {
23-
pages, site, redirects, siteTree, earlyAccessPaths
45+
if (!siteTree) {
46+
siteTree = await loadSiteTree(pages, site, redirects)
2447
}
48+
49+
if (process.env.NODE_ENV !== 'test') {
50+
console.log(`Context primed in ${Date.now() - startTime} ms`)
51+
}
52+
53+
return getWarmedCache()
2554
}
2655

2756
// Instrument the `warmServer` function so that
2857
// it's wrapped in a timer that reports to Datadog
29-
module.exports = statsd.asyncTimer(warmServer, 'warm_server')
58+
const instrumentedWarmServer = statsd.asyncTimer(warmServer, 'warm_server')
59+
60+
// We only want statistics if the priming needs to occur, so let's wrap the
61+
// real method and return early [without statistics] whenever possible
62+
module.exports = async function warmServerWrapper () {
63+
// Bail out early if everything is properly ready to use
64+
if (isFullyWarmed()) {
65+
return getWarmedCache()
66+
}
67+
68+
return instrumentedWarmServer()
69+
}

0 commit comments

Comments
 (0)