all files / bull/lib/ worker.js

37.5% Statements 12/32
0% Branches 0/4
45.45% Functions 5/11
37.5% Lines 12/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                55× 55× 55×                             55×     55×                                                  
/*eslint-env node */
'use strict';
 
var utils = require('./utils');
module.exports = function(Queue) {
  // IDEA, How to store metadata associated to a worker.
  // create a key from the worker ID associated to the given name.
  // We keep a hash table bull:myqueue:workers where every worker is a hash key workername:workerId with json holding
  // metadata of the worker. The worker key gets expired every 30 seconds or so, we renew the worker metadata.
  //
  Queue.prototype.setWorkerName = function() {
    var _this = this;
    return utils.isRedisReady(this.client).then(function() {
      return _this.client.client('setname', _this.clientName());
    });
  };
 
  Queue.prototype.getWorkers = function() {
    var _this = this;
    return utils
      .isRedisReady(this.client)
      .then(function() {
        return _this.client.client('list');
      })
      .then(function(clients) {
        return _this.parseClientList(clients);
      });
  };
 
  Queue.prototype.base64Name = function() {
    return new Buffer(this.name).toString('base64');
  };
 
  Queue.prototype.clientName = function() {
    return this.keyPrefix + ':' + this.base64Name();
  };
 
  Queue.prototype.parseClientList = function(list) {
    var _this = this;
    var lines = list.split('\n');
    var clients = [];
 
    lines.forEach(function(line) {
      var client = {};
      var keyValues = line.split(' ');
      keyValues.forEach(function(keyValue) {
        var index = keyValue.indexOf('=');
        var key = keyValue.substring(0, index);
        var value = keyValue.substring(index + 1);
        client[key] = value;
      });
      var name = client['name'];
      if (name && name.startsWith(_this.clientName())) {
        client['name'] = _this.name;
        clients.push(client);
      }
    });
    return clients;
  };
};