gancio-upstream/server/initialize.server.js

73 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-05-25 10:53:39 +02:00
const config = require('../server/config')
const db = require('./api/models/index')
const log = require('../server/log')
const { Settings } = require('luxon')
db.initialize()
const settingsController = require('./api/controller/settings')
2022-05-25 10:53:39 +02:00
const initialize = {
// close connections/port/unix socket
async shutdown (exit = true) {
2022-06-22 11:31:14 +02:00
if (config.status == 'READY') {
const log = require('../server/log')
const TaskManager = require('../server/taskManager').TaskManager
if (TaskManager) { TaskManager.stop() }
log.info('Closing DB')
const sequelize = require('../server/api/models')
await sequelize.close()
}
2022-05-25 10:53:39 +02:00
process.off('SIGTERM', initialize.shutdown)
process.off('SIGINT', initialize.shutdown)
if (exit) {
process.exit()
}
},
2022-05-25 10:53:39 +02:00
async start () {
if (config.status == 'CONFIGURED') {
await db.sequelize.authenticate()
log.debug('Running migrations')
await db.runMigrations()
2023-08-08 23:11:40 +02:00
await db.fixMariaDBJSON()
await settingsController.load()
config.status = 'READY'
} else {
if (process.env.GANCIO_DB_DIALECT) {
const setupController = require('./api/controller/setup')
const dbConf = {
dialect: process.env.GANCIO_DB_DIALECT,
storage: process.env.GANCIO_DB_STORAGE,
host: process.env.GANCIO_DB_HOST,
port: process.env.GANCIO_DB_PORT,
database: process.env.GANCIO_DB_DATABASE,
username: process.env.GANCIO_DB_USERNAME,
password: process.env.GANCIO_DB_PASSWORD,
}
2022-06-25 23:15:58 +02:00
await setupController._setupDb(dbConf)
2022-05-20 13:54:57 +02:00
.catch(e => {
log.warn(String(e))
process.exit(1)
})
}
await settingsController.load()
}
2022-05-25 10:53:39 +02:00
Settings.defaultLocale = settingsController.settings.instance_locale
Settings.defaultZone = settingsController.settings.instance_timezone
2022-05-25 10:53:39 +02:00
2021-10-20 14:10:33 +02:00
let TaskManager
if (config.status === 'READY' && process.env.NODE_ENV != 'test') {
2021-10-20 14:10:33 +02:00
TaskManager = require('../server/taskManager').TaskManager
TaskManager.start()
}
2022-05-25 10:53:39 +02:00
process.on('SIGTERM', initialize.shutdown)
process.on('SIGINT', initialize.shutdown)
2021-12-06 20:28:37 +01:00
}
2021-10-20 14:10:33 +02:00
}
2022-05-25 10:53:39 +02:00
module.exports = initialize