service-core/runner.mjs

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-09-01 17:31:38 +00:00
import { readFileSync } from 'fs'
2020-09-08 08:11:42 +00:00
import getLog from './core/log.mjs'
import lowdb from './core/db.mjs'
2020-09-07 00:47:53 +00:00
import Core from './core/core.mjs'
2020-09-08 08:11:42 +00:00
import Util from './core/util.mjs'
2020-09-01 17:31:38 +00:00
let config
try {
config = JSON.parse(readFileSync('./config.json'))
} catch (err) {
let logger = getLog('critical-error')
logger.fatal('Error opening config file')
logger.fatal('Make sure it is valid JSON')
logger.fatal(err)
logger.event.error('Unable to start, error in config.json: ' + err.message)
process.exit(10)
}
const log = getLog(config.name)
2020-09-07 00:47:53 +00:00
const close = function(err) {
if (err) {
log.fatal(err, 'App recorded a fatal error')
log.event.error('App recorded a fatal error: ' + err.message, null, function() {
process.exit(4)
2020-09-01 17:31:38 +00:00
})
2020-09-07 00:47:53 +00:00
return
}
log.warn('App asked to be restarted')
log.event.warn('App requested to be restarted', null, function() {
process.exit(0)
2020-09-01 17:31:38 +00:00
})
2020-09-07 00:47:53 +00:00
}
2020-09-08 08:11:42 +00:00
const util = new Util(import.meta.url)
2020-09-09 15:41:05 +00:00
lowdb(util, log).then(async function(db) {
2020-09-08 08:11:42 +00:00
let core = new Core(util, config, db, log, close)
2020-09-09 15:41:05 +00:00
let errors = 0
try {
await core.start('app')
} catch (err) {
log.event.error('Unable to start app: ' + err.message)
2020-09-09 15:41:05 +00:00
log.error(err, 'Unable to start app')
errors++
}
try {
await core.start('manage')
} catch (err) {
log.event.error('Unable to start manage: ' + err.message)
2020-09-09 15:41:05 +00:00
log.error(err, 'Unable to start manage')
errors++
}
core.startMonitor()
2020-09-09 15:41:05 +00:00
if (errors === 2 || (!core.appRunning && !core.manageRunning)) {
throw new Error('Neither manage or app were started, exiting.')
2020-09-07 00:47:53 +00:00
}
2020-09-01 17:31:38 +00:00
}, function(err) {
log.fatal(err, 'Critical error opening database')
2020-09-07 00:47:53 +00:00
log.event.error('Critical error opening database: ' + err.message, null, function() {
process.exit(2)
})
2020-09-01 17:31:38 +00:00
}).catch(function(err) {
log.fatal(err, 'Unknown error occured opening app')
2020-09-07 00:47:53 +00:00
log.event.error('Unknown error occured opening app: ' + err.message, null, function() {
process.exit(3)
})
2020-09-01 17:31:38 +00:00
})