1
0
mirror of synced 2025-12-26 05:02:55 -05:00
Files
docs/lib/warm-server.js
James M. Greene fb30a0766f Unravel pages (the array + map object) (#16708)
* Revise the 'pages' module to export two methods: 'loadPages' and 'loadPageMap'

Update all existing references to use 'loadPages' for now

* Remove explicit Promise resolutions on loadPage* methods

* Condense reduction method into its now-singular usage spot

* Opt for for-of instead of forEach

* Make require of pages in warm-server more explicit

* Be more explicit about find-page using a pageMap

* Be more explicit about find-page-in-version using a pageMap

* Be more explicit about site-tree using a pageMap

* Extract the map creation from loadPageMap

* Be more explicit about using a pageMap

* Update redirects precompile to take two arguments: pageList, pageMap

* Rename internal loadPages method to loadPageList

* Clarify pageMap is what is stored in context.pages

* Use loadPageMap in tests and stuff
2020-12-03 22:31:10 +00:00

73 lines
1.8 KiB
JavaScript

const statsd = require('./statsd')
const { loadPages, loadPageMap } = require('./pages')
const loadRedirects = require('./redirects/precompile')
const loadSiteData = require('./site-data')
const loadSiteTree = require('./site-tree')
// For local caching
let pageList, pageMap, site, redirects, siteTree
function isFullyWarmed () {
// NOTE: Yes, `pageList` is specifically excluded here as it is transient data
const fullyWarmed = !!(pageMap && site && redirects && siteTree)
return fullyWarmed
}
function getWarmedCache () {
return {
pages: pageMap,
site,
redirects,
siteTree
}
}
async function warmServer () {
const startTime = Date.now()
if (process.env.NODE_ENV !== 'test') {
console.log('Priming context information...')
}
if (!pageList || !site) {
// Promise.all is used to load multiple things in parallel
[pageList, site] = await Promise.all([
pageList || loadPages(),
site || loadSiteData()
])
}
if (!pageMap) {
pageMap = await loadPageMap(pageList)
}
if (!redirects) {
redirects = await loadRedirects(pageList, pageMap)
}
if (!siteTree) {
siteTree = await loadSiteTree(pageMap, 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()
}