kernel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. WIKI.logger.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. await this.preBootMaster()
  51. await require('../master')()
  52. this.postBootMaster()
  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.commentProviders.refreshProvidersFromDisk()
  65. await WIKI.models.editors.refreshEditorsFromDisk()
  66. await WIKI.models.renderers.refreshRenderersFromDisk()
  67. await WIKI.models.storage.refreshTargetsFromDisk()
  68. await WIKI.extensions.init()
  69. await WIKI.auth.activateStrategies()
  70. await WIKI.models.commentProviders.initProvider()
  71. await WIKI.models.storage.initTargets()
  72. WIKI.scheduler.start()
  73. await WIKI.models.subscribeToNotifications()
  74. },
  75. /**
  76. * Init Telemetry
  77. */
  78. async initTelemetry() {
  79. require('./telemetry').init()
  80. process.on('unhandledRejection', (err) => {
  81. WIKI.logger.warn(err)
  82. WIKI.telemetry.sendError(err)
  83. })
  84. process.on('uncaughtException', (err) => {
  85. WIKI.logger.warn(err)
  86. WIKI.telemetry.sendError(err)
  87. })
  88. },
  89. /**
  90. * Graceful shutdown
  91. */
  92. async shutdown () {
  93. if (WIKI.servers) {
  94. await WIKI.servers.stopServers()
  95. }
  96. if (WIKI.scheduler) {
  97. await WIKI.scheduler.stop()
  98. }
  99. if (WIKI.models) {
  100. await WIKI.models.unsubscribeToNotifications()
  101. if (WIKI.models.knex) {
  102. await WIKI.models.knex.destroy()
  103. }
  104. }
  105. if (WIKI.asar) {
  106. await WIKI.asar.unload()
  107. }
  108. process.exit(0)
  109. }
  110. }