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.

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