|
1 | 1 | const statsd = require('./statsd') |
2 | 2 | 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 |
3 | 9 | let pages, site, redirects, siteTree, earlyAccessPaths |
4 | 10 |
|
| 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 | + |
5 | 25 | 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 | + } |
10 | 31 |
|
| 32 | + if (!pages || !site || !earlyAccessPaths) { |
11 | 33 | // Promise.all is used to load multiple things in parallel |
12 | 34 | [pages, site, earlyAccessPaths] = await Promise.all([ |
13 | | - require('./pages')(), |
14 | | - require('./site-data')(), |
15 | | - fetchEarlyAccessPaths() |
| 35 | + pages || loadPages(), |
| 36 | + site || loadSiteData(), |
| 37 | + earlyAccessPaths || fetchEarlyAccessPaths() |
16 | 38 | ]) |
| 39 | + } |
17 | 40 |
|
18 | | - redirects = await require('./redirects/precompile')(pages) |
19 | | - siteTree = await require('./site-tree')(pages, site, redirects) |
| 41 | + if (!redirects) { |
| 42 | + redirects = await loadRedirects(pages) |
20 | 43 | } |
21 | 44 |
|
22 | | - return { |
23 | | - pages, site, redirects, siteTree, earlyAccessPaths |
| 45 | + if (!siteTree) { |
| 46 | + siteTree = await loadSiteTree(pages, site, redirects) |
24 | 47 | } |
| 48 | + |
| 49 | + if (process.env.NODE_ENV !== 'test') { |
| 50 | + console.log(`Context primed in ${Date.now() - startTime} ms`) |
| 51 | + } |
| 52 | + |
| 53 | + return getWarmedCache() |
25 | 54 | } |
26 | 55 |
|
27 | 56 | // Instrument the `warmServer` function so that |
28 | 57 | // 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