import lightshipCjs from 'lightship'
import logger from './logger.js'

const { createLightship } = lightshipCjs
const lightship = await createLightship()

// This is a graceful shutdown handler in case of uncaught exceptions
process.on('uncaughtException', async err => {
  logger.error(err, 'uncaughtException')
  await lightship.shutdown()
})

// This is a graceful shutdown handler in case of unhandled promise rejections
process.on('unhandledRejection', async err => {
  logger.error(err, 'unhandledRejection')
  await lightship.shutdown()
})

// This is a graceful shutdown handler in case of SIGINT
process.on('SIGINT', async (process) => {
  logger.info('SIGINT received')
  await lightship.shutdown()
})

export default lightship