node.js - Graceful shutdown does not log anything - Stack Overflow

exit function is not working as it should.It should be registering everything inside logger function.

exit function is not working as it should. It should be registering everything inside logger function. Instead, only console text pop-ups. Even everything wrapped in server.close is not working at all. What's the reason behind this?

import { MongoRepository } from '@repositories/index'
import { container } from '@container/index'
import types from '@inversifyTypes/types'
import logger from '@logging/logger'
import { Server } from 'http'

const processHandler = (server: Server): void => {
  const exit = (signal: string): void => {
    console.log(`Received signal ${signal}. Closing resources...`)
    logger.info(`Received signal ${signal}. Closing resources...`)

    server.close(async () => {
      console.log('Closing MongoDB connection...')
      logger.info('Closing MongoDB connection...')
      const mongoRepository = container.get<MongoRepository>(types.MongoRepository)
      await mongoRepository.disconnect()
      console.log('MongoDB connection closed successfully')
      logger.info('MongoDB connection closed successfully')
      process.exit(0)
    })
  }

  // Handle SIGINT (Ctrl+C in the terminal)
  process.on('SIGINT', () => exit('SIGINT'))

  // Handle SIGTERM (termination signal sent)
  process.on('SIGTERM', () => exit('SIGTERM'))

  // Handle uncaught exceptions
  process.on('uncaughtException', (error) => {
    logger.error({ error }, 'Uncaught exception detected')
    exit('uncaughtException')
  })

  // Handle unhandled promise rejections
  process.on('unhandledRejection', (reason, promise) => {
    logger.error({ promise, reason }, 'Unhandled promise rejection detected')
    exit('unhandledRejection')
  })
}

export default processHandler

CMD printing

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577035a4634027.html

相关推荐

  • node.js - Graceful shutdown does not log anything - Stack Overflow

    exit function is not working as it should.It should be registering everything inside logger function.

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信