close db connection in server HMR

This commit is contained in:
lesion 2022-05-25 10:53:39 +02:00
parent 601c3f7dc8
commit 5125c51ea8
No known key found for this signature in database
GPG key ID: 352918250B012177
2 changed files with 33 additions and 31 deletions

View file

@ -1,15 +1,29 @@
const config = require('../server/config')
config.load()
module.exports = function () { const initialize = {
const config = require('../server/config') // close connections/port/unix socket
const log = require('../server/log') async shutdown (exit = true) {
const settingsController = require('./api/controller/settings') const log = require('../server/log')
const db = require('./api/models/index') const TaskManager = require('../server/taskManager').TaskManager
const dayjs = require('dayjs') if (TaskManager) { TaskManager.stop() }
const timezone = require('dayjs/plugin/timezone') log.info('Closing DB')
dayjs.extend(timezone) const sequelize = require('../server/api/models')
await sequelize.close()
process.off('SIGTERM', initialize.shutdown)
process.off('SIGINT', initialize.shutdown)
if (exit) {
process.exit()
}
},
async function start (nuxt) { async start () {
config.load() const log = require('../server/log')
const settingsController = require('./api/controller/settings')
const db = require('./api/models/index')
const dayjs = require('dayjs')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(timezone)
if (config.status == 'READY') { if (config.status == 'READY') {
await db.initialize() await db.initialize()
} else { } else {
@ -33,31 +47,19 @@ module.exports = function () {
} }
await settingsController.load() await settingsController.load()
} }
dayjs.tz.setDefault(settingsController.settings.instance_timezone) dayjs.tz.setDefault(settingsController.settings.instance_timezone)
let TaskManager let TaskManager
if (config.status === 'READY' && process.env.NODE_ENV == 'production') { if (config.status === 'READY' && process.env.NODE_ENV == 'production') {
TaskManager = require('../server/taskManager').TaskManager TaskManager = require('../server/taskManager').TaskManager
TaskManager.start() TaskManager.start()
} }
log.info(`Listen on ${config.server.host}:${config.server.port}`)
process.on('SIGTERM', initialize.shutdown)
// close connections/port/unix socket process.on('SIGINT', initialize.shutdown)
async function shutdown () {
if (TaskManager) { TaskManager.stop() }
log.info('Closing DB')
const sequelize = require('../server/api/models')
await sequelize.close()
process.off('SIGTERM', shutdown)
process.off('SIGINT', shutdown)
if (nuxt) {
nuxt.close()
}
process.exit()
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
} }
return start(this.nuxt)
} }
module.exports = initialize

View file

@ -83,7 +83,7 @@ module.exports = {
load () { load () {
console.error('dentro load !') console.error('dentro load !')
}, },
unload: initialize.shutdown unload: () => initialize.shutdown(false)
// async unload () { // async unload () {
// const db = require('./api/models/index') // const db = require('./api/models/index')
// await db.close() // await db.close()