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.

127 lines
3.3 KiB

5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const EventEmitter = require('eventemitter2').EventEmitter2
  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.sideloader = await require('./sideloader').init()
  31. WIKI.cache = require('./cache').init()
  32. WIKI.scheduler = require('./scheduler').init()
  33. WIKI.servers = require('./servers')
  34. WIKI.events = {
  35. inbound: new EventEmitter(),
  36. outbound: new EventEmitter()
  37. }
  38. WIKI.extensions = require('./extensions')
  39. WIKI.asar = require('./asar')
  40. } catch (err) {
  41. WIKI.logger.error(err)
  42. process.exit(1)
  43. }
  44. },
  45. /**
  46. * Boot Master Process
  47. */
  48. async bootMaster() {
  49. try {
  50. if (WIKI.config.setup) {
  51. WIKI.logger.info('Starting setup wizard...')
  52. require('../setup')()
  53. } else {
  54. await this.preBootMaster()
  55. await require('../master')()
  56. this.postBootMaster()
  57. }
  58. } catch (err) {
  59. WIKI.logger.error(err)
  60. process.exit(1)
  61. }
  62. },
  63. /**
  64. * Post-Master Boot Sequence
  65. */
  66. async postBootMaster() {
  67. await WIKI.models.analytics.refreshProvidersFromDisk()
  68. await WIKI.models.authentication.refreshStrategiesFromDisk()
  69. await WIKI.models.commentProviders.refreshProvidersFromDisk()
  70. await WIKI.models.editors.refreshEditorsFromDisk()
  71. await WIKI.models.loggers.refreshLoggersFromDisk()
  72. await WIKI.models.renderers.refreshRenderersFromDisk()
  73. await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
  74. await WIKI.models.storage.refreshTargetsFromDisk()
  75. await WIKI.extensions.init()
  76. await WIKI.auth.activateStrategies()
  77. await WIKI.models.commentProviders.initProvider()
  78. await WIKI.models.searchEngines.initEngine()
  79. await WIKI.models.storage.initTargets()
  80. WIKI.scheduler.start()
  81. await WIKI.models.subscribeToNotifications()
  82. },
  83. /**
  84. * Init Telemetry
  85. */
  86. async initTelemetry() {
  87. require('./telemetry').init()
  88. process.on('unhandledRejection', (err) => {
  89. WIKI.logger.warn(err)
  90. WIKI.telemetry.sendError(err)
  91. })
  92. process.on('uncaughtException', (err) => {
  93. WIKI.logger.warn(err)
  94. WIKI.telemetry.sendError(err)
  95. })
  96. },
  97. /**
  98. * Graceful shutdown
  99. */
  100. async shutdown () {
  101. if (WIKI.servers) {
  102. await WIKI.servers.stopServers()
  103. }
  104. if (WIKI.scheduler) {
  105. await WIKI.scheduler.stop()
  106. }
  107. if (WIKI.models) {
  108. await WIKI.models.unsubscribeToNotifications()
  109. if (WIKI.models.knex) {
  110. await WIKI.models.knex.destroy()
  111. }
  112. }
  113. if (WIKI.asar) {
  114. await WIKI.asar.unload()
  115. }
  116. process.exit(0)
  117. }
  118. }