尝试在节点集群后面扩展时 phantomjs-node 崩溃

2024-04-13

相关 GitHub 问题:https://github.com/sgentle/phantomjs-node/issues/280 https://github.com/sgentle/phantomjs-node/issues/280

我有一个简单的应用程序,可以执行以下操作:

var
    phantom = require('phantom'),
    express = require('express'),
    serve = express();

serve.get('/foo', function (req, res) {
    try {
        phantom.create(function (ph) {
            console.log('Phantom browser created w/ pid: ', ph.process.pid);
            ph.onError = function (msg, trace) {
                var msgStack = ['PHANTOM ERROR: ' + msg];
                if (trace && trace.length) {
                    msgStack.push('TRACE:');
                    trace.forEach(function (t) {
                        msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
                    });
                }
                console.log(msgStack.join('\n'));
            };
            ph.createPage(function(page){
                page.open('http://www.stackoverflow.com', function(status){
                    res.json({pageStatus: status});
                    page.close();
                    ph.exit();
                });
            });
        });
    } catch(e){
        console.log(e);
        throw e;
    }

});

只要我在 WebStorm 中运行它,它就运行得很好,我可以点击/foo端点有我想要的尽可能多的并发请求,并且一切都按预期工作。

但一旦我尝试将其扩展到 PM2 之后pm2 start -i 0 phantomapp.js,只要我不向它提出太多请求,一切仍然可以正常工作。第二次我打开两个浏览器窗口并在两个窗口中点击刷新,我得到以下内容pm2 logs。此时,PM2 管理的所有 8 个进程就“消失”了!我究竟做错了什么?? :

PM2: 2015-05-27 18:53:17: [PM2] Error caught by domain:
PM2: AssertionError: false == true
PM2:     at RoundRobinHandle.add (cluster.js:140:3)
PM2:     at queryServer (cluster.js:480:12)
PM2:     at Worker.onmessage (cluster.js:438:7)
PM2:     at ChildProcess.<anonymous> (cluster.js:692:8)
PM2:     at ChildProcess.emit (events.js:129:20)
PM2:     at handleMessage (child_process.js:324:10)
PM2:     at Pipe.channel.onread (child_process.js:352:11)
PM2: 2015-05-27 18:53:18: [PM2] Automatic `pm2 update` failed. Killing PM2 daemon and its processes...
PM2: 2015-05-27 18:53:18: pm2 has been killed by signal, dumping process list before exit...
PM2: 2015-05-27 18:53:18: Stopping app:phantomapp id:0
PM2: assert.js:86
PM2:   throw new assert.AssertionError({
PM2:         ^
PM2: AssertionError: false == true
PM2:     at RoundRobinHandle.add (cluster.js:140:3)
PM2:     at queryServer (cluster.js:480:12)
PM2:     at Worker.onmessage (cluster.js:438:7)
PM2:     at ChildProcess.<anonymous> (cluster.js:692:8)
PM2:     at ChildProcess.emit (events.js:129:20)
PM2:     at handleMessage (child_process.js:324:10)
PM2:     at Pipe.channel.onread (child_process.js:352:11)
phantomapp-0 (err): Process disconnected from parent !
PM2: [PM2] Spawning PM2 daemon
PM2: 2015-05-27 18:53:18: [PM2][WORKER] Started with refreshing interval: 30000
PM2: 2015-05-27 18:53:18: [[[[ PM2/God daemon launched ]]]]
PM2: 2015-05-27 18:53:18: BUS system [READY] on port /Users/matthewmarcus/.pm2/pub.sock
PM2: 2015-05-27 18:53:18: RPC interface [READY] on port /Users/matthewmarcus/.pm2/rpc.sock
PM2: [PM2] PM2 Successfully daemonized
PM2: Be sure to have the latest version by doing `npm install pm2@latest -g` before doing this procedure.
PM2: [PM2] Stopping PM2...
PM2: [PM2][WARN] No process found
PM2: [PM2] All processes have been stopped and deleted
PM2: 2015-05-27 18:53:18: PM2 is being killed via kill method
PM2: 2015-05-27 18:53:18: RPC socket closed
PM2: 2015-05-27 18:53:18: PUB socket closed
PM2: [PM2] PM2 stopped
PM2: 2015-05-27 18:53:19: [PM2][WORKER] Started with refreshing interval: 30000
PM2: 2015-05-27 18:53:19: [[[[ PM2/God daemon launched ]]]]
PM2: 2015-05-27 18:53:19: BUS system [READY] on port /Users/matthewmarcus/.pm2/pub.sock
PM2: 2015-05-27 18:53:19: RPC interface [READY] on port /Users/matthewmarcus/.pm2/rpc.sock
PM2: >>>>>>>>>> PM2 updated
PM2: ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬────────┬──────────┐
PM2: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
PM2: └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴────────┴──────────┘
PM2:  Use `pm2 show <id|name>` to get more details about an app

您可以使用节点本机集群重现相同的问题:

var cluster = require('cluster');

if(cluster.isMaster){
    var cpuCount = require('os').cpus().length;

    for (var i = 0; i < cpuCount; i++){
        cluster.fork();
    }

    cluster.on('exit', function(worker){
        console.log('Worker ' + woker.id + ' died. Forking...');
        cluster.fork();
    });

} else {
    var
        phantom = require('phantom'),
        express = require('express'),
        serve = express();

    serve.get('/foo', function (req, res) {
        try {
            phantom.create(function (ph) {
                console.log('Phantom browser created w/ pid: ', ph.process.pid);
                ph.onError = function (msg, trace) {
                    var msgStack = ['PHANTOM ERROR: ' + msg];
                    if (trace && trace.length) {
                        msgStack.push('TRACE:');
                        trace.forEach(function (t) {
                            msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
                        });
                    }
                    console.log(msgStack.join('\n'));
                };
                ph.createPage(function(page){
                    page.open('http://www.stackoverflow.com', function(status){
                        res.json({pageStatus: status});
                        page.close();
                        ph.exit();
                    });
                });
            });
        } catch(e){
            console.log(e);
            throw e;
        }

    });
}

当尝试同时处理 > 1 个请求时,会在控制台中产生以下错误:

assert.js:86
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at RoundRobinHandle.add (cluster.js:140:3)
    at queryServer (cluster.js:480:12)
    at Worker.onmessage (cluster.js:438:7)
    at ChildProcess.<anonymous> (cluster.js:692:8)
    at ChildProcess.emit (events.js:129:20)
    at handleMessage (child_process.js:324:10)
    at Pipe.channel.onread (child_process.js:352:11)

我尝试过这个要点 https://gist.github.com/soyuka/00ef5cc5327bd4810979我能够创建幻影浏览器并获得响应。

  • 如果我运行两个选项卡localhost:3000/foo,只有当第一个结束时才会生成第二个。
  • If I run more than two concurrent requests in my browser (chrome), I get the error too.
    • 如果我尝试运行比集群数量更多的请求(来自 bash),它就会崩溃。
  • 如果我从 bash 脚本运行请求(小于或等于集群数量),它根本不会崩溃。

    另外,我注意到使用 bash 脚本我同时运行所有内容:

    http GET localhost:3000/foo &
    http GET localhost:3000/foo &
    http GET localhost:3000/foo &
    http GET localhost:3000/foo &
    

您看到的错误来自这个断言 https://github.com/joyent/node/blob/master/lib/cluster.js#L142:

  assert(worker.id in this.all === false);

这意味着该工作人员不再处于循环中。

我不认为这与 PM2 直接相关,但有一个错误。 PM2 不应该崩溃。我建议你的报告问题 https://github.com/Unitech/PM2通过引用这个stackoverflow。但不确定是否可以修复它是否是节点错误。

遗憾的是,目前还没有解决此问题的方法,但您可以看一下节点问题 https://github.com/joyent/node/issues/9261。我读过 iojs 修复了这个问题,也许你可以尝试一下;)。

另一个相关的 stackoverflow:NodeJS 集群意外断言.AssertionError https://stackoverflow.com/questions/21217285/nodejs-cluster-unexpected-assert-assertionerror.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试在节点集群后面扩展时 phantomjs-node 崩溃 的相关文章

随机推荐