<commit_before>var AWS = require('aws-sdk');

AWS.config.update({accessKeyId: 'your-access-key', secretAccessKey: 'your-secret-access'});
AWS.config.update({region: 'your-region'});

var ec2 = new AWS.EC2();

module.exports.changeTag = function(timeout, oldTags, newTags, beforeHook) {
  schedule(timeout, beforeHook, function() {
    getInstanceId(oldTags, function(id) {
      ec2.createTags({ Resources: [id], Tags: newTags }, function(err, data) {
        if (err) console.log(err);
      });
    });
  });
}; 

module.exports.terminate = function(timeout, tags, beforeHook) {
  schedule(timeout, beforeHook, function() {
    getInstanceId(tags, function(id) {
      ec2.terminateInstances({ InstanceIds: [ id ] }, function(err, data) { 
        if (err) console.log(err);
      });
    });
  });
};

function schedule(timeout, beforeHook, action) {
  timeout = timeout || 60 * 60 * 1000;
  beforeHook = beforeHook || function(cb) { cb(); };
  setTimeout(function() {
    beforeHook(action);
  }, timeout * Math.random());
}

function getInstanceId(tags, cb) {
  var params = {
    Filters: [
      { Name: 'instance-state-name', Values: ['running'] },
      { Name: 'tag-key', Values: tags.names },
      { Name: 'tag-value', Values: tags.values }
    ]
  };
  
  ec2.describeInstances(params, function(error, data) {
    if (error) return console.log(error);
    var instanceIds = data.Reservations.map(function(reservation) {
      return reservation.Instances[0].InstanceId;
    });
    var instanceId = instanceIds[Math.floor(Math.random() * instanceIds.length)];
    cb(instanceId);
  });
}<commit_msg>reuse the instanceId, minor tweaks<commit_after>var AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'your-access-key',
  secretAccessKey: 'your-secret-access',
  region: 'your-region'
});
var ec2 = new AWS.EC2();

var instanceId;

module.exports.changeTag = function(timeout, oldTags, newTags, beforeHook, afterHook) {
  schedule(timeout, beforeHook, function() {
    getInstanceId(oldTags, function(id) {
      instanceId = instanceId || id;
      ec2.createTags({ Resources: [id], Tags: newTags }, function(err, data) {
        if (err) console.log(err);
        afterHook(err, data);
      });
    });
  });
};

module.exports.terminate = function(timeout, tags, beforeHook, afterHook) {
  schedule(timeout, beforeHook, function() {
    getInstanceId(tags, function(id) {
      instanceId = instanceId || id;
      ec2.terminateInstances({ InstanceIds: [ id ] }, function(err, data) {
        if (err) console.log(err);
        afterHook(err, data);
      });
    });
  });
};

function schedule(timeout, beforeHook, action) {
  timeout = timeout || 60 * 60 * 1000;
  beforeHook = beforeHook || function(cb) { cb(); };
  setTimeout(function() {
    beforeHook(action);
  }, timeout * Math.random());
}

function getInstanceId(tags, cb) {
  var params = {
    Filters: [
      { Name: 'instance-state-name', Values: ['running'] },
      { Name: 'tag-key', Values: tags.names },
      { Name: 'tag-value', Values: tags.values }
    ]
  };

  ec2.describeInstances(params, function(error, data) {
    if (error) return console.log(error);
    var instanceIds = data.Reservations.map(function(reservation) {
      return reservation.Instances[0].InstanceId;
    });
    var instanceId = instanceIds[Math.floor(Math.random() * instanceIds.length)];
    cb(instanceId);
  });
}
{'fraction_non_alphanumeric': 0.1641659311562224, 'fraction_numerical': 0.008237716975581053, 'mean_word_length': 3.6448087431693987, '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': '2506523', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1083, 'n_words': 223}