Skip to content

Commit

Permalink
[refactor] Create unique worker socket files using the microtime mo…
Browse files Browse the repository at this point in the history
…dule
  • Loading branch information
indexzero committed Jan 14, 2012
1 parent 72dac45 commit 1fa4943
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
3 changes: 3 additions & 0 deletions examples/cli-multiple-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
forever start examples/server.js -p 8080
forever start examples/server.js -p 8081
forever start examples/server.js -p 8082
38 changes: 11 additions & 27 deletions lib/forever/worker.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
var events = require('events'),
fs = require('fs'),
path = require('path'),
microtime = require('microtime'),
nssocket = require('nssocket'),
utile = require('utile'),
forever = require('../forever');

function findSocket(sockPath, startAt, callback) {
if (typeof startAt == "function") {
callback = startAt;
startAt = null;
}

startAt || (startAt = 0);
var sock = path.join(sockPath, 'worker.' + startAt + '.sock');
fs.stat(sock, function (err, stat) {
if (err) {
return err.code == 'ENOENT'
? callback(null, sock)
: callback(err);
}

return findSocket(sockPath, ++startAt, callback);
});
}

var Worker = exports.Worker = function (options) {
events.EventEmitter.call(this);
options || (options = {});
Expand Down Expand Up @@ -116,14 +98,16 @@ Worker.prototype.start = function (callback) {
callback && callback(err);
});

findSocket(self.sockPath, function (err, sock) {
if (err) {
return callback && callback(err);
}

self._sockFile = sock;
self._socket.listen(sock);
});
//
// Create a unique socket file based on the current microtime.
//
var sock = self._sockFile = path.join(self.sockPath, [
'worker',
microtime.nowStruct().join(''),
'sock'
].join('.'));

self._socket.listen(sock);
}

//
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"cliff": "0.x.x",
"daemon": "0.4.x",
"flatiron": "0.1.x",
"microtime": "0.2.x",
"minimatch": "0.0.x",
"nconf": "0.5.x",
"nssocket": "0.3.x",
Expand Down

0 comments on commit 1fa4943

Please sign in to comment.