<commit_before>var events = require('events');

var processQueue = function processQueue(queue, dataToModify) {
  var onCompleteCallbacks = [];

  while (queue.length) {
    var atom = queue.shift();

    if (atom.modify) {
      atom.modify(dataToModify);
    }

    if (atom.done) {
      onCompleteCallbacks.push(atom.done);
    }
  }

  return onCompleteCallbacks;
};

var modifyData = function modifyData(accountHref, dataToModify, queue, emitter) {
  var onCompleteCallbacks = processQueue(queue, dataToModify);

  dataToModify.save(function finishSaving(err, account) {
    onCompleteCallbacks.forEach(function executeCallback(callback) {
      callback(err, account);
    });

    queue.lock = false;

    emitter.emit('doneSaving', accountHref);
  });
};

var executeQueue = function executeQueue(queue, accountHref, app, emitter) {
  if (!queue.length) {
    return;
  }

  var client = app.get('stormpathClient');

  if (!client) {
    app.once('stormpath.ready', function retryExecuteQueue() {
      if (queue.lock) {
        return;
      }

      executeQueue(queue, accountHref, app, emitter);
    });

    return;
  }

  queue.lock = true;

  var options = {
    expand: 'customData',
  };

  client.getAccount(accountHref, options, function modifyAccountData(err, account) {
    modifyData(accountHref, account.customData, queue, emitter);
  });
};

var Enqueuer = function newEnqueuer(app) {
  var customDataQueues = {};
  var emitter = new events.EventEmitter();

  emitter.on('doneSaving', function executeAccountQueue(accountHref) {
    executeQueue(customDataQueues[accountHref], accountHref, app, emitter);
  });

  this.customDataQueues = customDataQueues;
  this.emitter = emitter;
  this.app = app;
};

Enqueuer.prototype = {
  populate: function populate(req) {
    req.stormpathEnqueuer = this;
  },

  modifyCustomData: function modifyCustomData(accountHref, modifyCallback, doneCallback) {
    var queue = this.customDataQueues[accountHref];
<commit_msg>Retry queue execution if there's an error getting the account.<commit_after>var events = require('events');

var processQueue = function processQueue(queue, dataToModify) {
  var onCompleteCallbacks = [];

  while (queue.length) {
    var atom = queue.shift();

    if (atom.modify) {
      atom.modify(dataToModify);
    }

    if (atom.done) {
      onCompleteCallbacks.push(atom.done);
    }
  }

  return onCompleteCallbacks;
};

var modifyData = function modifyData(accountHref, dataToModify, queue, emitter) {
  var onCompleteCallbacks = processQueue(queue, dataToModify);

  dataToModify.save(function finishSaving(err, account) {
    onCompleteCallbacks.forEach(function executeCallback(callback) {
      callback(err, account);
    });

    queue.lock = false;

    emitter.emit('doneSaving', accountHref);
  });
};

var executeQueue = function executeQueue(queue, accountHref, app, emitter) {
  if (!queue.length) {
    return;
  }

  var client = app.get('stormpathClient');

  if (!client) {
    app.once('stormpath.ready', function retryExecuteQueue() {
      if (queue.lock) {
        return;
      }

      executeQueue(queue, accountHref, app, emitter);
    });

    return;
  }

  queue.lock = true;

  var options = {
    expand: 'customData',
  };

  client.getAccount(accountHref, options, function modifyAccountData(err, account) {
    if (err) {
      queue.lock = false;

      return executeQueue(queue, accountHref, app, emitter);
    }

    modifyData(accountHref, account.customData, queue, emitter);
  });
};

var Enqueuer = function newEnqueuer(app) {
  var customDataQueues = {};
  var emitter = new events.EventEmitter();

  emitter.on('doneSaving', function executeAccountQueue(accountHref) {
    executeQueue(customDataQueues[accountHref], accountHref, app, emitter);
  });

  this.customDataQueues = customDataQueues;
  this.emitter = emitter;
  this.app = app;
};

Enqueuer.prototype = {
  populate: function populate(req) {
    req.stormpathEnqueuer = this;
  },

  modifyCustomData: function modifyCustomData(accountHref, modifyCallback, doneCallback) {
    var queue = this.customDataQueues[accountHref];

{'fraction_non_alphanumeric': 0.12139568693966561, 'fraction_numerical': 0.0, 'mean_word_length': 3.879432624113475, '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': '386726', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1273, 'n_words': 264}