You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const EventEmitter = require('events')
  3. /* global WIKI */
  4. module.exports = {
  5. async init() {
  6. WIKI.logger.info('=======================================')
  7. WIKI.logger.info(`= Wiki.js ${_.padEnd(WIKI.version + ' ', 29, '=')}`)
  8. WIKI.logger.info('=======================================')
  9. WIKI.logger.info('Initializing...')
  10. WIKI.models = require('./db').init()
  11. try {
  12. await WIKI.models.onReady
  13. await WIKI.configSvc.loadFromDb()
  14. await WIKI.configSvc.applyFlags()
  15. } catch (err) {
  16. WIKI.logger.error('Database Initialization Error: ' + err.message)
  17. if (WIKI.IS_DEBUG) {
  18. console.error(err)
  19. }
  20. process.exit(1)
  21. }
  22. this.bootMaster()
  23. },
  24. /**
  25. * Pre-Master Boot Sequence
  26. */
  27. async preBootMaster() {
  28. try {
  29. await this.initTelemetry()
  30. WIKI.cache = require('./cache').init()
  31. WIKI.scheduler = require('./scheduler').init()
  32. WIKI.events = new EventEmitter()
  33. } catch (err) {
  34. WIKI.logger.error(err)
  35. process.exit(1)
  36. }
  37. },
  38. /**
  39. * Boot Master Process
  40. */
  41. async bootMaster() {
  42. try {
  43. if (WIKI.config.setup) {
  44. WIKI.logger.info('Starting setup wizard...')
  45. require('../setup')()
  46. } else {
  47. await this.preBootMaster()
  48. await require('../master')()
  49. this.postBootMaster()
  50. }
  51. } catch (err) {
  52. WIKI.logger.error(err)
  53. process.exit(1)
  54. }
  55. },
  56. /**
  57. * Post-Master Boot Sequence
  58. */
  59. async postBootMaster() {
  60. await WIKI.models.authentication.refreshStrategiesFromDisk()
  61. await WIKI.models.editors.refreshEditorsFromDisk()
  62. await WIKI.models.loggers.refreshLoggersFromDisk()
  63. await WIKI.models.renderers.refreshRenderersFromDisk()
  64. await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
  65. await WIKI.models.storage.refreshTargetsFromDisk()
  66. await WIKI.auth.activateStrategies()
  67. await WIKI.models.searchEngines.initEngine()
  68. await WIKI.models.storage.initTargets()
  69. WIKI.scheduler.start()
  70. },
  71. /**
  72. * Init Telemetry
  73. */
  74. async initTelemetry() {
  75. require('./telemetry').init()
  76. process.on('unhandledRejection', (err) => {
  77. WIKI.logger.warn(err)
  78. WIKI.telemetry.sendError(err)
  79. })
  80. process.on('uncaughtException', (err) => {
  81. WIKI.logger.warn(err)
  82. WIKI.telemetry.sendError(err)
  83. })
  84. }
  85. }