wikijs-fork/server/modules/kernel.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-10-08 02:44:35 +00:00
const cluster = require('cluster')
const Promise = require('bluebird')
const _ = require('lodash')
/* global wiki */
module.exports = {
numWorkers: 1,
workers: [],
init() {
if (cluster.isMaster) {
wiki.logger.info('=======================================')
wiki.logger.info('= Wiki.js =============================')
wiki.logger.info('=======================================')
wiki.redis = require('./redis').init()
wiki.queue = require('./queue').init()
this.setWorkerLimit()
this.bootMaster()
} else {
this.bootWorker()
}
},
/**
* Pre-Master Boot Sequence
*/
preBootMaster() {
return Promise.mapSeries([
() => { return wiki.db.onReady },
() => { return wiki.configSvc.loadFromDb() },
() => { return wiki.queue.clean() }
], fn => { return fn() })
},
/**
* Boot Master Process
*/
bootMaster() {
this.preBootMaster().then(sequenceResults => {
2017-12-24 05:34:47 +00:00
if (_.every(sequenceResults, rs => rs === true) && wiki.config.configMode !== 'setup') {
2017-10-08 02:44:35 +00:00
this.postBootMaster()
} else {
wiki.logger.info('Starting configuration manager...')
2017-12-24 05:34:47 +00:00
require('../setup')()
2017-10-08 02:44:35 +00:00
}
return true
}).catch(err => {
wiki.logger.error(err)
process.exit(1)
})
},
/**
* Post-Master Boot Sequence
*/
2017-12-24 05:34:47 +00:00
async postBootMaster() {
await require('../master')()
2017-10-08 02:44:35 +00:00
2017-12-24 05:34:47 +00:00
_.times(this.numWorkers, () => {
this.spawnWorker()
})
wiki.queue.uplClearTemp.add({}, {
repeat: { cron: '*/15 * * * *' }
2017-10-08 02:44:35 +00:00
})
cluster.on('exit', (worker, code, signal) => {
wiki.logger.info(`Background Worker #${worker.id} was terminated.`)
})
},
/**
* Boot Worker Process
*/
bootWorker() {
wiki.logger.info(`Background Worker #${cluster.worker.id} is initializing...`)
require('../worker')
},
/**
* Spawn new Worker process
*/
spawnWorker() {
this.workers.push(cluster.fork())
},
/**
* Set Worker count based on config + system capabilities
*/
setWorkerLimit() {
const numCPUs = require('os').cpus().length
this.numWorkers = (wiki.config.workers > 0) ? wiki.config.workers : numCPUs
if (this.numWorkers > numCPUs) {
this.numWorkers = numCPUs
}
}
}