2022-05-25 10:53:39 +02:00
|
|
|
const config = require('../server/config')
|
2022-12-23 01:08:14 +01:00
|
|
|
const db = require('./api/models/index')
|
|
|
|
const log = require('../server/log')
|
2023-03-28 19:02:08 +02:00
|
|
|
const { Settings } = require('luxon')
|
2022-12-23 01:08:14 +01:00
|
|
|
|
|
|
|
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-04-27 11:58:58 +02:00
|
|
|
|
2022-05-25 10:53:39 +02:00
|
|
|
async start () {
|
2022-08-02 15:14:34 +02:00
|
|
|
if (config.status == 'CONFIGURED') {
|
2022-12-23 01:08:14 +01:00
|
|
|
await db.sequelize.authenticate()
|
|
|
|
log.debug('Running migrations')
|
|
|
|
await db.runMigrations()
|
2023-08-08 23:11:40 +02:00
|
|
|
await db.fixMariaDBJSON()
|
2022-12-23 01:08:14 +01:00
|
|
|
await settingsController.load()
|
2022-08-02 15:14:34 +02:00
|
|
|
config.status = 'READY'
|
2022-03-09 14:21:13 +01:00
|
|
|
} 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,
|
2022-05-20 13:07:32 +02:00
|
|
|
port: process.env.GANCIO_DB_PORT,
|
2022-03-09 14:21:13 +01:00
|
|
|
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)
|
|
|
|
})
|
2022-03-09 14:21:13 +01:00
|
|
|
}
|
|
|
|
await settingsController.load()
|
|
|
|
}
|
2022-05-25 10:53:39 +02:00
|
|
|
|
2023-03-28 19:02:08 +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
|
2023-03-28 19:02:08 +02:00
|
|
|
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
|