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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 443× 23× 23× 23× 443× 443× 443× 443× 443× 443× 443× 443× 443× 443× 443× 1× 443× 443× 443× 443× 443× 443× 443× 1× 1× 275× 275× 275× 1× 275× 275× 275× 275× 275× 275× 275× 1× 47× 47× 47× 1× 3× 1× 2× 2× 1× 1× 1× 275× 275× 1× 275× 275× 275× 275× 275× 275× 275× 1× 1× 5× 5× 1× 3× 3× 3× 1× 113× 113× 113× 113× 1× 1× 22× 22× 22× 22× 22× 22× 22× 2× 2× 1× 1× 1× 1× 1× 20× 20× 20× 22× 20× 20× 20× 1× 1× 1× 2× 2× 2× 2× 4× 2× 2× 1× 1× 1× 1× 1× 1× 1× 1× 12× 1× 19× 1× 12× 1× 7× 1× 9× 1× 4× 1× 1× 1× 1× 9× 9× 9× 54× 15× 39× 39× 9× 1× 6× 6× 6× 6× 6× 5× 1× 1× 8× 8× 8× 8× 8× 8× 4× 4× 1× 3× 4× 4× 1× 3× 3× 3× 3× 2× 3× 3× 1× 1× 1× 1× 4× 4× 1× 4× 4× 4× 4× 1× 43× 43× 1× 20× 1× 22× 22× 22× 2× 22× 22× 22× 22× 1× 168× 168× 168× 168× 168× 168× 168× 168× 29× 168× 157× 168× 168× 168× 168× 21× 168× 1× 168× 168× 168× 160× 8× 1× 21× 21× 21× 1× | /*eslint-env node */ 'use strict'; var Promise = require('bluebird'); var _ = require('lodash'); var utils = require('./utils'); var scripts = require('./scripts'); var debuglog = require('debuglog')('bull'); var errors = require('./errors'); var backoffs = require('./backoffs'); var FINISHED_WATCHDOG = 5000; /** interface JobOptions { priority: Priority; attempts: number; delay: number; } */ // queue: Queue, data: {}, opts: JobOptions var Job = function(queue, name, data, opts) { if (typeof name !== 'string') { opts = data; data = name; name = '__default__'; } // defaults this.opts = setDefaultOpts(opts); this.name = name; this.queue = queue; this.data = data; this._progress = 0; this.delay = this.opts.delay; this.timestamp = this.opts.timestamp; this.stacktrace = []; this.returnvalue = null; this.attemptsMade = 0; this.toKey = _.bind(queue.toKey, queue); }; function setDefaultOpts(opts) { var _opts = Object.assign({}, opts); _opts.attempts = typeof _opts.attempts == 'undefined' ? 1 : _opts.attempts; _opts.delay = typeof _opts.delay == 'undefined' ? 0 : _opts.delay; _opts.timestamp = typeof _opts.timestamp == 'undefined' ? Date.now() : _opts.timestamp; _opts.attempts = parseInt(_opts.attempts); _opts.backoff = backoffs.normalize(_opts.backoff); return _opts; } Job.DEFAULT_JOB_NAME = '__default__'; function addJob(queue, job) { var opts = job.opts; var jobData = job.toData(); return scripts.addJob( queue.client, queue, jobData, { lifo: opts.lifo, customJobId: opts.jobId, priority: opts.priority }, queue.token ); } Job.create = function(queue, name, data, opts) { var job = new Job(queue, name, data, opts); return queue .isReady() .then(function() { return addJob(queue, job); }) .then(function(jobId) { job.id = jobId; job.lockKey = job.toKey(jobId) + ':lock'; debuglog('Job added', jobId); return job; }); }; Job.fromId = function(queue, jobId) { // jobId can be undefined if moveJob returns undefined Iif (!jobId) { return Promise.resolve(); } return queue.client.hgetall(queue.toKey(jobId)).then(function(jobData) { return utils.isEmpty(jobData) ? null : Job.fromJSON(queue, jobData, jobId); }); }; Job.prototype.progress = function(progress) { if (_.isUndefined(progress)) { return this._progress; } this._progress = progress; return scripts.updateProgress(this, progress); }; Job.prototype.update = function(data) { return this.queue.client.hset( this.queue.toKey(this.id), 'data', JSON.stringify(data) ); }; Job.prototype.toJSON = function() { var opts = Object.assign({}, this.opts); return { id: this.id, name: this.name, data: this.data || {}, opts: opts, progress: this._progress, delay: this.delay, // Move to opts timestamp: this.timestamp, attemptsMade: this.attemptsMade, failedReason: this.failedReason, stacktrace: this.stacktrace || null, returnvalue: this.returnvalue || null, finishedOn: this.finishedOn || null, processedOn: this.processedOn || null }; }; Job.prototype.toData = function() { var json = this.toJSON(); json.data = JSON.stringify(json.data); json.opts = JSON.stringify(json.opts); json.stacktrace = JSON.stringify(json.opts); json.failedReason = JSON.stringify(json.failedReason); json.returnvalue = JSON.stringify(json.returnvalue); return json; }; /** Return a unique key representing a lock for this Job */ Job.prototype.lockKey = function() { return this.toKey(this.id) + ':lock'; }; /** Takes a lock for this job so that no other queue worker can process it at the same time. */ Job.prototype.takeLock = function() { return scripts.takeLock(this.queue, this).then(function(lock) { return lock || false; }); }; /** Releases the lock. Only locks owned by the queue instance can be released. */ Job.prototype.releaseLock = function() { var _this = this; return scripts.releaseLock(this.queue, this.id).then(function(unlocked) { Iif (unlocked != 1) { throw new Error('Could not release lock for job ' + _this.id); } }); }; Job.prototype.moveToCompleted = function(returnValue, ignoreLock) { this.returnvalue = returnValue || 0; returnValue = utils.tryCatch(JSON.stringify, JSON, [returnValue]); Iif (returnValue === utils.errorObject) { var err = utils.errorObject.value; return Promise.reject(err); } return scripts.moveToCompleted( this, returnValue, this.opts.removeOnComplete, ignoreLock ); }; Job.prototype.discard = function() { this._discarded = true; }; Job.prototype.moveToFailed = function(err, ignoreLock) { var _this = this; this.failedReason = err.message; return new Promise(function(resolve, reject) { var command; var multi = _this.queue.client.multi(); _this._saveAttempt(multi, err); // Check if an automatic retry should be performed if (_this.attemptsMade < _this.opts.attempts && !_this._discarded) { // Check if backoff is needed var delay = backoffs.calculate( _this.opts.backoff, _this.attemptsMade, _this.queue.settings.backoffStrategies ); if (delay) { // If so, move to delayed (need to unlock job in this case!) var args = scripts.moveToDelayedArgs( _this.queue, _this.id, Date.now() + delay, ignoreLock ); multi.moveToDelayed(args); command = 'delayed'; } else { // If not, retry immediately multi.retryJob(scripts.retryJobArgs(_this, ignoreLock)); command = 'retry'; } } else { // If not, move to failed var args = scripts.moveToFailedArgs( _this, err.message, _this.opts.removeOnFail, ignoreLock ); multi.moveToFinished(args); command = 'failed'; } return multi.exec().then(function(results) { var code = _.last(results)[1]; Iif (code < 0) { return reject(scripts.finishedErrors(code, _this.id, command)); } resolve(); }, reject); }); }; Job.prototype.moveToDelayed = function(timestamp, ignoreLock) { return scripts.moveToDelayed(this.queue, this.id, timestamp, ignoreLock); }; Job.prototype.promote = function() { var queue = this.queue; var jobId = this.id; var script = [ 'if redis.call("ZREM", KEYS[1], ARGV[1]) == 1 then', ' redis.call("LPUSH", KEYS[2], ARGV[1])', ' return 0', 'else', ' return -1', 'end' ].join('\n'); var keys = _.map(['delayed', 'wait'], function(name) { return queue.toKey(name); }); return queue.client .eval(script, keys.length, keys[0], keys[1], jobId) .then(function(result) { if (result === -1) { throw new Error('Job ' + jobId + ' is not in a delayed state'); } }); }; /** * Attempts to retry the job. Only a job that has failed can be retried. * * @return {Promise} If resolved and return code is 1, then the queue emits a waiting event * otherwise the operation was not a success and throw the corresponding error. If the promise * rejects, it indicates that the script failed to execute */ Job.prototype.retry = function() { var _this = this; return this.queue.isReady().then(function() { return scripts .reprocessJob(_this, { state: 'failed' }) .then(function(result) { Eif (result === 1) { return; } else if (result === 0) { throw new Error(errors.Messages.RETRY_JOB_NOT_EXIST); } else if (result === -1) { throw new Error(errors.Messages.RETRY_JOB_IS_LOCKED); } else if (result === -2) { throw new Error(errors.Messages.RETRY_JOB_NOT_FAILED); } }); }); }; Job.prototype.isCompleted = function() { return this._isDone('completed'); }; Job.prototype.isFailed = function() { return this._isDone('failed'); }; Job.prototype.isDelayed = function() { return this._isDone('delayed'); }; Job.prototype.isActive = function() { return this._isInList('active'); }; Job.prototype.isWaiting = function() { return this._isInList('wait'); }; Job.prototype.isPaused = function() { return this._isInList('paused'); }; Job.prototype.isStuck = function() { return this.getState().then(function(state) { return state === 'stuck'; }); }; Job.prototype.getState = function() { var _this = this; var fns = [ { fn: 'isCompleted', state: 'completed' }, { fn: 'isFailed', state: 'failed' }, { fn: 'isDelayed', state: 'delayed' }, { fn: 'isActive', state: 'active' }, { fn: 'isWaiting', state: 'waiting' }, { fn: 'isPaused', state: 'paused' } ]; return Promise.reduce( fns, function(state, fn) { if (state) { return state; } return _this[fn.fn]().then(function(result) { return result ? fn.state : null; }); }, null ).then(function(result) { return result ? result : 'stuck'; }); }; Job.prototype.remove = function() { var queue = this.queue; var job = this; return queue.isReady().then(function() { return scripts.remove(queue, job.id).then(function(removed) { if (removed) { queue.emit('removed', job); } else { throw new Error('Could not remove job ' + job.id); } }); }); }; /** * Returns a promise the resolves when the job has finished. (completed or failed). */ Job.prototype.finished = function() { var _this = this; return Promise.all([ this.queue._registerEvent('global:completed'), this.queue._registerEvent('global:failed') ]).then(function() { return _this.queue.isReady().then(function() { return scripts.isFinished(_this).then(function(status) { var finished = status > 0; if (finished) { return Job.fromId(_this.queue, _this.id).then(function(job) { if (status == 2) { throw new Error(job.failedReason); } else { return job.returnvalue; } }); } else { return new Promise(function(resolve, reject) { var interval; function onCompleted(jobId, resultValue) { Eif (String(jobId) === String(_this.id)) { var result = void 0; try { if (typeof resultValue === 'string') { result = JSON.parse(resultValue); } } catch (err) { //swallow exception because the resultValue got corrupted somehow. debuglog('corrupted resultValue: ' + resultValue, err); } resolve(result); removeListeners(); } } function onFailed(jobId, failedReason) { Eif (String(jobId) === String(_this.id)) { reject(new Error(failedReason)); removeListeners(); } } _this.queue.on('global:completed', onCompleted); _this.queue.on('global:failed', onFailed); function removeListeners() { clearInterval(interval); _this.queue.removeListener('global:completed', onCompleted); _this.queue.removeListener('global:failed', onFailed); } // // Watchdog // interval = setInterval(function() { scripts.isFinished(_this).then(function(status) { var finished = status > 0; if (finished) { Job.fromId(_this.queue, _this.id).then(function(job) { removeListeners(); if (status == 2) { reject(new Error(job.failedReason)); } else { resolve(job.returnvalue); } }); } }); }, FINISHED_WATCHDOG); }); } }); }); }); }; // ----------------------------------------------------------------------------- // Private methods // ----------------------------------------------------------------------------- Job.prototype._isDone = function(list) { return this.queue.client .zscore(this.queue.toKey(list), this.id) .then(function(score) { return score !== null; }); }; Job.prototype._isInList = function(list) { return scripts.isJobInList( this.queue.client, this.queue.toKey(list), this.id ); }; Job.prototype._saveAttempt = function(multi, err) { this.attemptsMade++; var params = { attemptsMade: this.attemptsMade }; if (this.opts.stackTraceLimit) { this.stacktrace = this.stacktrace.slice(0, this.opts.stackTraceLimit - 1); } this.stacktrace.push(err.stack); params.stacktrace = JSON.stringify(this.stacktrace); params.failedReason = err.message; multi.hmset(this.queue.toKey(this.id), params); }; Job.fromJSON = function(queue, json, jobId) { var data = JSON.parse(json.data || '{}'); var opts = JSON.parse(json.opts || '{}'); var job = new Job(queue, json.name || Job.DEFAULT_JOB_NAME, data, opts); job.id = json.id || jobId; job._progress = parseInt(json.progress || 0); job.delay = parseInt(json.delay); job.timestamp = parseInt(json.timestamp); if (json.finishedOn) { job.finishedOn = parseInt(json.finishedOn); } if (json.processedOn) { job.processedOn = parseInt(json.processedOn); } job.failedReason = json.failedReason; job.attemptsMade = parseInt(json.attemptsMade || 0); job.stacktrace = getTraces(json.stacktrace); if (typeof json.returnvalue === 'string') { job.returnvalue = getReturnValue(json.returnvalue); } return job; }; function getTraces(stacktrace) { var _traces; _traces = utils.tryCatch(JSON.parse, JSON, [stacktrace]); if (_traces === utils.errorObject || !(_traces instanceof Array)) { return []; } else { return _traces; } } function getReturnValue(_value) { var value = utils.tryCatch(JSON.parse, JSON, [_value]); Eif (value !== utils.errorObject) { return value; } else { debuglog('corrupted returnvalue: ' + _value, value); } } module.exports = Job; |