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.

89 lines
2.2 KiB

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