app.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. logger.initialize(process.env.METRICS_APP_NAME || 'web')
  21. logger.logger.serializers.user = Serializers.user
  22. logger.logger.serializers.docs = Serializers.docs
  23. logger.logger.serializers.files = Serializers.files
  24. logger.logger.serializers.project = Serializers.project
  25. http.globalAgent.keepAlive = false
  26. http.globalAgent.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
  27. https.globalAgent.keepAlive = false
  28. https.globalAgent.maxSockets = Settings.limits.httpsGlobalAgentMaxSockets
  29. metrics.memory.monitor(logger)
  30. metrics.leaked_sockets.monitor(logger)
  31. metrics.open_sockets.monitor()
  32. if (Settings.catchErrors) {
  33. process.removeAllListeners('uncaughtException')
  34. process.removeAllListeners('unhandledRejection')
  35. process
  36. .on('uncaughtException', error =>
  37. logger.error({ err: error }, 'uncaughtException')
  38. )
  39. .on('unhandledRejection', (reason, p) => {
  40. logger.error({ err: reason }, 'unhandledRejection at Promise', p)
  41. })
  42. }
  43. // Create ./data/dumpFolder if needed
  44. FileWriter.ensureDumpFolderExists()
  45. const port = Settings.port || Settings.internal.web.port || 3000
  46. const host = Settings.internal.web.host || '127.0.0.1'
  47. if (process.argv[1] === fileURLToPath(import.meta.url)) {
  48. // Called directly
  49. // We want to make sure that we provided a password through the environment.
  50. if (!process.env.WEB_API_USER || !process.env.WEB_API_PASSWORD) {
  51. throw new Error('No API user and password provided')
  52. }
  53. PlansLocator.ensurePlansAreSetupCorrectly()
  54. Promise.all([
  55. mongodb.connectionPromise,
  56. mongoose.connectionPromise,
  57. HistoryManager.promises.loadGlobalBlobs(),
  58. ])
  59. .then(async () => {
  60. Server.server.listen(port, host, function () {
  61. logger.debug(`web starting up, listening on ${host}:${port}`)
  62. logger.debug(`${http.globalAgent.maxSockets} sockets enabled`)
  63. // wait until the process is ready before monitoring the event loop
  64. metrics.event_loop.monitor(logger)
  65. })
  66. QueueWorkers.start()
  67. await Modules.start()
  68. })
  69. .catch(err => {
  70. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  71. process.exit(1)
  72. })
  73. }
  74. // initialise site admin tasks
  75. Promise.all([
  76. mongodb.connectionPromise,
  77. mongoose.connectionPromise,
  78. HistoryManager.promises.loadGlobalBlobs(),
  79. ])
  80. .then(() => SiteAdminHandler.initialise())
  81. .catch(err => {
  82. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  83. process.exit(1)
  84. })
  85. // handle SIGTERM for graceful shutdown in kubernetes
  86. process.on('SIGTERM', function (signal) {
  87. triggerGracefulShutdown(Server.server, signal)
  88. })
  89. export default Server.server