2018-08-06 03:03:46 +00:00
|
|
|
const _ = require('lodash')
|
2020-02-03 02:55:29 +00:00
|
|
|
const EventEmitter = require('eventemitter2').EventEmitter2
|
2018-08-06 03:03:46 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-10-08 02:44:35 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2018-05-28 18:46:55 +00:00
|
|
|
async init() {
|
|
|
|
WIKI.logger.info('=======================================')
|
2018-08-06 03:03:46 +00:00
|
|
|
WIKI.logger.info(`= Wiki.js ${_.padEnd(WIKI.version + ' ', 29, '=')}`)
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.info('=======================================')
|
2019-05-27 04:28:12 +00:00
|
|
|
WIKI.logger.info('Initializing...')
|
2017-10-08 02:44:35 +00:00
|
|
|
|
2018-07-30 02:23:33 +00:00
|
|
|
WIKI.models = require('./db').init()
|
2017-10-08 02:44:35 +00:00
|
|
|
|
2019-02-22 22:05:18 +00:00
|
|
|
try {
|
|
|
|
await WIKI.models.onReady
|
|
|
|
await WIKI.configSvc.loadFromDb()
|
2019-02-23 23:22:25 +00:00
|
|
|
await WIKI.configSvc.applyFlags()
|
2019-02-22 22:05:18 +00:00
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error('Database Initialization Error: ' + err.message)
|
|
|
|
if (WIKI.IS_DEBUG) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2019-02-13 22:20:46 +00:00
|
|
|
|
2018-05-28 18:46:55 +00:00
|
|
|
this.bootMaster()
|
2017-10-08 02:44:35 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Pre-Master Boot Sequence
|
|
|
|
*/
|
2018-05-28 18:46:55 +00:00
|
|
|
async preBootMaster() {
|
|
|
|
try {
|
2019-02-13 22:20:46 +00:00
|
|
|
await this.initTelemetry()
|
|
|
|
WIKI.cache = require('./cache').init()
|
|
|
|
WIKI.scheduler = require('./scheduler').init()
|
2020-01-12 03:33:19 +00:00
|
|
|
WIKI.servers = require('./servers')
|
2019-06-22 00:54:09 +00:00
|
|
|
WIKI.sideloader = require('./sideloader').init()
|
2020-04-20 00:26:26 +00:00
|
|
|
WIKI.events = {
|
|
|
|
inbound: new EventEmitter(),
|
|
|
|
outbound: new EventEmitter()
|
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2017-10-08 02:44:35 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Boot Master Process
|
|
|
|
*/
|
2018-05-28 18:46:55 +00:00
|
|
|
async bootMaster() {
|
|
|
|
try {
|
|
|
|
if (WIKI.config.setup) {
|
|
|
|
WIKI.logger.info('Starting setup wizard...')
|
2017-12-24 05:34:47 +00:00
|
|
|
require('../setup')()
|
2018-05-28 18:46:55 +00:00
|
|
|
} else {
|
2019-02-13 22:20:46 +00:00
|
|
|
await this.preBootMaster()
|
2018-05-28 18:46:55 +00:00
|
|
|
await require('../master')()
|
|
|
|
this.postBootMaster()
|
2017-10-08 02:44:35 +00:00
|
|
|
}
|
2018-05-28 18:46:55 +00:00
|
|
|
} catch (err) {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error(err)
|
2017-10-08 02:44:35 +00:00
|
|
|
process.exit(1)
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
2017-10-08 02:44:35 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Post-Master Boot Sequence
|
|
|
|
*/
|
2017-12-24 05:34:47 +00:00
|
|
|
async postBootMaster() {
|
2019-06-23 22:35:14 +00:00
|
|
|
await WIKI.models.analytics.refreshProvidersFromDisk()
|
2018-08-04 21:27:55 +00:00
|
|
|
await WIKI.models.authentication.refreshStrategiesFromDisk()
|
2020-04-07 02:21:42 +00:00
|
|
|
await WIKI.models.commentProviders.refreshProvidersFromDisk()
|
2018-08-20 05:02:57 +00:00
|
|
|
await WIKI.models.editors.refreshEditorsFromDisk()
|
2018-09-01 19:15:44 +00:00
|
|
|
await WIKI.models.loggers.refreshLoggersFromDisk()
|
2018-08-27 02:38:08 +00:00
|
|
|
await WIKI.models.renderers.refreshRenderersFromDisk()
|
2018-09-01 19:15:44 +00:00
|
|
|
await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.storage.refreshTargetsFromDisk()
|
2018-08-06 03:03:46 +00:00
|
|
|
|
|
|
|
await WIKI.auth.activateStrategies()
|
2020-05-17 02:46:05 +00:00
|
|
|
await WIKI.models.commentProviders.initProvider()
|
2019-03-09 05:51:02 +00:00
|
|
|
await WIKI.models.searchEngines.initEngine()
|
2019-02-03 22:08:06 +00:00
|
|
|
await WIKI.models.storage.initTargets()
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.scheduler.start()
|
2020-04-20 00:26:26 +00:00
|
|
|
|
|
|
|
await WIKI.models.subscribeToNotifications()
|
2019-02-02 06:17:09 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Init Telemetry
|
|
|
|
*/
|
|
|
|
async initTelemetry() {
|
|
|
|
require('./telemetry').init()
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (err) => {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
WIKI.telemetry.sendError(err)
|
|
|
|
})
|
|
|
|
process.on('uncaughtException', (err) => {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
WIKI.telemetry.sendError(err)
|
|
|
|
})
|
2020-02-03 02:55:29 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Graceful shutdown
|
|
|
|
*/
|
|
|
|
async shutdown () {
|
|
|
|
if (WIKI.models) {
|
|
|
|
await WIKI.models.unsubscribeToNotifications()
|
|
|
|
await WIKI.models.knex.client.pool.destroy()
|
|
|
|
await WIKI.models.knex.destroy()
|
|
|
|
}
|
|
|
|
if (WIKI.scheduler) {
|
|
|
|
WIKI.scheduler.stop()
|
|
|
|
}
|
|
|
|
if (WIKI.servers) {
|
|
|
|
await WIKI.servers.stopServers()
|
|
|
|
}
|
2017-10-08 02:44:35 +00:00
|
|
|
}
|
|
|
|
}
|