[Server] Refactor

This commit is contained in:
syuilo 2017-01-24 10:28:14 +09:00
parent 1d62a17c3c
commit 5cf067c814

View file

@ -10,6 +10,7 @@ Error.stackTraceLimit = Infinity;
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os'; import * as os from 'os';
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import * as debug from 'debug';
import Logger from './utils/logger'; import Logger from './utils/logger';
import * as chalk from 'chalk'; import * as chalk from 'chalk';
import portUsed = require('tcp-port-used'); import portUsed = require('tcp-port-used');
@ -23,6 +24,8 @@ import DependencyInfo from './utils/dependencyInfo';
import { path as configPath } from './config'; import { path as configPath } from './config';
import loadConfig from './config'; import loadConfig from './config';
const clusterLog = debug('misskey:cluster');
// Init babel // Init babel
require('babel-core/register'); require('babel-core/register');
require('babel-polyfill'); require('babel-polyfill');
@ -41,7 +44,7 @@ main();
/** /**
* Init proccess * Init proccess
*/ */
function main(): void { function main() {
if (cluster.isMaster) { if (cluster.isMaster) {
masterMain(); masterMain();
} else { } else {
@ -52,7 +55,7 @@ function main(): void {
/** /**
* Init master proccess * Init master proccess
*/ */
async function masterMain(): Promise<void> { async function masterMain() {
let initResult: InitResult; let initResult: InitResult;
try { try {
@ -76,35 +79,15 @@ async function masterMain(): Promise<void> {
return; return;
} }
const config = loadConfig();
spawnWorkers(() => { spawnWorkers(() => {
Logger.info(chalk.bold.green(`Now listening on port ${config.port}`)); Logger.info(chalk.bold.green(`Now listening on port ${loadConfig().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();
});
}); });
} }
/** /**
* Init worker proccess * Init worker proccess
*/ */
function workerMain(): void { function workerMain() {
// start server // start server
require('./server'); require('./server');
} }
@ -112,7 +95,7 @@ function workerMain(): void {
/** /**
* Init app * Init app
*/ */
async function init(): Promise<InitResult> { async function init() {
let warn = false; let warn = false;
Logger.info('Welcome to Misskey!'); Logger.info('Welcome to Misskey!');
@ -160,7 +143,7 @@ async function init(): Promise<InitResult> {
return warn ? InitResult.Warn : InitResult.Success; return warn ? InitResult.Warn : InitResult.Success;
} }
function spawnWorkers(onComplete: any): void { function spawnWorkers(onComplete: any) {
// Count the machine's CPUs // Count the machine's CPUs
const cpuCount = os.cpus().length; 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 // Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir); process.on('unhandledRejection', console.dir);