app.mjs 3.5 KB

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