最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论