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.

94 lines
2.4 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.sideloader = require('./sideloader').init()
  33. WIKI.events = new EventEmitter()
  34. } catch (err) {
  35. WIKI.logger.error(err)
  36. process.exit(1)
  37. }
  38. },
  39. /**
  40. * Boot Master Process
  41. */
  42. async bootMaster() {
  43. try {
  44. if (WIKI.config.setup) {
  45. WIKI.logger.info('Starting setup wizard...')
  46. require('../setup')()
  47. } else {
  48. await this.preBootMaster()
  49. await require('../master')()
  50. this.postBootMaster()
  51. }
  52. } catch (err) {
  53. WIKI.logger.error(err)
  54. process.exit(1)
  55. }
  56. },
  57. /**
  58. * Post-Master Boot Sequence
  59. */
  60. async postBootMaster() {
  61. await WIKI.models.analytics.refreshProvidersFromDisk()
  62. await WIKI.models.authentication.refreshStrategiesFromDisk()
  63. await WIKI.models.editors.refreshEditorsFromDisk()
  64. await WIKI.models.loggers.refreshLoggersFromDisk()
  65. await WIKI.models.renderers.refreshRenderersFromDisk()
  66. await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
  67. await WIKI.models.storage.refreshTargetsFromDisk()
  68. await WIKI.auth.activateStrategies()
  69. await WIKI.models.searchEngines.initEngine()
  70. await WIKI.models.storage.initTargets()
  71. WIKI.scheduler.start()
  72. },
  73. /**
  74. * Init Telemetry
  75. */
  76. async initTelemetry() {
  77. require('./telemetry').init()
  78. process.on('unhandledRejection', (err) => {
  79. WIKI.logger.warn(err)
  80. WIKI.telemetry.sendError(err)
  81. })
  82. process.on('uncaughtException', (err) => {
  83. WIKI.logger.warn(err)
  84. WIKI.telemetry.sendError(err)
  85. })
  86. }
  87. }