<commit_before>var util = require('util');
var EventEmitter = require('events').EventEmitter;
var fs = require('fs-extra');
var path = require('path');
var rimraf = require('rimraf');
var forever = require('forever-monitor');
var exec = require('child_process').exec;
var _ = require('lodash');
var async = require('async');
var request = require('request');
var debug     = require('debug')('gateblu:deviceManager');

var DeviceManager = function (config) {
  var self = this;
  var deviceProcesses = {};
  var runningDevices = [];
  self.refreshDevices = function (devices, callback) {
    debug('refreshDevices', _.pluck(devices, 'uuid'));
    callback = callback || _.noop;

    async.map(devices || [], self.deviceExists, function (error, devices) {
      if (error) {
        return callback(error);
      }

      devices = _.compact(devices);

      debug('devices:', devices);
      debug('runningDevices:', runningDevices);

      var devicesToStop = _.reject(runningDevices, function(device){
        return _.findWhere(devices, {uuid: device.uuid, token: device.token});
      });

      debug('devicesToStop:', devicesToStop);

      var devicesToStart = _.reject(devices, function(device){
        return _.findWhere(runningDevices, {uuid: device.uuid, token: device.token});
      });

      debug('devicesToStart:', devicesToStart);

      runningDevices = devices;

      async.each( _.pluck(devicesToStop, 'uuid'), self.stopDevice, function(error){
        self.emit('update', devices);
        self.installDevices(devicesToStart, callback);
      });

    });
  };

  self.deviceExists = function (device, callback) {
    callback = callback || _.noop;
    var authHeaders, deviceUrl;
    if (!device.connector) {
      _.defer(callback);
      return;
    }

    authHeaders = {skynet_auth_uuid: device.uuid, skynet_auth_token: device.token};
    deviceUrl = 'http://' + config.server + ':' + config.port + '/devices/' + device.uuid;
    debug('requesting device', deviceUrl);
    request({url: deviceUrl, headers: authHeaders, json: true}, function (error, response, body) {
      if (error || response.statusCode !== 200) {
        debug('error', body.error);
        debug('Error retrieving device', 'url:', deviceUrl, 'uuid:', device.uuid, 'token:', device.token);
        return callback(new Error('error retrieving devices'), null);
      }
      debug('device exists', deviceUrl);
      callback(null, _.extend(body.devices[0], device));
    });
  };

  self.installDevices = function (devices, callback) {
    callback = callback || _.noop;
    debug('installDevices', _.pluck(devices, 'uuid'));
    var connectors = _.compact(_.uniq(_.pluck(devices, 'connector')));

    async.series([
      function (callback) {
        self.installConnectors(connectors, callback);
      },
      function (callback) {<commit_msg>Prevent death on deleted subdevices<commit_after>var util = require('util');
var EventEmitter = require('events').EventEmitter;
var fs = require('fs-extra');
var path = require('path');
var rimraf = require('rimraf');
var forever = require('forever-monitor');
var exec = require('child_process').exec;
var _ = require('lodash');
var async = require('async');
var request = require('request');
var debug     = require('debug')('gateblu:deviceManager');

var DeviceManager = function (config) {
  var self = this;
  var deviceProcesses = {};
  var runningDevices = [];
  self.refreshDevices = function (devices, callback) {
    debug('refreshDevices', _.pluck(devices, 'uuid'));
    callback = callback || _.noop;

    async.map(devices || [], self.deviceExists, function (error, devices) {
      if (error) {
        console.error(error, 'Error verifying devices. Refusing to be useful');
        return callback(error);
      }

      devices = _.compact(devices);

      debug('devices:', devices);
      debug('runningDevices:', runningDevices);

      var devicesToStop = _.reject(runningDevices, function(device){
        return _.findWhere(devices, {uuid: device.uuid, token: device.token});
      });

      debug('devicesToStop:', devicesToStop);

      var devicesToStart = _.reject(devices, function(device){
        return _.findWhere(runningDevices, {uuid: device.uuid, token: device.token});
      });

      debug('devicesToStart:', devicesToStart);

      runningDevices = devices;

      async.each( _.pluck(devicesToStop, 'uuid'), self.stopDevice, function(error){
        self.emit('update', devices);
        self.installDevices(devicesToStart, callback);
      });

    });
  };

  self.deviceExists = function (device, callback) {
    callback = callback || _.noop;
    var authHeaders, deviceUrl;
    if (!device.connector) {
      _.defer(callback);
      return;
    }

    authHeaders = {skynet_auth_uuid: device.uuid, skynet_auth_token: <PASSWORD>.token};
    deviceUrl = 'http://' + config.server + ':' + config.port + '/devices/' + device.uuid;
    debug('requesting device', deviceUrl);
    request({url: deviceUrl, headers: authHeaders, json: true}, function (error, response, body) {
      if (error || response.statusCode !== 200) {
        return callback(error, null);
      }
      debug('device exists', deviceUrl);
      callback(null, _.extend(body.devices[0], device));
    });
  };

  self.installDevices = function (devices, callback) {
    callback = callback || _.noop;
    debug('installDevices', _.pluck(devices, 'uuid'));
    var connectors = _.compact(_.uniq(_.pluck(devices, 'connector')));

    async.series([
      function (callback) {
        self.installConnectors(connectors, callback);
      },
      function (callback) {
{'fraction_non_alphanumeric': 0.15424301725671588, 'fraction_numerical': 0.0014232342999466287, 'mean_word_length': 3.593137254901961, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2286019', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1583, 'n_words': 376}