all files / bull/lib/ getters.js

91.57% Statements 76/83
96.97% Branches 32/33
75.86% Functions 22/29
91.57% Lines 76/83
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187            28×   28× 58×   58×   58×         27×       31×                             13× 13×                     17× 17×   17× 44×     17× 17× 17× 44×   17×                                                                   11×   11× 11×   11× 11×   11× 14×                   11× 11×   11× 14×   14×       11×       11× 11× 11×         28×             28×        
/*eslint-env node */
'use strict';
 
var _ = require('lodash');
var Job = require('./job');
 
module.exports = function(Queue) {
  Queue.prototype.getJob = function(jobId) {
    return Job.fromId(this, jobId);
  };
 
  Queue.prototype._commandByType = function(types, count, callback) {
    var _this = this;
 
    return _.map(types, function(type) {
      type = type === 'waiting' ? 'wait' : type; // alias
 
      var key = _this.toKey(type);
 
      switch (type) {
        case 'completed':
        case 'failed':
        case 'delayed':
        case 'repeat':
          return callback(key, count ? 'zcard' : 'zrange');
        case 'active':
        case 'wait':
        case 'paused':
          return callback(key, count ? 'llen' : 'lrange');
      }
    });
  };
 
  /**
    Returns the number of jobs waiting to be processed.
  */
  Queue.prototype.count = function() {
    return this.getJobCountByTypes('wait', 'paused', 'delayed');
  };
 
  // Job counts by type
  // Queue#getJobCountByTypes('completed') => completed count
  // Queue#getJobCountByTypes('completed,failed') => completed + failed count
  // Queue#getJobCountByTypes('completed', 'failed') => completed + failed count
  // Queue#getJobCountByTypes('completed,waiting', 'failed') => completed + waiting + failed count
  Queue.prototype.getJobCountByTypes = function() {
    return this.getJobCounts.apply(this, arguments).then(function(result) {
      return _.chain(result)
        .values()
        .sum()
        .value();
    });
  };
 
  /**
   * Returns the job counts for each type specified or every list/set in the queue by default.
   *
   */
  Queue.prototype.getJobCounts = function() {
    var types = parseTypeArg(arguments);
    var multi = this.multi();
 
    this._commandByType(types, true, function(key, command) {
      multi[command](key);
    });
 
    return multi.exec().then(function(res) {
      var counts = {};
      res.forEach(function(res, index) {
        counts[types[index]] = res[1] || 0;
      });
      return counts;
    });
  };
 
  Queue.prototype.getCompletedCount = function() {
    return this.getJobCountByTypes('completed');
  };
 
  Queue.prototype.getFailedCount = function() {
    return this.getJobCountByTypes('failed');
  };
 
  Queue.prototype.getDelayedCount = function() {
    return this.getJobCountByTypes('delayed');
  };
 
  Queue.prototype.getActiveCount = function() {
    return this.getJobCountByTypes('active');
  };
 
  Queue.prototype.getWaitingCount = function() {
    return this.getJobCountByTypes('wait', 'paused');
  };
 
  // TO BE DEPRECATED
  Queue.prototype.getPausedCount = function() {
    return this.getJobCountByTypes('paused');
  };
 
  Queue.prototype.getWaiting = function(start, end) {
    return this.getJobs(['wait', 'paused'], start, end, true);
  };
 
  Queue.prototype.getActive = function(start, end) {
    return this.getJobs('active', start, end, true);
  };
 
  Queue.prototype.getDelayed = function(start, end) {
    return this.getJobs('delayed', start, end, true);
  };
 
  Queue.prototype.getCompleted = function(start, end) {
    return this.getJobs('completed', start, end, false);
  };
 
  Queue.prototype.getFailed = function(start, end) {
    return this.getJobs('failed', start, end, false);
  };
 
  Queue.prototype.getRanges = function(types, start, end, asc) {
    var _this = this;
 
    start = _.isUndefined(start) ? 0 : start;
    end = _.isUndefined(end) ? -1 : end;
 
    var multi = this.multi();
    var multiCommands = [];
 
    _this._commandByType(parseTypeArg(types), false, function(key, command) {
      switch (command) {
        case 'lrange':
          if (asc) {
            multiCommands.push('lrange');
            multi.lrange(key, -(end + 1), -(start + 1));
          } else {
            multi.lrange(key, start, end);
          }
          break;
        case 'zrange':
          multiCommands.push('zrange');
          if (asc) {
            multi.zrange(key, start, end);
          } else {
            multi.zrevrange(key, start, end);
          }
          break;
      }
    });
 
    return multi.exec().then(function(responses) {
      var results = [];
 
      responses.forEach(function(response, index) {
        var result = response[1] || [];
 
        if (asc && multiCommands[index] === 'lrange') {
          results = results.concat(result.reverse());
        } else {
          results = results.concat(result);
        }
      });
      return results;
    });
  };
 
  Queue.prototype.getJobs = function(types, start, end, asc) {
    var _this = this;
    return this.getRanges(types, start, end, asc).then(function(jobIds) {
      return Promise.all(jobIds.map(_this.getJobFromId));
    });
  };
};
 
function parseTypeArg(args) {
  var types = _.chain([])
    .concat(args)
    .join(',')
    .split(/\s*,\s*/g)
    .compact()
    .value();
 
  return types.length
    ? types
    : ['waiting', 'active', 'completed', 'failed', 'delayed', 'paused'];
}