2

Okay, so I have a bot hosting thing I do. It's pretty much the only thing I have right now that's keeping me sane. It's an IRC type bot for a music/chat website, and it does a lot of things. But the problem is keeping them online. I used to use a sh file that would start all the bots individually with forever.

cd dir/bots
forever start bot1.js
forever start bot2.js
...

etc, etc. And that worked. But the bots themselves take around 30mb of RAM, while the forever process ALSO takes around 30mb of RAM. So with as many bots as I was running, I was near maxed out on RAM, and that wasn't good, because if I had to get another server, things would get infinitely more complicated for me, and if I'm being honest, I'm not very good at this.

So I did some research, and figured I'd just use child.fork() and use one bot.js to spawn the rest of the bots. And it works beautifully, and cut my ram useage down to less than half of what it was. But now it's a pain keeping the bots online.

var child = require("child_process");
var running = {};
var bots = ["bot1","bot2","bot3"];
for (var i=0;i<bots.length;i++) { 
    running[bots[i]] = child.fork("bots/"+bots[i]+".js");
};

My question is - is this the most efficient way to run this setup? Because they are constantly crashing, and if I want to be considered reliable by any means, they need to be pretty self sufficient, and just shutting off in the wee hours of the night whilst I'm sleeping.

Right now I am using node-scheduler to create a fake cron job that sends a message to the bots (not a node message, because that would return as long as the js file is running, not if the bot is connected to the service) from the service, and have the bot return a command. And it sets a command, so that if it doesn't get a response from the bot in 15 seconds, it reboots it. But it doesn't seem to be working all the time. And I'm at a loss.

Any help would be appreciated, and I'll provide more details if I can.

4

2 回答 2

0

按子堆栈查看舰队。一架无人机可以管理任意数量的进程,并将自动重启任何崩溃的进程。

Fleet 通过设置集线器和连接到集线器的 1 架或多架无人机来工作。您使用 git 将代码推送到集线器。集线器会自动将新版本的代码部署到所有连接的无人机。然后就可以调用了fleet spawn -- node foo.js。Fleet 将开始运行node foo.js并在崩溃时自动重启 foo.js

于 2013-04-08T14:05:17.583 回答
0

我的解决方案是使用spawn节点内的流程,使用Promise模式来同步流程执行,然后将结果与 a 连接Promise.all(请参见promiseAll此处的函数:

var promiseAll = function(items, block, done, fail) {
    var self = this;
    var promises = [],
        index = 0;
    items.forEach(function(item) {
        promises.push(function(item, i) {
            return new Promise(function(resolve, reject) {
                if (block) {
                    block.apply(this, [item, index, resolve, reject]);
                }
            });
        }(item, ++index))
    });
    Promise.all(promises).then(function AcceptHandler(results) {
        if (done) done(results);
    }, function ErrorHandler(error) {
        if (fail) fail(error);
    });
}; //promiseAll 

现在进行导入

var cp = require('child_process');

并编写将产生每个进程的执行块:

var ExecutionBlock = function(item, index, resolve, reject) {
        var options = [
            "--ssl-protocol", "tlsv1",
            "--ignore-ssl-errors", "true"
        ];
        options.push(executableFile); // push input file path
        options.push(item); // push executable arguments
        // LP: now spawn the power!
        var child = spawn(settings.executable, options);
        // Listen for an exit event:
        child.on('exit', function(exitCode) {
            console.log("Child exited with code: " + exitCode);
            return resolve(exitCode);
        });
        // Listen for stdout data
        child.stdout.on('data', function(data) {
            console.log(data.toString());
        });
        // child error
        child.stderr.on('data',
            function(data) {
                console.log('err data: ' + data);
                // on error, kill this child
                child.kill();
                return reject(new Error(data.toString()));
            }
        );

    } //ExecutionBlock

此时,应该在inputItemsArray我们的可执行文件的参数列表中,我们可以通过以下Promise.All方法运行它们:

// inputItemsArray is a list of arguments for the executable
promiseAll(inputItemsArray, function(item, index, resolve, reject) {
    ExecutionBlock(item, index, resolve, reject);
}
,function(results) { // aggregated results

    // all execution done here. The process exitCodes will be returned
    // array index is the index of the processed that exited
}
,function(error) { // error

});

完成块会将退出代码聚合到一个数组中,因为我们已经在resolve. 通过这种方式,您可以很好地控制执行,在执行期间为每个流程exitCode最终stdout和每个流程都设置好stderr

这里的要点中的一个工作示例。

于 2016-04-13T00:07:57.017 回答