From c30719cb670d247f001cd65223de6bb1e0a8f5ec Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 28 Jul 2018 17:52:54 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=84=E3=81=8F=E3=81=A4=E3=81=8B=E3=81=AE?= =?UTF-8?q?=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E3=83=A9=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=99=E3=82=8B=E3=81=AA=E3=81=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/index.ts | 87 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 0b9ecdc10..fff3a432d 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "bootstrap-vue": "2.0.0-rc.11", "cafy": "11.3.0", "chalk": "2.4.1", + "commander": "2.16.0", "crc-32": "1.2.0", "css-loader": "1.0.0", "dateformat": "3.0.3", diff --git a/src/index.ts b/src/index.ts index eae25762e..3698141e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import chalk from 'chalk'; import * as portscanner from 'portscanner'; import isRoot = require('is-root'); import Xev from 'xev'; +import * as program from 'commander'; import Logger from './misc/logger'; import ProgressBar from './misc/cli/progressbar'; @@ -31,6 +32,14 @@ if (process.env.NODE_ENV != 'production') { process.env.DEBUG = 'misskey:*'; } +const pkg = require('../package.json'); + +program + .version(pkg.version) + .option('--no-daemons', 'Disable daemon processes (for debbuging)') + .option('--disable-clustering', 'Disable clustering') + .parse(process.argv); + // Start app main(); @@ -38,13 +47,20 @@ main(); * Init process */ function main() { - if (cluster.isMaster) { + if (cluster.isMaster || program.disableClustering) { masterMain(); - ev.mount(); - serverStats(); - notesStats(); - } else { + if (cluster.isMaster) { + ev.mount(); + } + + if (!program.noDaemons) { + serverStats(); + notesStats(); + } + } + + if (cluster.isWorker || program.disableClustering) { workerMain(); } } @@ -66,10 +82,12 @@ async function masterMain() { Logger.succ('Misskey initialized'); - spawnWorkers(config.clusterLimit, () => { + if (!program.disableClustering) { + await spawnWorkers(config.clusterLimit); Logger.succ('All workers started'); - Logger.info(`Now listening on port ${config.port} on ${config.url}`); - }); + } + + Logger.info(`Now listening on port ${config.port} on ${config.url}`); } /** @@ -79,8 +97,10 @@ async function workerMain() { // start server await require('./server').default(); - // Send a 'ready' message to parent process - process.send('ready'); + if (cluster.isWorker) { + // Send a 'ready' message to parent process + process.send('ready'); + } } /** @@ -124,40 +144,45 @@ async function init(): Promise { } // Try to connect to MongoDB + checkMongoDb(config); + + return config; +} + +function checkMongoDb(config: Config) { const mongoDBLogger = new Logger('MongoDB'); mongoDBLogger.info(`Host: ${config.mongodb.host}`); mongoDBLogger.info(`Port: ${config.mongodb.port}`); mongoDBLogger.info(`DB: ${config.mongodb.db}`); if (config.mongodb.user) mongoDBLogger.info(`User: ${config.mongodb.user}`); if (config.mongodb.pass) mongoDBLogger.info(`Pass: ****`); - const db = require('./db/mongodb').default; + require('./db/mongodb'); mongoDBLogger.succ('Connectivity confirmed'); - db.close(); - - return config; } -function spawnWorkers(limit: number, onComplete: Function) { - // Count the machine's CPUs - const cpuCount = os.cpus().length; +function spawnWorkers(limit: number) { + return new Promise(res => { + // Count the machine's CPUs + const cpuCount = os.cpus().length; - const count = limit || cpuCount; + const count = limit || cpuCount; - const progress = new ProgressBar(count, 'Starting workers'); + const progress = new ProgressBar(count, 'Starting workers'); - // Create a worker for each CPU - for (let i = 0; i < count; i++) { - const worker = cluster.fork(); - worker.on('message', message => { - if (message === 'ready') { - progress.increment(); - } + // Create a worker for each CPU + for (let i = 0; i < count; i++) { + const worker = cluster.fork(); + worker.on('message', message => { + if (message === 'ready') { + progress.increment(); + } + }); + } + + // On all workers started + progress.on('complete', () => { + res(); }); - } - - // On all workers started - progress.on('complete', () => { - onComplete(); }); }