1
0
mirror of synced 2025-12-26 14:02:45 -05:00
Files
docs/lib/warm-server.js
James M. Greene 5f6994d001 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
2020-12-01 19:05:24 -06:00

70 lines
1.8 KiB
JavaScript

const statsd = require('./statsd')
const fetchEarlyAccessPaths = require('./fetch-early-access-paths')
const loadPages = require('./pages')
const loadRedirects = require('./redirects/precompile')
const loadSiteData = require('./site-data')
const loadSiteTree = require('./site-tree')
// For local caching
let pages, site, redirects, siteTree, earlyAccessPaths
function isFullyWarmed () {
return Boolean(pages && site && earlyAccessPaths && redirects && siteTree)
}
function getWarmedCache () {
return {
pages,
site,
redirects,
siteTree,
earlyAccessPaths
}
}
async function warmServer () {
const startTime = Date.now()
if (process.env.NODE_ENV !== 'test') {
console.log('Priming context information...')
}
if (!pages || !site || !earlyAccessPaths) {
// Promise.all is used to load multiple things in parallel
[pages, site, earlyAccessPaths] = await Promise.all([
pages || loadPages(),
site || loadSiteData(),
earlyAccessPaths || fetchEarlyAccessPaths()
])
}
if (!redirects) {
redirects = await loadRedirects(pages)
}
if (!siteTree) {
siteTree = await loadSiteTree(pages, site, redirects)
}
if (process.env.NODE_ENV !== 'test') {
console.log(`Context primed in ${Date.now() - startTime} ms`)
}
return getWarmedCache()
}
// Instrument the `warmServer` function so that
// it's wrapped in a timer that reports to Datadog
const instrumentedWarmServer = statsd.asyncTimer(warmServer, 'warm_server')
// We only want statistics if the priming needs to occur, so let's wrap the
// real method and return early [without statistics] whenever possible
module.exports = async function warmServerWrapper () {
// Bail out early if everything is properly ready to use
if (isFullyWarmed()) {
return getWarmedCache()
}
return instrumentedWarmServer()
}