MCPcopy
hub / github.com/bee-queue/bee-queue / _finishJob

Method _finishJob

lib/queue.js:593–660  ·  view source on GitHub ↗
(err, data, job)

Source from the content-addressed store, hash-verified

591 }
592
593 _finishJob(err, data, job) {
594 if (this._isClosed) {
595 const status = err ? 'failed' : 'succeeded';
596 throw new Error(`unable to update the status of ${status} job ${job.id}`);
597 }
598
599 const multi = this.client
600 .multi()
601 .lrem(this.toKey('active'), 0, job.id)
602 .srem(this.toKey('stalling'), job.id);
603
604 const delay = err ? job.computeDelay() : -1;
605 const status = err ? (delay >= 0 ? 'retrying' : 'failed') : 'succeeded';
606
607 job.status = status;
608 if (err) {
609 const errInfo = err.stack || err.message || err;
610 job.options.stacktraces.unshift(errInfo);
611 }
612
613 switch (status) {
614 case 'failed':
615 if (this.settings.removeOnFailure) {
616 multi.hdel(this.toKey('jobs'), job.id);
617 } else {
618 multi.hset(this.toKey('jobs'), job.id, job.toData());
619 multi.sadd(this.toKey('failed'), job.id);
620 }
621 break;
622 case 'retrying':
623 --job.options.retries;
624 multi.hset(this.toKey('jobs'), job.id, job.toData());
625 if (delay === 0) {
626 multi.lpush(this.toKey('waiting'), job.id);
627 } else {
628 const time = Date.now() + delay;
629 multi
630 .zadd(this.toKey('delayed'), time, job.id)
631 .publish(this.toKey('earlierDelayed'), time);
632 }
633 break;
634 case 'succeeded':
635 if (this.settings.removeOnSuccess) {
636 multi.hdel(this.toKey('jobs'), job.id);
637 } else {
638 multi.hset(this.toKey('jobs'), job.id, job.toData());
639 multi.sadd(this.toKey('succeeded'), job.id);
640 }
641 break;
642 }
643
644 if (this.settings.sendEvents) {
645 multi.publish(
646 this.toKey('events'),
647 JSON.stringify({
648 id: job.id,
649 event: status,
650 data: err ? err.message : data,

Callers 1

handleOutcomeMethod · 0.95

Calls 3

toKeyMethod · 0.95
computeDelayMethod · 0.80
toDataMethod · 0.80

Tested by

no test coverage detected