all files / bull/lib/ scripts.js

87.6% Statements 113/129
69.12% Branches 47/68
91.67% Functions 33/36
87.6% Lines 113/129
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489                  13×   13× 13× 13×     13×       20× 20×         275× 275×                 275×                         275× 275×                 36×             90× 90×   90× 90× 90× 90× 90×   90×               90×     90×                                       133× 133×   133×                 133×                           133×                     113×               113× 113×   113× 69×   44×                             113×                     20×                                                 16×                                                                                     42×                                                                   55×           55× 55×                       57×                 57×           57×         13× 13× 13× 13× 13×   13×                     13× 13× 13× 13×     13×                   13×                                           13×                     13×                                                                         129× 129× 909×   129×     159× 129× 129× 129× 129×     30×    
/**
 * Includes all the scripts needed by the queue and jobs.
 */
 
/*eslint-env node */
'use strict';
 
var _ = require('lodash');
var debuglog = require('debuglog')('bull');
 
// TO Deprecate.
function execScript(client, hash, lua, numberOfKeys) {
  var args = _.drop(arguments, 4);
 
  Eif (!client[hash]) {
    debuglog(hash, lua, args);
    client.defineCommand(hash, { numberOfKeys: numberOfKeys, lua: lua });
  }
 
  return client[hash](args);
}
 
var scripts = {
  isJobInList: function(client, listKey, jobId) {
    return client.isJobInList([listKey, jobId]).then(function(result) {
      return result === 1;
    });
  },
 
  addJob: function(client, queue, job, opts) {
    var queueKeys = queue.keys;
    var keys = [
      queueKeys.wait,
      queueKeys.paused,
      queueKeys['meta-paused'],
      queueKeys.id,
      queueKeys.delayed,
      queueKeys.priority
    ];
 
    var args = [
      queueKeys[''],
      _.isUndefined(opts.customJobId) ? '' : opts.customJobId,
      job.name,
      job.data,
      job.opts,
      job.timestamp,
      job.delay,
      job.delay ? job.timestamp + job.delay : 0,
      opts.priority || 0,
      opts.lifo ? 'RPUSH' : 'LPUSH',
      queue.token
    ];
    keys = keys.concat(args);
    return client.addJob(keys);
  },
 
  pause: function(queue, pause) {
    var src = 'wait',
      dst = 'paused';
    if (!pause) {
      src = 'paused';
      dst = 'wait';
    }
 
    var keys = _.map(
      [src, dst, 'meta-paused', pause ? 'paused' : 'resumed'],
      function(name) {
        return queue.toKey(name);
      }
    );
 
    return queue.client.pause(keys.concat([pause ? 'paused' : 'resumed']));
  },
 
  moveToActive: function(queue, jobId) {
    var queueKeys = queue.keys;
    var keys = [queueKeys.wait, queueKeys.active, queueKeys.priority];
 
    keys[3] = keys[1] + '@' + queue.token;
    keys[4] = queueKeys.stalled;
    keys[5] = queueKeys.limiter;
    keys[6] = queueKeys.delayed;
    keys[7] = queueKeys.drained;
 
    var args = [
      queueKeys[''],
      queue.token,
      queue.settings.lockDuration,
      Date.now(),
      jobId
    ];
 
    Iif (queue.limiter) {
      args.push(queue.limiter.max, queue.limiter.duration);
    }
    return queue.client.moveToActive(keys.concat(args)).then(raw2jobData);
  },
 
  updateProgress: function(job, progress) {
    var queue = job.queue;
    var keys = [job.id, 'progress'].map(function(name) {
      return queue.toKey(name);
    });
 
    return queue.client
      .updateProgress(keys, [progress, job.id + ',' + progress])
      .then(function() {
        queue.emit('progress', job, progress);
      });
  },
 
  moveToFinishedArgs: function(
    job,
    val,
    propVal,
    shouldRemove,
    target,
    ignoreLock,
    notFetch
  ) {
    var queue = job.queue;
    var queueKeys = queue.keys;
 
    var keys = [
      queueKeys.active,
      queueKeys[target],
      queue.toKey(job.id),
      queueKeys.wait,
      queueKeys.priority,
      queueKeys.active + '@' + queue.token
    ];
 
    var args = [
      job.id,
      Date.now(),
      propVal,
      _.isUndefined(val) ? 'null' : val,
      ignoreLock ? '0' : queue.token,
      shouldRemove ? '1' : '0',
      JSON.stringify({ jobId: job.id, val: val }),
      notFetch || queue.paused || queue.closing || queue.limiter ? 0 : 1,
      queueKeys[''],
      queue.settings.lockDuration,
      queue.token
    ];
 
    return keys.concat(args);
  },
 
  moveToFinished: function(
    job,
    val,
    propVal,
    shouldRemove,
    target,
    ignoreLock
  ) {
    var args = scripts.moveToFinishedArgs(
      job,
      val,
      propVal,
      shouldRemove,
      target,
      ignoreLock
    );
    return job.queue.client.moveToFinished(args).then(function(result) {
      Iif (result < 0) {
        throw scripts.finishedErrors(result, job.id, 'finished');
      } else if (result) {
        return raw2jobData(result);
      }
      return 0;
    });
  },
 
  finishedErrors: function(code, jobId, command) {
    switch (code) {
      case -1:
        return new Error('Missing key for job ' + jobId + ' ' + command);
      case -2:
        return new Error('Missing lock for job ' + jobId + ' ' + command);
    }
  },
 
  // TODO: add a retention argument for completed and finished jobs (in time).
  moveToCompleted: function(job, returnvalue, removeOnComplete, ignoreLock) {
    return scripts.moveToFinished(
      job,
      returnvalue,
      'returnvalue',
      removeOnComplete,
      'completed',
      ignoreLock
    );
  },
 
  moveToFailedArgs: function(job, failedReason, removeOnFailed, ignoreLock) {
    return scripts.moveToFinishedArgs(
      job,
      failedReason,
      'failedReason',
      removeOnFailed,
      'failed',
      ignoreLock,
      true
    );
  },
 
  moveToFailed: function(job, failedReason, removeOnFailed, ignoreLock) {
    var args = scripts.moveToFailedArgs(
      job,
      failedReason,
      'failedReason',
      removeOnFailed,
      'failed',
      ignoreLock,
      true
    );
    return scripts.moveToFinished(args);
  },
 
  isFinished: function(job) {
    var keys = _.map(['completed', 'failed'], function(key) {
      return job.queue.toKey(key);
    });
 
    return job.queue.client.isFinished(keys.concat([job.id]));
  },
 
  moveToDelayedArgs: function(queue, jobId, timestamp, ignoreLock) {
    //
    // Bake in the job id first 12 bits into the timestamp
    // to guarantee correct execution order of delayed jobs
    // (up to 4096 jobs per given timestamp or 4096 jobs apart per timestamp)
    //
    // WARNING: Jobs that are so far apart that they wrap around will cause FIFO to fail
    //
    timestamp = _.isUndefined(timestamp) ? 0 : timestamp;
 
    timestamp = +timestamp || 0;
    timestamp = timestamp < 0 ? 0 : timestamp;
    Eif (timestamp > 0) {
      timestamp = timestamp * 0x1000 + (jobId & 0xfff);
    }
 
    var keys = _.map(['active', 'delayed', jobId], function(name) {
      return queue.toKey(name);
    });
    return keys.concat([
      JSON.stringify(timestamp),
      jobId,
      ignoreLock ? '0' : queue.token
    ]);
  },
 
  moveToDelayed: function(queue, jobId, timestamp, ignoreLock) {
    var args = scripts.moveToDelayedArgs(queue, jobId, timestamp, ignoreLock);
    return queue.client.moveToDelayed(args).then(function(result) {
      switch (result) {
        case -1:
          throw new Error(
            'Missing Job ' +
              jobId +
              ' when trying to move from active to delayed'
          );
        case -2:
          throw new Error(
            'Job ' +
              jobId +
              ' was locked when trying to move from active to delayed'
          );
      }
    });
  },
 
  remove: function(queue, jobId) {
    var keys = _.map(
      ['active', 'wait', 'delayed', 'paused', 'completed', 'failed', jobId],
      function(name) {
        return queue.toKey(name);
      }
    );
 
    return queue.client.removeJob(keys.concat([jobId, queue.token]));
  },
 
  extendLock: function(queue, jobId) {
    return queue.client.extendLock([
      queue.toKey(jobId) + ':lock',
      queue.keys.stalled,
      queue.token,
      queue.settings.lockDuration,
      jobId
    ]);
  },
 
  releaseLock: function(queue, jobId) {
    return queue.client.releaseLock([
      queue.toKey(jobId) + ':lock',
      queue.token
    ]);
  },
 
  takeLock: function(queue, job) {
    return queue.client.takeLock([
      job.lockKey,
      queue.token,
      queue.settings.lockDuration
    ]);
  },
 
  /**
    It checks if the job in the top of the delay set should be moved back to the
    top of the  wait queue (so that it will be processed as soon as possible)
  */
  updateDelaySet: function(queue, delayedTimestamp) {
    var keys = [
      queue.keys.delayed,
      queue.keys.active,
      queue.keys.wait,
      queue.keys.priority
    ];
    var args = [queue.toKey(''), delayedTimestamp];
    return queue.client.updateDelaySet(keys.concat(args));
  },
 
  /**
   * Looks for unlocked jobs in the active queue.
   *
   *    The job was being worked on, but the worker process died and it failed to renew the lock.
   *    We call these jobs 'stalled'. This is the most common case. We resolve these by moving them
   *    back to wait to be re-processed. To prevent jobs from cycling endlessly between active and wait,
   *    (e.g. if the job handler keeps crashing), we limit the number stalled job recoveries to settings.maxStalledCount.
   */
  moveUnlockedJobsToWait: function(queue) {
    var keys = [
      queue.keys.stalled,
      queue.keys.wait,
      queue.keys.active,
      queue.keys.failed,
      queue.keys['stalled-check'],
      queue.keys['meta-paused'],
      queue.keys.paused
    ];
    var args = [
      queue.settings.maxStalledCount,
      queue.toKey(''),
      Date.now(),
      queue.settings.stalledInterval
    ];
    return queue.client.moveStalledJobsToWait(keys.concat(args));
  },
 
  // TODO: Refactor into lua script
  cleanJobsInSet: function(queue, set, ts, limit) {
    var command;
    var removeCommand;
    var breakEarlyCommand = '';
    var hash;
    limit = limit || 0;
 
    switch (set) {
      case 'wait':
      case 'active':
      case 'paused':
        command = 'local jobs = redis.call("LRANGE", KEYS[1], 0, -1)';
        removeCommand = 'redis.call("LREM", KEYS[1], 0, job)';
        hash = 'cleanList';
        break;
      case 'delayed':
      case 'completed':
      case 'failed':
        command = 'local jobs = redis.call("ZRANGE", KEYS[1], 0, -1)';
        removeCommand = 'redis.call("ZREM", KEYS[1], job)';
        hash = 'cleanOSet';
        break;
    }
 
    Iif (limit > 0) {
      breakEarlyCommand = [
        'if deletedCount >= limit then',
        '  break',
        'end'
      ].join('\n');
 
      hash = hash + 'WithLimit';
    }
 
    var script = [
      command,
      'local deleted = {}',
      'local deletedCount = 0',
      'local limit = tonumber(ARGV[3])',
      'local jobTS',
      'for _, job in ipairs(jobs) do',
      breakEarlyCommand,
      '  local jobKey = ARGV[1] .. job',
      '  if (redis.call("EXISTS", jobKey ..  ":lock") == 0) then',
      '    jobTS = redis.call("HGET", jobKey, "timestamp")',
      '    if(not jobTS or jobTS < ARGV[2]) then',
      removeCommand,
      '      redis.call("DEL", jobKey)',
      '      deletedCount = deletedCount + 1',
      '      table.insert(deleted, job)',
      '    end',
      '  end',
      'end',
      'return deleted'
    ].join('\n');
 
    var args = [
      queue.client,
      hash,
      script,
      1,
      queue.toKey(set),
      queue.toKey(''),
      ts,
      limit
    ];
 
    return execScript.apply(scripts, args);
  },
 
  retryJobArgs: function(job, ignoreLock) {
    var queue = job.queue;
    var jobId = job.id;
 
    var keys = _.map(['active', 'wait', jobId], function(name) {
      return queue.toKey(name);
    });
 
    var pushCmd = (job.opts.lifo ? 'R' : 'L') + 'PUSH';
 
    return keys.concat([pushCmd, jobId, ignoreLock ? '0' : job.queue.token]);
  },
 
  /**
   * Attempts to reprocess a job
   *
   * @param {Job} job
   * @param {Object} options
   * @param {String} options.state The expected job state. If the job is not found
   * on the provided state, then it's not reprocessed. Supported states: 'failed', 'completed'
   *
   * @return {Promise<Number>} Returns a promise that evaluates to a return code:
   * 1 means the operation was a success
   * 0 means the job does not exist
   * -1 means the job is currently locked and can't be retried.
   * -2 means the job was not found in the expected set
   */
  reprocessJob: function(job, options) {
    var queue = job.queue;
 
    var keys = [
      queue.toKey(job.id),
      queue.toKey(job.id) + ':lock',
      queue.toKey(options.state),
      queue.toKey('wait')
    ];
 
    var args = [job.id, (job.opts.lifo ? 'R' : 'L') + 'PUSH', queue.token];
 
    return queue.client.reprocessJob(keys.concat(args));
  }
};
 
module.exports = scripts;
 
function array2obj(arr) {
  var obj = {};
  for (var i = 0; i < arr.length; i += 2) {
    obj[arr[i]] = arr[i + 1];
  }
  return obj;
}
 
function raw2jobData(raw) {
  if (raw) {
    var jobData = raw[0];
    Eif (jobData.length) {
      var job = array2obj(jobData);
      return [job, raw[1]];
    }
  }
  return [];
}