Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

warm-server.js 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  1. const statsd = require('./statsd')
  2. const { loadPages, loadPageMap } = require('./pages')
  3. const loadRedirects = require('./redirects/precompile')
  4. const loadSiteData = require('./site-data')
  5. const loadSiteTree = require('./site-tree')
  6. // Instrument these functions so that
  7. // it's wrapped in a timer that reports to Datadog
  8. const dog = {
  9. loadPages: statsd.asyncTimer(loadPages, 'load_pages'),
  10. loadPageMap: statsd.asyncTimer(loadPageMap, 'load_page_map'),
  11. loadRedirects: statsd.asyncTimer(loadRedirects, 'load_redirects'),
  12. loadSiteData: statsd.timer(loadSiteData, 'load_site_data'),
  13. loadSiteTree: statsd.asyncTimer(loadSiteTree, 'load_site_tree')
  14. }
  15. // For local caching
  16. let pageList, pageMap, site, redirects, siteTree
  17. function isFullyWarmed () {
  18. // NOTE: Yes, `pageList` is specifically excluded here as it is transient data
  19. const fullyWarmed = !!(pageMap && site && redirects && siteTree)
  20. return fullyWarmed
  21. }
  22. function getWarmedCache () {
  23. return {
  24. pages: pageMap,
  25. site,
  26. redirects,
  27. siteTree
  28. }
  29. }
  30. async function warmServer () {
  31. const startTime = Date.now()
  32. if (process.env.NODE_ENV !== 'test') {
  33. console.log('Priming context information...')
  34. }
  35. if (!pageList) {
  36. pageList = await dog.loadPages()
  37. }
  38. if (!site) {
  39. site = dog.loadSiteData()
  40. }
  41. if (!pageMap) {
  42. pageMap = await dog.loadPageMap(pageList)
  43. }
  44. if (!redirects) {
  45. redirects = await dog.loadRedirects(pageList, pageMap)
  46. }
  47. if (!siteTree) {
  48. siteTree = await dog.loadSiteTree(pageMap, site, redirects)
  49. }
  50. if (process.env.NODE_ENV !== 'test') {
  51. console.log(`Context primed in ${Date.now() - startTime} ms`)
  52. }
  53. return getWarmedCache()
  54. }
  55. // Instrument the `warmServer` function so that
  56. // it's wrapped in a timer that reports to Datadog
  57. dog.warmServer = statsd.asyncTimer(warmServer, 'warm_server')
  58. // We only want statistics if the priming needs to occur, so let's wrap the
  59. // real method and return early [without statistics] whenever possible
  60. module.exports = async function warmServerWrapper () {
  61. // Bail out early if everything is properly ready to use
  62. if (isFullyWarmed()) {
  63. return getWarmedCache()
  64. }
  65. return dog.warmServer()
  66. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...