This commit is contained in:
syuilo 2018-11-06 03:48:23 +09:00
parent 6c5a78aeb2
commit e9a3495225
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
2 changed files with 12 additions and 97 deletions

View File

@ -17,7 +17,6 @@ import * as program from 'commander';
import mongo from './db/mongodb'; import mongo from './db/mongodb';
import Logger from './misc/logger'; import Logger from './misc/logger';
import ProgressBar from './misc/cli/progressbar';
import EnvironmentInfo from './misc/environmentInfo'; import EnvironmentInfo from './misc/environmentInfo';
import MachineInfo from './misc/machineInfo'; import MachineInfo from './misc/machineInfo';
import serverStats from './daemons/server-stats'; import serverStats from './daemons/server-stats';
@ -87,10 +86,9 @@ async function masterMain() {
if (!program.disableClustering) { if (!program.disableClustering) {
await spawnWorkers(config.clusterLimit); await spawnWorkers(config.clusterLimit);
Logger.succ('All workers started');
} }
Logger.info(`Now listening on port ${config.port} on ${config.url}`); Logger.succ(`Now listening on port ${config.port} on ${config.url}`);
} }
/** /**
@ -168,28 +166,30 @@ function checkMongoDb(config: Config) {
} }
function spawnWorkers(limit: number) { function spawnWorkers(limit: number) {
Logger.info('Starting workers...');
return new Promise(res => { return new Promise(res => {
// Count the machine's CPUs // Count the machine's CPUs
const cpuCount = os.cpus().length; const cpuCount = os.cpus().length;
const count = limit || cpuCount; const count = limit || cpuCount;
let started = 0;
const progress = new ProgressBar(count, 'Starting workers');
// Create a worker for each CPU // Create a worker for each CPU
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
const worker = cluster.fork(); const worker = cluster.fork();
worker.on('message', message => {
if (message === 'ready') {
progress.increment();
}
});
}
// On all workers started worker.on('message', message => {
progress.on('complete', () => { if (message !== 'ready') return;
started++;
// When all workers started
if (started == count) {
Logger.succ('All workers started');
res(); res();
}
}); });
}
}); });
} }

View File

@ -1,85 +0,0 @@
import { EventEmitter } from 'events';
import * as readline from 'readline';
import chalk from 'chalk';
/**
* Progress bar
*/
export default class extends EventEmitter {
public max: number;
public value: number;
public text: string;
private indicator: number;
constructor(max: number, text: string = null) {
super();
this.max = max;
this.value = 0;
this.text = text;
this.indicator = 0;
this.draw();
const iclock = setInterval(() => {
this.indicator = (this.indicator + 1) % 4;
this.draw();
}, 200);
this.on('complete', () => {
clearInterval(iclock);
});
}
public increment(): void {
this.value++;
this.draw();
// Check if it is fulfilled
if (this.value === this.max) {
this.indicator = null;
cll();
process.stdout.write(`${this.render()} -> ${chalk.bold('Complete')}\n`);
this.emit('complete');
}
}
public draw(): void {
const str = this.render();
cll();
process.stdout.write(str);
}
private render(): string {
const width = 30;
const t = this.text ? `${this.text} ` : '';
const v = Math.floor((this.value / this.max) * width);
const vs = new Array(v + 1).join('*');
const p = width - v;
const ps = new Array(p + 1).join(' ');
const percentage = Math.floor((this.value / this.max) * 100);
const percentages = chalk.gray(`(${percentage} %)`);
let i: string;
switch (this.indicator) {
case 0: i = '-'; break;
case 1: i = '\\'; break;
case 2: i = '|'; break;
case 3: i = '/'; break;
case null: i = '+'; break;
}
return `${i} ${t}[${vs}${ps}] ${this.value} / ${this.max} ${percentages}`;
}
}
/**
* Clear current line
*/
function cll(): void {
readline.clearLine(process.stdout, 0); // Clear current text
readline.cursorTo(process.stdout, 0, null); // Move cursor to the head of line
}