rschmukler/agenda

View on GitHub
src/JobProcessor.ts

Summary

Maintainability
F
5 days
Test Coverage

File JobProcessor.ts has 463 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import * as debug from 'debug';
import type { IAgendaJobStatus, IAgendaStatus } from './types/AgendaStatus';
import type { IJobDefinition } from './types/JobDefinition';
import type { Agenda, JobWithId } from './index';
import type { IJobParameters } from './types/JobParameters';
Severity: Minor
Found in src/JobProcessor.ts - About 7 hrs to fix

    Function runOrRetry has 118 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private async runOrRetry(job: JobWithId): Promise<void> {
            if (!this.isRunning) {
                // const a = new Error();
                // console.log('STACK', a.stack);
                log.extend('runOrRetry')(
    Severity: Major
    Found in src/JobProcessor.ts - About 4 hrs to fix

      Function runOrRetry has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
      Open

          private async runOrRetry(job: JobWithId): Promise<void> {
              if (!this.isRunning) {
                  // const a = new Error();
                  // console.log('STACK', a.stack);
                  log.extend('runOrRetry')(
      Severity: Minor
      Found in src/JobProcessor.ts - About 4 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function jobProcessing has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring.
      Open

          private async jobProcessing(handledJobs: IJobParameters['_id'][] = []) {
              // Ensure we have jobs
              if (this.jobQueue.length === 0) {
                  return;
              }
      Severity: Minor
      Found in src/JobProcessor.ts - About 4 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function jobProcessing has 73 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          private async jobProcessing(handledJobs: IJobParameters['_id'][] = []) {
              // Ensure we have jobs
              if (this.jobQueue.length === 0) {
                  return;
              }
      Severity: Major
      Found in src/JobProcessor.ts - About 2 hrs to fix

        Function lockOnTheFly has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
        Open

            async lockOnTheFly(): Promise<void> {
                // Already running this? Return
                if (this.isLockingOnTheFly) {
                    log.extend('lockOnTheFly')('already running, returning');
                    return;
        Severity: Minor
        Found in src/JobProcessor.ts - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function lockOnTheFly has 55 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            async lockOnTheFly(): Promise<void> {
                // Already running this? Return
                if (this.isLockingOnTheFly) {
                    log.extend('lockOnTheFly')('already running, returning');
                    return;
        Severity: Major
        Found in src/JobProcessor.ts - About 2 hrs to fix

          Function getStatus has 46 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              async getStatus(fullDetails = false): Promise<IAgendaStatus> {
                  const jobStatus = Object.keys(this.jobStatus).reduce((obj, key) => {
                      // eslint-disable-next-line no-param-reassign
                      obj[key] = {
                          ...this.jobStatus[key],
          Severity: Minor
          Found in src/JobProcessor.ts - About 1 hr to fix

            Function jobQueueFilling has 41 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                private async jobQueueFilling(name: string): Promise<void> {
                    this.isJobQueueFilling.set(name, true);
            
                    try {
                        // Don't lock because of a limit we have set (lockLimit, etc)
            Severity: Minor
            Found in src/JobProcessor.ts - About 1 hr to fix

              Function checkIfJobIsStillAlive has 40 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                                  new Promise<void>((resolve, reject) => {
                                      setTimeout(async () => {
                                          // when job is not running anymore, just finish
                                          if (!jobIsRunning) {
                                              log.extend('runOrRetry')(
              Severity: Minor
              Found in src/JobProcessor.ts - About 1 hr to fix

                Function jobQueueFilling has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                Open

                    private async jobQueueFilling(name: string): Promise<void> {
                        this.isJobQueueFilling.set(name, true);
                
                        try {
                            // Don't lock because of a limit we have set (lockLimit, etc)
                Severity: Minor
                Found in src/JobProcessor.ts - About 55 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Avoid too many return statements within this function.
                Open

                                        return;
                Severity: Major
                Found in src/JobProcessor.ts - About 30 mins to fix

                  Similar blocks of code found in 4 locations. Consider refactoring.
                  Open

                                  if (runningJobIndex === -1) {
                                      // lookup by id
                                      runningJobIndex = this.runningJobs.findIndex(
                                          j => j.attrs._id?.toString() === job.attrs._id?.toString()
                                      );
                  Severity: Major
                  Found in src/JobProcessor.ts and 3 other locations - About 1 hr to fix
                  src/JobProcessingQueue.ts on lines 43..48
                  src/JobProcessor.ts on lines 429..434
                  src/JobProcessor.ts on lines 604..609

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 72.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Similar blocks of code found in 4 locations. Consider refactoring.
                  Open

                                          if (lockedJobIndex === -1) {
                                              // lookup by id
                                              lockedJobIndex = this.lockedJobs.findIndex(
                                                  j => j.attrs._id?.toString() === job.attrs._id?.toString()
                                              );
                  Severity: Major
                  Found in src/JobProcessor.ts and 3 other locations - About 1 hr to fix
                  src/JobProcessingQueue.ts on lines 43..48
                  src/JobProcessor.ts on lines 589..594
                  src/JobProcessor.ts on lines 604..609

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 72.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Similar blocks of code found in 4 locations. Consider refactoring.
                  Open

                                  if (lockedJobIndex === -1) {
                                      // lookup by id
                                      lockedJobIndex = this.lockedJobs.findIndex(
                                          j => j.attrs._id?.toString() === job.attrs._id?.toString()
                                      );
                  Severity: Major
                  Found in src/JobProcessor.ts and 3 other locations - About 1 hr to fix
                  src/JobProcessingQueue.ts on lines 43..48
                  src/JobProcessor.ts on lines 429..434
                  src/JobProcessor.ts on lines 589..594

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 72.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Similar blocks of code found in 3 locations. Consider refactoring.
                  Open

                              runningJobs: !fullDetails
                                  ? this.runningJobs.length
                                  : this.runningJobs.map(job => ({
                                          ...job.toJson(),
                                          canceled: job.getCanceledMessage()
                  Severity: Major
                  Found in src/JobProcessor.ts and 2 other locations - About 1 hr to fix
                  src/JobProcessor.ts on lines 62..67
                  src/JobProcessor.ts on lines 68..73

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 60.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Similar blocks of code found in 3 locations. Consider refactoring.
                  Open

                              lockedJobs: !fullDetails
                                  ? this.lockedJobs.length
                                  : this.lockedJobs.map(job => ({
                                          ...job.toJson(),
                                          canceled: job.getCanceledMessage()
                  Severity: Major
                  Found in src/JobProcessor.ts and 2 other locations - About 1 hr to fix
                  src/JobProcessor.ts on lines 56..61
                  src/JobProcessor.ts on lines 68..73

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 60.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Similar blocks of code found in 3 locations. Consider refactoring.
                  Open

                              jobsToLock: !fullDetails
                                  ? this.jobsToLock.length
                                  : this.jobsToLock.map(job => ({
                                          ...job.toJson(),
                                          canceled: job.getCanceledMessage()
                  Severity: Major
                  Found in src/JobProcessor.ts and 2 other locations - About 1 hr to fix
                  src/JobProcessor.ts on lines 56..61
                  src/JobProcessor.ts on lines 62..67

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 60.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  There are no issues that match your filters.

                  Category
                  Status