kernel.js 2.0 KB

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