MCPcopy Index your code
hub / github.com/bee-queue/bee-queue / getJobs

Method getJobs

lib/queue.js:397–454  ·  view source on GitHub ↗

* Get jobs from queue type. * * @param {String} type The queue type (failed, succeeded, waiting, etc.) * @param {?Object=} page An object containing some of the following fields. * @param {Number=} page.start Start of query range for waiting/active/delayed * queue types. Defaults to

(type, page, cb)

Source from the content-addressed store, hash-verified

395 * @return {Promise<Job[]>} Resolves to the jobs the function found.
396 */
397 getJobs(type, page, cb) {
398 if (typeof page === 'function') {
399 cb = page;
400 page = null;
401 }
402 // Set defaults.
403 page = Object.assign(
404 {
405 size: 100,
406 start: 0,
407 end: 100,
408 },
409 page
410 );
411 const promise = this._commandable()
412 .then((client) => {
413 const idsPromise = helpers.deferred(),
414 next = idsPromise.defer();
415 const key = this.toKey(type);
416 switch (type) {
417 case 'failed':
418 case 'succeeded':
419 this._scanForJobs(key, '0', page.size, new Set(), next);
420 break;
421 case 'waiting':
422 case 'active':
423 client.lrange(key, page.start, page.end, next);
424 break;
425 case 'delayed':
426 client.zrange(key, page.start, page.end, next);
427 break;
428 default:
429 throw new Error('Improper queue type');
430 }
431
432 return idsPromise;
433 })
434 .then((ids) => {
435 const jobs = [],
436 idsToFetch = [];
437 // ids might be a Set or an Array, but this will iterate just the same.
438 for (const jobId of ids) {
439 const job = this.jobs.get(jobId);
440 if (job) {
441 jobs.push(job);
442 } else {
443 idsToFetch.push(jobId);
444 }
445 }
446 if (!idsToFetch.length) {
447 return jobs;
448 }
449 return this._addJobsByIds(jobs, idsToFetch);
450 });
451
452 if (cb) helpers.asCallback(promise, cb);
453 return promise;
454 }

Callers 1

queue-test.jsFile · 0.80

Calls 4

_commandableMethod · 0.95
toKeyMethod · 0.95
_scanForJobsMethod · 0.95
_addJobsByIdsMethod · 0.95

Tested by

no test coverage detected