文档看起来非常简单,可以实现Throng以使用Heroku创建其他Web工作者。话虽如此,它给我留下了几个问题:
1)我是否还需要安装Cluster;从我读到的内容Throng是Cluster的抽象,但我没有看到下载中包含Cluster。我错过了什么,或者Throng是否准备好开箱即用?
2)我在哪里放了几行代码? Angular Fullstack在服务器目录中有一个服务器目录和一个app.js文件。这是我最好的选择,但我可以想象,它可能更好地放在配置目录中,甚至可能放在其他地方。
3)我需要以什么顺序要求Throng?我明白,如果事情发生故障,可能会搞砸。如果app.js是放置var throng = require('throng')的正确位置。
以下链接是我对Throng方向的参考: https://github.com/goodeggs/heroku-web-cluster
我将以下代码放在'start server'下的app.js文件中:
var throng = require('throng');
var WORKERS = process.env.WEB_CONCURRENCY || 1;
var start = function(){
process.on('SIGTERM', function() {
console.log('Worker exiting');
process.exit();
});
}
throng(start, {
workers: 1,
lifetime: Infinity,
});
当我这样做时,我在终端本地部署时会收到一个快速重复的错误:
Error: bind EADDRINUSE
at exports._errnoException (util.js:746:11)
at cb (net.js:1178:33)
at rr (cluster.js:592:14)
at Worker.<anonymous> (cluster.js:563:9)
at process.<anonymous> (cluster.js:692:8)
at process.emit (events.js:129:20)
at handleMessage (child_process.js:324:10)
at Pipe.channel.onread (child_process.js:352:11)
Debugger listening on port 5889
events.js:85
throw er; // Unhandled 'error' event
^
答案 0 :(得分:4)
var express = require('express');
var cluster = require('cluster'); //no need to download anything
var os = require('os'); //no need to download anything
if(cluster.isMaster) {
var numWorkers = os.cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
var app = require('express')();
var server = require('http').createServer(app);
require('./config/express')(app);
require('./routes')(app);
// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
}