<commit_before>var cluster = require('cluster');
var debug = require('debug')('strong-supervisor:channel');
var master = require('strong-cluster-control');
var server = require('strong-control-channel/server').create(onRequest);

// FIXME ctrl-C the supervisor leaves runctl pipe

exports.start = start;
exports.onRequest = onRequest; // For testing

function start(options) {
  var logger = options.logger;

  server.listen(options.channel);

  server.on('error', function(er) {
    logger.error('control channel failed to listen on `%s`: %s',
                 options.channel, er);
    // XXX exit? probably, they asked, but did not get a channel
  });
  // server.on('listening', ...)


  master.on('stop', function() {
    server.close();
  });
  return server;
};

// XXX all the usual problems to check for:
//   start/stop callbacks
//   exit on failure
//   etc.

function onRequest(req, callback) {
  debug('request', req);

  var cmd = req.cmd;
  var rsp = {
  };

  if(cmd === 'status') {
    rsp = master.status();
  } else if(cmd === 'set-size') {
    try {
      master.setSize(req.size);
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'stop') {
    try {
      master.stop();
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'restart') {
    try {
      master.restart();
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'disconnect') {
    cluster.disconnect();

  } else if(cmd === 'fork') {
    cluster.fork();

  } else {
    rsp.error = 'unsupported command ' + req.cmd;
  }

  if(callback) {
    process.nextTick(callback.bind(null, rsp));
  }
}
<commit_msg>run: unlink control channel on startup<commit_after>var cluster = require('cluster');
var debug = require('debug')('strong-supervisor:channel');
var master = require('strong-cluster-control');
var server = require('strong-control-channel/server').create(onRequest);

exports.start = start;
exports.onRequest = onRequest; // For testing

function start(options) {
  var logger = options.logger;

  // XXX(sam) I don't like this 'last one wins' approach, but its impossible to
  // prevent the channel outliving sl-run under all conditions, this is the only
  // robust way I've found.
  try {
    fs.unlinkSync(options.channel);
  } catch(er) {
    // Didn't exist
  }

  server.listen(options.channel);

  server.on('error', function(er) {
    logger.error('control channel failed to listen on `%s`: %s',
                 options.channel, er);
    throw er;
  });


  master.on('stop', function() {
    server.close();
  });
  return server;
};

function onRequest(req, callback) {
  debug('request', req);

  var cmd = req.cmd;
  var rsp = {
  };

  if(cmd === 'status') {
    rsp = master.status();
  } else if(cmd === 'set-size') {
    try {
      master.setSize(req.size);
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'stop') {
    try {
      master.stop();
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'restart') {
    try {
      master.restart();
    } catch(er) {
      rsp.error = er.message;
    }

  } else if(cmd === 'disconnect') {
    cluster.disconnect();

  } else if(cmd === 'fork') {
    cluster.fork();

  } else {
    rsp.error = 'unsupported command ' + req.cmd;
  }

  if(callback) {
    process.nextTick(callback.bind(null, rsp));
  }
}

{'fraction_non_alphanumeric': 0.1680473372781065, 'fraction_numerical': 0.0, 'mean_word_length': 2.760845383759733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3340374', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1107, 'n_words': 264}