kernel.js 3.0 KB

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