Permalink
Browse files

Merge pull request #218 from MiguelMadero/serverportfailure

Checks if a port is open before starting the server
  • Loading branch information...
2 parents 9312e42 + c77c155 commit fddf752331c3405be30a354975bc262436c9c482 @larzconwell larzconwell committed Oct 10, 2012
Showing with 18 additions and 5 deletions.
  1. +18 −5 lib/cluster/worker.js
View
@@ -71,15 +71,28 @@ Worker.prototype = new (function () {
this.startServer = function () {
if (this.config) {
var hostname = this.config.hostname || null
+ , self = this
+ , port = parseInt(this.config.port, 10)
, ssl = this.config.ssl ? ' (SSL)' : ''
, spdy = this.config.spdy ? '(SPDY)' : '';
- this.server.listen(parseInt(this.config.port, 10), hostname);
+ utils.network.isPortOpen(port, hostname, function (err, isOpen) {
+ if (err) {
+ self.log.error(err);
+ }
+ else if (isOpen) {
+ self.log.error("The port " + port + " is already in use.");
+ }
+ else {
+ self.server.listen(port, hostname, function () {
- this.log.info('Server worker running in ' + this.config.environment +
- ' on port ' + this.config.port + ssl + spdy + ' with a PID of: ' + process.pid);
- this.log.debug('LOGGING STARTED ============================================');
- this.log.debug('============================================================');
+ self.log.info('Server worker running in ' + self.config.environment +
+ ' on port ' + self.config.port + ssl + spdy + ' with a PID of: ' + process.pid);
+ self.log.debug('LOGGING STARTED ============================================');
+ self.log.debug('============================================================');
+ });
+ }
+ });
}
else {
this.log.error('Cannot start server without config.');

0 comments on commit fddf752

Please sign in to comment.