Skip to content

Commit

Permalink
[api] Sketch of Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki committed Dec 9, 2011
1 parent 34ccb24 commit f06c345
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/forever/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var fs = require('fs'),
path = require('path'),
nssocket = require('nssocket'),
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) {
if (err.code == 'ENOENT') {
return callback(null, sock)
}
return callback(err);
}
return findSocket(sockPath, ++startAt, callback);
});
}

var Worker = exports.Worker = function (options) {
this.monitor = options.monitor;
this.sockPath = options.sockPath || forever.config.get('sockPath');

this._socket = null;
};

Worker.prototype.start = function (cb) {
var self = this;

if (this._socket) throw new Error("Can't start already started worker");

self._socket = nssocket.createServer(function (socket) {
socket.data(['ping'], function () {
socket.send(['pong']);
});
});

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

0 comments on commit f06c345

Please sign in to comment.