app.mjs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 '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.maxSockets = Settings.limits.httpGlobalAgentMaxSockets
  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([mongodb.connectionPromise, mongoose.connectionPromise])
  55. .then(async () => {
  56. Server.server.listen(port, host, function () {
  57. logger.debug(`web starting up, listening on ${host}:${port}`)
  58. logger.debug(`${http.globalAgent.maxSockets} sockets enabled`)
  59. // wait until the process is ready before monitoring the event loop
  60. metrics.event_loop.monitor(logger)
  61. })
  62. QueueWorkers.start()
  63. await Modules.start()
  64. })
  65. .catch(err => {
  66. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  67. process.exit(1)
  68. })
  69. }
  70. // initialise site admin tasks
  71. Promise.all([mongodb.connectionPromise, mongoose.connectionPromise])
  72. .then(() => SiteAdminHandler.initialise())
  73. .catch(err => {
  74. logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
  75. process.exit(1)
  76. })
  77. // handle SIGTERM for graceful shutdown in kubernetes
  78. process.on('SIGTERM', function (signal) {
  79. triggerGracefulShutdown(Server.server, signal)
  80. })
  81. export default Server.server