app.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Metrics must be initialized before importing anything else
  2. import '@overleaf/metrics/initialize.js'
  3. import Modules from './app/src/infrastructure/Modules.js'
  4. import metrics from '@overleaf/metrics'
  5. import Settings from '@overleaf/settings'
  6. import logger from '@overleaf/logger'
  7. import PlansLocator from './app/src/Features/Subscription/PlansLocator.js'
  8. import HistoryManager from './app/src/Features/History/HistoryManager.js'
  9. import SiteAdminHandler from './app/src/infrastructure/SiteAdminHandler.js'
  10. import http from 'node:http'
  11. import https from 'node:https'
  12. import * as Serializers from './app/src/infrastructure/LoggerSerializers.js'
  13. import Server from './app/src/infrastructure/Server.mjs'
  14. import QueueWorkers from './app/src/infrastructure/QueueWorkers.js'
  15. import mongodb from './app/src/infrastructure/mongodb.js'
  16. import mongoose from './app/src/infrastructure/Mongoose.js'
  17. import { triggerGracefulShutdown } from './app/src/infrastructure/GracefulShutdown.js'
  18. import FileWriter from './app/src/infrastructure/FileWriter.js'
  19. import { fileURLToPath } from 'node:url'
  20. import Features from './app/src/infrastructure/Features.js'
  21. logger.initialize(process.env.METRICS_APP_NAME || 'web')
  22. logger.logger.serializers.user = Serializers.user
  23. logger.logger.serializers.docs = Serializers.docs
  24. logger.logger.serializers.files = Serializers.files
  25. logger.logger.serializers.project = Serializers.project
  26. http.globalAgent.keepAlive = false
  27. http.globalAgent.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
  28. https.globalAgent.keepAlive = false
  29. https.globalAgent.maxSockets = Settings.limits.httpsGlobalAgentMaxSockets
  30. metrics.memory.monitor(logger)
  31. metrics.leaked_sockets.monitor(logger)
  32. metrics.open_sockets.monitor()
  33. if (Settings.catchErrors) {
  34. process.removeAllListeners('uncaughtException')
  35. process.removeAllListeners('unhandledRejection')
  36. process
  37. .on('uncaughtException', error =>
  38. logger.error({ err: error }, 'uncaughtException')
  39. )
  40. .on('unhandledRejection', (reason, p) => {
  41. logger.error({ err: reason }, 'unhandledRejection at Promise', p)
  42. })
  43. }
  44. // Create ./data/dumpFolder if needed
  45. FileWriter.ensureDumpFolderExists()
  46. if (
  47. !Features.hasFeature('project-history-blobs') &&
  48. !Features.hasFeature('filestore')
  49. ) {
  50. throw new Error(
  51. 'invalid config: must enable either project-history-blobs (Settings.enableProjectHistoryBlobs=true) or enable filestore (Settings.disableFilestore=false)'
  52. )
  53. }
  54. const port = Settings.port || Settings.internal.web.port || 3000
  55. const host = Settings.internal.web.host || '127.0.0.1'
  56. if (process.argv[1] === fileURLToPath(import.meta.url)) {
  57. // Called directly
  58. // We want to make sure that we provided a password through the environment.
  59. if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
  60. throw new Error('No API user and password provided')
  61. }
  62. PlansLocator.ensurePlansAreSetupCorrectly()
  63. Promise.all([
  64. mongodb.connectionPromise,
  65. mongoose.connectionPromise,
  66. HistoryManager.promises.loadGlobalBlobs(),
  67. ])
  68. .then(async () => {
  69. Server.server.listen(port, host, function () {
  70. logger.debug(`web starting up, listening on ${host}:${port}`)
  71. logger.debug(`${http.globalAgent.maxSockets} sockets enabled`)
  72. // wait until the process is ready before monitoring the event loop
  73. metrics.event_loop.monitor(logger)
  74. })
  75. QueueWorkers.start()
  76. await Modules.start()
  77. })
  78. .catch(err => {
  79. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  80. process.exit(1)
  81. })
  82. }
  83. // initialise site admin tasks
  84. Promise.all([
  85. mongodb.connectionPromise,
  86. mongoose.connectionPromise,
  87. HistoryManager.promises.loadGlobalBlobs(),
  88. ])
  89. .then(() => SiteAdminHandler.initialise())
  90. .catch(err => {
  91. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  92. process.exit(1)
  93. })
  94. // handle SIGTERM for graceful shutdown in kubernetes
  95. process.on('SIGTERM', function (signal) {
  96. triggerGracefulShutdown(Server.server, signal)
  97. })
  98. export default Server.server