kernel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.editors.refreshEditorsFromDisk()
  68. await WIKI.models.loggers.refreshLoggersFromDisk()
  69. await WIKI.models.renderers.refreshRenderersFromDisk()
  70. await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
  71. await WIKI.models.storage.refreshTargetsFromDisk()
  72. await WIKI.auth.activateStrategies()
  73. await WIKI.models.searchEngines.initEngine()
  74. await WIKI.models.storage.initTargets()
  75. WIKI.scheduler.start()
  76. await WIKI.models.subscribeToNotifications()
  77. },
  78. /**
  79. * Init Telemetry
  80. */
  81. async initTelemetry() {
  82. require('./telemetry').init()
  83. process.on('unhandledRejection', (err) => {
  84. WIKI.logger.warn(err)
  85. WIKI.telemetry.sendError(err)
  86. })
  87. process.on('uncaughtException', (err) => {
  88. WIKI.logger.warn(err)
  89. WIKI.telemetry.sendError(err)
  90. })
  91. },
  92. /**
  93. * Graceful shutdown
  94. */
  95. async shutdown () {
  96. if (WIKI.models) {
  97. await WIKI.models.unsubscribeToNotifications()
  98. await WIKI.models.knex.client.pool.destroy()
  99. await WIKI.models.knex.destroy()
  100. }
  101. if (WIKI.scheduler) {
  102. WIKI.scheduler.stop()
  103. }
  104. if (WIKI.servers) {
  105. await WIKI.servers.stopServers()
  106. }
  107. }
  108. }