diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-01-24 10:28:14 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-01-24 10:28:14 +0900 |
| commit | 5cf067c814343a2dda32ce95ae959c7d4bc2f6ac (patch) | |
| tree | 04b3013049cc8c2a8fb8e41356b53440d93a28be /src | |
| parent | Update README.md (diff) | |
| download | misskey-5cf067c814343a2dda32ce95ae959c7d4bc2f6ac.tar.gz misskey-5cf067c814343a2dda32ce95ae959c7d4bc2f6ac.tar.bz2 misskey-5cf067c814343a2dda32ce95ae959c7d4bc2f6ac.zip | |
[Server] Refactor
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/src/index.ts b/src/index.ts index 6ce6ab2ee4..aee566be6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ Error.stackTraceLimit = Infinity; import * as fs from 'fs'; import * as os from 'os'; import * as cluster from 'cluster'; +import * as debug from 'debug'; import Logger from './utils/logger'; import * as chalk from 'chalk'; import portUsed = require('tcp-port-used'); @@ -23,6 +24,8 @@ import DependencyInfo from './utils/dependencyInfo'; import { path as configPath } from './config'; import loadConfig from './config'; +const clusterLog = debug('misskey:cluster'); + // Init babel require('babel-core/register'); require('babel-polyfill'); @@ -41,7 +44,7 @@ main(); /** * Init proccess */ -function main(): void { +function main() { if (cluster.isMaster) { masterMain(); } else { @@ -52,7 +55,7 @@ function main(): void { /** * Init master proccess */ -async function masterMain(): Promise<void> { +async function masterMain() { let initResult: InitResult; try { @@ -76,35 +79,15 @@ async function masterMain(): Promise<void> { return; } - const config = loadConfig(); - spawnWorkers(() => { - Logger.info(chalk.bold.green(`Now listening on port ${config.port}`)); - - // Listen new workers - cluster.on('fork', worker => { - console.log(`Process forked: [${worker.id}]`); - }); - - // Listen online workers - cluster.on('online', worker => { - console.log(`Process is now online: [${worker.id}]`); - }); - - // Listen for dying workers - cluster.on('exit', worker => { - // Replace the dead worker, - // we're not sentimental - console.log(chalk.red(`[${worker.id}] died :(`)); - cluster.fork(); - }); + Logger.info(chalk.bold.green(`Now listening on port ${loadConfig().port}`)); }); } /** * Init worker proccess */ -function workerMain(): void { +function workerMain() { // start server require('./server'); } @@ -112,7 +95,7 @@ function workerMain(): void { /** * Init app */ -async function init(): Promise<InitResult> { +async function init() { let warn = false; Logger.info('Welcome to Misskey!'); @@ -160,7 +143,7 @@ async function init(): Promise<InitResult> { return warn ? InitResult.Warn : InitResult.Success; } -function spawnWorkers(onComplete: any): void { +function spawnWorkers(onComplete: any) { // Count the machine's CPUs const cpuCount = os.cpus().length; @@ -182,6 +165,24 @@ function spawnWorkers(onComplete: any): void { }); } +// Listen new workers +cluster.on('fork', worker => { + clusterLog(`Process forked: [${worker.id}]`); +}); + +// Listen online workers +cluster.on('online', worker => { + clusterLog(`Process is now online: [${worker.id}]`); +}); + +// Listen for dying workers +cluster.on('exit', worker => { + // Replace the dead worker, + // we're not sentimental + clusterLog(chalk.red(`[${worker.id}] died :(`)); + cluster.fork(); +}); + // Display detail of unhandled promise rejection process.on('unhandledRejection', console.dir); |