Permalink
Browse files

create and cull procs

  • Loading branch information...
1 parent d838074 commit 956ee7047ae614a299045ca68f63a318a7fd030d @substack committed Apr 22, 2012
Showing with 46 additions and 3 deletions.
  1. +46 −3 index.js
View
49 index.js
@@ -2,6 +2,7 @@ var airport = require('airport');
var seaport = require('seaport');
var dnode = require('dnode');
var marx = require('marx');
+var spawn = require('child_process').spawn;
module.exports = function (ports) {
var isSeaport = ports && typeof ports === 'object'
@@ -49,14 +50,56 @@ module.exports = function (ports) {
function drone (ports, plan) {
if (!plan) prevPlan = {};
+ var procs = {};
var service = function (remote, conn) {
this.plan = function (workers, plan) {
// todo: query zygote roles at the start and subscribe to update
- prevPlan = plan;
- var work = marx(workers, plan)[id] || {};
- console.dir(work);
+ var work = Object.keys(plan).reduce(function (acc, name) {
+ acc[name] = plan[name].number;
+ return acc;
+ }, {});
+
+ var share = marx(workers, work)[id] || {};
+
+ var names = Object.keys(prevPlan).concat(Object.keys(share));
+ names.forEach(function (name) {
+ var diff = (share[name] || 0) - (prevPlan[name] || 0);
+ var cmd = plan[name].command;
+ if (!Array.isArray(cmd)) cmd = cmd.split(' ');
+
+ for (var i = diff; i < 0; i++) {
+ // cull excess services
+ procs[name]
+ .removeAllListeners('exit')
+ .on('exit', function () {
+ delete procs[name];
+ })
+ ;
+ procs[name].kill('SIGHUP');
+ }
+
+ function createProc (cmd) {
+ var ps = spawn(cmd[0], cmd.slice(1));
+ ps.stdout.pipe(process.stdout, { end : false });
+ ps.stderr.pipe(process.stderr, { end : false });
+ return ps;
+ }
+
+ for (var i = 0; i < diff; i++) {
+ // spawn extra services
+ procs[name] = createProc(cmd);
+
+ procs[name].on('exit', function () {
+ setTimeout(function () {
+ procs[name] = createProc(cmd);
+ }, 1000);
+ });
+ }
+ });
+
+ prevShare = share;
};
};

0 comments on commit 956ee70

Please sign in to comment.