app.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {
  2. initInstrumentation,
  3. setupAdditionalResourceAttributes,
  4. startOpenTelemetry,
  5. } from '~/features/opentelemetry/server';
  6. import loggerFactory from '~/utils/logger';
  7. import { hasProcessFlag } from '~/utils/process-utils';
  8. const logger = loggerFactory('growi');
  9. /** **********************************
  10. * Main Process
  11. ********************************** */
  12. process.on('uncaughtException', (err?: Error) => {
  13. logger.error({ err }, 'Uncaught Exception');
  14. });
  15. process.on('unhandledRejection', (reason, p) => {
  16. logger.error({ reason, promise: p }, 'Unhandled Rejection');
  17. });
  18. async function main() {
  19. try {
  20. // Initialize OpenTelemetry
  21. await initInstrumentation();
  22. const Crowi = (await import('./crowi')).default;
  23. const growi = new Crowi();
  24. const server = await growi.start();
  25. // Start OpenTelemetry
  26. await setupAdditionalResourceAttributes();
  27. startOpenTelemetry();
  28. if (hasProcessFlag('ci')) {
  29. logger.info('"--ci" flag is detected. Exit process.');
  30. server.close(() => {
  31. process.exit();
  32. });
  33. }
  34. } catch (err) {
  35. logger.error('An error occurred, unable to start the server');
  36. logger.error(err);
  37. process.exit(1);
  38. }
  39. }
  40. main();