kernel.js 3.26 KB
Newer Older
1
const _ = require('lodash')
2
const EventEmitter = require('eventemitter2').EventEmitter2
3

4
/* global WIKI */
NGPixel's avatar
NGPixel committed
5 6

module.exports = {
7 8
  async init() {
    WIKI.logger.info('=======================================')
9
    WIKI.logger.info(`= Wiki.js ${_.padEnd(WIKI.version + ' ', 29, '=')}`)
10
    WIKI.logger.info('=======================================')
11
    WIKI.logger.info('Initializing...')
NGPixel's avatar
NGPixel committed
12

13
    WIKI.models = require('./db').init()
NGPixel's avatar
NGPixel committed
14

Nick's avatar
Nick committed
15 16 17
    try {
      await WIKI.models.onReady
      await WIKI.configSvc.loadFromDb()
Nick's avatar
Nick committed
18
      await WIKI.configSvc.applyFlags()
Nick's avatar
Nick committed
19 20 21 22 23 24 25
    } catch (err) {
      WIKI.logger.error('Database Initialization Error: ' + err.message)
      if (WIKI.IS_DEBUG) {
        console.error(err)
      }
      process.exit(1)
    }
26

27
    this.bootMaster()
NGPixel's avatar
NGPixel committed
28 29 30 31
  },
  /**
   * Pre-Master Boot Sequence
   */
32 33
  async preBootMaster() {
    try {
34 35 36
      await this.initTelemetry()
      WIKI.cache = require('./cache').init()
      WIKI.scheduler = require('./scheduler').init()
NGPixel's avatar
NGPixel committed
37
      WIKI.servers = require('./servers')
38
      WIKI.sideloader = require('./sideloader').init()
39 40 41 42
      WIKI.events = {
        inbound: new EventEmitter(),
        outbound: new EventEmitter()
      }
43
      WIKI.extensions = require('./extensions')
44
      WIKI.asar = require('./asar')
45 46 47 48
    } catch (err) {
      WIKI.logger.error(err)
      process.exit(1)
    }
NGPixel's avatar
NGPixel committed
49 50 51 52
  },
  /**
   * Boot Master Process
   */
53 54 55 56
  async bootMaster() {
    try {
      if (WIKI.config.setup) {
        WIKI.logger.info('Starting setup wizard...')
57
        require('../setup')()
58
      } else {
59
        await this.preBootMaster()
60 61
        await require('../master')()
        this.postBootMaster()
NGPixel's avatar
NGPixel committed
62
      }
63
    } catch (err) {
64
      WIKI.logger.error(err)
NGPixel's avatar
NGPixel committed
65
      process.exit(1)
66
    }
NGPixel's avatar
NGPixel committed
67 68 69 70
  },
  /**
   * Post-Master Boot Sequence
   */
71
  async postBootMaster() {
72
    await WIKI.models.analytics.refreshProvidersFromDisk()
73
    await WIKI.models.authentication.refreshStrategiesFromDisk()
NGPixel's avatar
NGPixel committed
74
    await WIKI.models.commentProviders.refreshProvidersFromDisk()
75
    await WIKI.models.editors.refreshEditorsFromDisk()
76
    await WIKI.models.loggers.refreshLoggersFromDisk()
77
    await WIKI.models.renderers.refreshRenderersFromDisk()
78
    await WIKI.models.searchEngines.refreshSearchEnginesFromDisk()
79
    await WIKI.models.storage.refreshTargetsFromDisk()
80

81 82
    await WIKI.extensions.init()

83
    await WIKI.auth.activateStrategies()
84
    await WIKI.models.commentProviders.initProvider()
85
    await WIKI.models.searchEngines.initEngine()
86
    await WIKI.models.storage.initTargets()
87
    WIKI.scheduler.start()
88 89

    await WIKI.models.subscribeToNotifications()
Nick's avatar
Nick committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  },
  /**
   * Init Telemetry
   */
  async initTelemetry() {
    require('./telemetry').init()

    process.on('unhandledRejection', (err) => {
      WIKI.logger.warn(err)
      WIKI.telemetry.sendError(err)
    })
    process.on('uncaughtException', (err) => {
      WIKI.logger.warn(err)
      WIKI.telemetry.sendError(err)
    })
105 106 107 108 109 110 111 112 113 114 115 116 117
  },
  /**
   * Graceful shutdown
   */
  async shutdown () {
    if (WIKI.models) {
      await WIKI.models.unsubscribeToNotifications()
      await WIKI.models.knex.client.pool.destroy()
      await WIKI.models.knex.destroy()
    }
    if (WIKI.scheduler) {
      WIKI.scheduler.stop()
    }
118 119 120
    if (WIKI.asar) {
      await WIKI.asar.unload()
    }
121 122 123
    if (WIKI.servers) {
      await WIKI.servers.stopServers()
    }
NGPixel's avatar
NGPixel committed
124 125
  }
}