var cluster = require('cluster');
var os = require('os');
var numCPUs = os.cpus().length;
var express = require('./express');
var zxy = require('zxy-comm');
var _ = require('underscore');

var runNode = function (conf, all, middleware) {
    var singleCoreProcess;
    if (process.argv) {
        if (process.argv.length) {
            singleCoreProcess = process.argv[process.argv.length - 1].indexOf('-d') == 0 ? true : false
        }
        else {
            singleCoreProcess = process.argv[0].indexOf('-d') == 0 ? true : false
        }
    }
    if (cluster.isMaster && !singleCoreProcess) {
        var c_log = console.log, c_warn = console.warn, c_info = console.info, c_error = console.error;
        var isfirstCore = true;
        var firstCorePid = 0;
        require('os').cpus().forEach(function () {
            var worker = cluster.fork();
            if (isfirstCore) {
                firstCorePid = worker.process.pid;
                isfirstCore = false;
                runjob();
            }
        });
        cluster.on('exit', function (worker, code, signal) {
            var pid = worker.process.pid;
            c_warn('process[' + pid + '] with #' + worker.id + ' exit');
            worker = cluster.fork();
            if (firstCorePid == pid) {
                firstCorePid = worker.process.pid;
                // runjob();
            }
        })
            .on("death", function (worker) {
                var pid = worker.process.pid;
                c_warn('process[' + pid + '] with #' + worker.id + ' death');
                worker = cluster.fork();
                if (firstCorePid == pid) {
                    firstCorePid = worker.process.pid;
                    // runjob();
                }
            })
            .on('listening', function (worker, address) {
                var serverip = zxy.app.m_obtain.getServerIP();
                c_info("process[" + worker.process.pid + "] with #" + worker.id + " express server listening on " + serverip + ":" + address.port);
            });
        Object.keys(cluster.workers).forEach(function (id) {
            cluster.workers[id].on('message', function (msg) {
                c_info("process[" + cluster.workers[id].process.pid + "] with #" + id + msg);
            });
        });
    } else {
        express.runExpress(conf.express, middleware);
        if (all) {
            zxy.runMysql(conf.mysql);
            process.send && process.send("being connected to the MySQL server " + conf.mysql.host + ":" + (conf.mysql.port ? conf.mysql.port : 3306));
            // process.send && process.send("mysql connected to " + conf.mysql.host + ":" + (conf.mysql.port ? conf.mysql.port : 3306));
            zxy.runRedis(conf.redis);
            process.send && process.send("being connected to the Redis server " + conf.redis.host + ":" + (conf.redis.port ? conf.redis.port : 6379));
            // process.send && process.send("redis connected to " + conf.redis.host + ":" + (conf.redis.port ? conf.redis.port : 6379));
        }
    }
}
exports.runNode = runNode;

function runjob() {
    if (zxy.app.f_job) {
        if (zxy.app.m_verify.isArray(zxy.app.f_job)) {
            _.each(zxy.app.f_job, function (job) {
                if (zxy.app.m_verify.isJson(job)) {
                    var job = new require('./comm/job').jobObj(job.conf, job.func, job.params, job.name);
                }
            });
        }
    }
}