当尝试在节点集群后扩展时,Phantomjs-node崩溃
phantomjs-node crashes when attempting to scale behind node-cluster
GitHub相关问题: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
端点w/尽可能多的并发请求,一切都像预期的那样工作。
但是一旦我尝试将其扩展到PM2 w/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)
我尝试了这个要点,我能够创建虚拟浏览器并获得响应。
- 如果我在
localhost:3000/foo
上运行两个选项卡,只有当第一个选项卡结束时才会生成第二个选项卡。 - 如果我在浏览器 (chrome)中运行两个以上的并发请求,我也会得到错误。
- 如果我试图运行更多的请求(从一个bash)超过集群的数量,它崩溃。
-
如果我从bash脚本中运行请求(小于或等于集群的数量),它根本不会崩溃。
另外,我注意到在bash脚本中,我以并发方式运行所有内容:
http GET localhost:3000/foo & http GET localhost:3000/foo & http GET localhost:3000/foo & http GET localhost:3000/foo &
您看到的错误来自以下断言:
assert(worker.id in this.all === false);
表示该worker不再处于轮询中。
我不认为这是直接相关的PM2,但有一个bug。PM2不应该崩溃。我建议你通过引用这个stackoverflow来报告一个问题。如果它是一个节点的错误,不确定它可以被修复。
遗憾的是,目前还没有解决这个问题,但你可以看看nodejs问题。我读到iojs修复了这个问题,也许你可以试试;)。
另一个相关的stackoverflow: NodeJS Cluster unexpected assert.AssertionError.
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- TableExport jquery插件:文件名和扩展名问题
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- Javascript”;类“;带有参数的扩展
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- PhantomJS无法打开扩展名未知的本地文件
- PhantomJS onResourceReceived no stream
- 当尝试在节点集群后扩展时,Phantomjs-node崩溃