// G!Service
import * as Cluster from 'cluster';
import * as Http from 'http';
import { cpus } from 'os';

import Index from './index';
import Config from './config/config';

const numCPUs: number = cpus().length;

if (Cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) {
        const worker: Cluster.Worker = Cluster.fork();
        worker.send(worker.process.pid);
    }

    Cluster.on('listening', (worker: Cluster.Worker, address: Cluster.Address) => {
        console.log('worker ' + worker.process.pid + ', listen: ' + address.address + ":" + address.port)
    });

    Cluster.on('exit', (worker: Cluster.Worker, code: number, signal: string) => {
        console.log('worker ' + worker.process.pid + ' died');
        Cluster.fork();
    });
} else {
    const HttpServer: Http.Server = Http.createServer(Index);
    HttpServer.listen(Config.portNumber);
}
