<commit_before>'use strict';

let _ = require('lodash'),
  config = require('../../config'),
  Machine = require('../connectors').Machine;

class MachineManager {
  constructor(cluster, node) {
    this.cluster = cluster;
    this.node    = node;
    this.machine = new Machine(config.auth.machine);
  }
  deploy() {
    return this.node.validate().then(err => {
      if (err) { return Promise.reject(err); }

      return this._createMachine();
    }).then(id => {
      this.node.provider_id = id;

      return this.cluster.addNode(this.node);
    }).then(() => {
      return this.cluster.notify(this.deployChanges);
    }).then(() => {
      return this.node.createAction({ type: 'deploy' });
    });
  }
  destroy() {
    return this._deleteMachine().then(() => {
      return this.node.destroy();
    }).then(() => {
      return this.cluster.notify(this.destroyChanges);
    }).then(() => {
      return this.node.getActions();
    }).then(actions => {
      return Promise.all(_.invoke(actions, 'destroy'));
    });
  }
  get deployChanges() {
    return _.pick(this.node, 'last_state');
  }
  get destroyChanges() {
    let changes = this.node.master ? { last_seen: null } : {};

    return _.merge(changes, { last_state: 'destroyed' });
  }
  _createMachine() {
    return this.node.byon ? Promise.resolve() : this.machine.create({
      name:   this.node.id,
      region: this.node.region,
      size:   this.node.size
    });
  }
  _deleteMachine()  {
    let deletion = this.node.byon ? Promise.resolve() : this.machine.delete(
      this.node.provider_id
    );
    return deletion.catch(err => {
      if (err.name !== 'MachineNotFoundError') { throw err; }
    });
  }
}

module.exports = MachineManager;<commit_msg>node deletion: ignore errors on machine not found<commit_after>'use strict';

let _ = require('lodash'),
  config = require('../../config'),
  Machine = require('../connectors').Machine;

class MachineManager {
  constructor(cluster, node) {
    this.cluster = cluster;
    this.node    = node;
    this.machine = new Machine(config.auth.machine);
  }
  deploy() {
    return this.node.validate().then(err => {
      if (err) { return Promise.reject(err); }

      return this._createMachine();
    }).then(id => {
      this.node.provider_id = id;

      return this.cluster.addNode(this.node);
    }).then(() => {
      return this.cluster.notify(this.deployChanges);
    }).then(() => {
      return this.node.createAction({ type: 'deploy' });
    });
  }
  destroy() {
    return this._deleteMachine().then(() => {
      return this.node.destroy();
    }).then(() => {
      return this.cluster.notify(this.destroyChanges);
    }).then(() => {
      return this.node.getActions();
    }).then(actions => {
      return Promise.all(_.invoke(actions, 'destroy'));
    });
  }
  get deployChanges() {
    return _.pick(this.node, 'last_state');
  }
  get destroyChanges() {
    let changes = this.node.master ? { last_seen: null } : {};

    return _.merge(changes, { last_state: 'destroyed' });
  }
  _createMachine() {
    return this.node.byon ? Promise.resolve() : this.machine.create({
      name:   this.node.id,
      region: this.node.region,
      size:   this.node.size
    });
  }
  _deleteMachine()  {
    let deletion = this.node.byon ? Promise.resolve() : this.machine.delete(
      this.node.provider_id
    );
    /*
     * If the machine behind has been manually deleted on the provider API,
     * we still want the node to be deleted as this node is dead anyway.
     * Therefore we can safely ignore this error.
     */
    return deletion.catch(err => {
      if (err.name !== 'MachineNotFoundError') { throw err; }
    });
  }
}

module.exports = MachineManager;
{'fraction_non_alphanumeric': 0.1685483870967742, 'fraction_numerical': 0.0, 'mean_word_length': 2.7284569138276553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5501727', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1132, 'n_words': 237}