app.mjs 3.4 KB

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