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.

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