schahriar/supertask-cluster

View on GitHub

Showing 27 of 27 total issues

Function STC_COM_SEND has a Cognitive Complexity of 29 (exceeds 5 allowed). Consider refactoring.
Open

CommunicationModel.prototype.send = function STC_COM_SEND(id, message, timeout, callback) {
    // Check if ID is valid
    if((!this.cluster.workers[id]) && (id !== 'master')) {
        if(callback) {
            return callback(new Error('Worker with the given ID was not found.'));
Severity: Minor
Found in lib/CommunicationModel.js - 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 STC_COM_INCOMING has a Cognitive Complexity of 28 (exceeds 5 allowed). Consider refactoring.
Open

CommunicationModel.prototype.incoming = function STC_COM_INCOMING(id, message) {
    if(message.ticket && !message.type) {
        this.emit('CLUSTER_CALLBACK::' + id + "::" + message.ticket, message);
    }else if(message.type === 'buffer') {
        var callback = this.callback(id, message.ticket);
Severity: Minor
Found in lib/CommunicationModel.js - 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

Identical blocks of code found in 2 locations. Consider refactoring.
Open

    if(message.done) {
        buffer.set(message.data, message.chunk);
        buffer.setDone();
        // Finish Buffer
        callback(null, { ticket: "STC_BUFFER:" + message.name, digest: buffer.digest(), encoding: buffer.encoding })
Severity: Major
Found in lib/Worker.js and 1 other location - About 3 hrs to fix
lib/CommunicationModel.js on lines 60..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 112.

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

Identical blocks of code found in 2 locations. Consider refactoring.
Open

                if(message.done) {
                    buffer.set(message.data, message.chunk);
                    buffer.setDone();
                    // Finish Buffer
                    callback(null, { ticket: "STC_BUFFER:" + message.name, digest: buffer.digest(), encoding: buffer.encoding });
Severity: Major
Found in lib/CommunicationModel.js and 1 other location - About 3 hrs to fix
lib/Worker.js on lines 83..90

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 112.

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

Function STC_DEPLOY_CLUSTER has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
Open

SuperTaskCluster.prototype.deploy = function STC_DEPLOY_CLUSTER(maxTotalWorkers) {
    var _this = this;
    this.STC_MAX_TOTAL_WORKERS = maxTotalWorkers || this.STC_MAX_TOTAL_WORKERS || os.cpus().length;
    var WorkersRequired = Math.max(this.STC_MAX_TOTAL_WORKERS - COM.headcount(), 0) || 0;
    
Severity: Minor
Found in cluster.js - About 1 hr 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 STC_HANDLER has 37 lines of code (exceeds 25 allowed). Consider refactoring.
Open

SuperTaskCluster.prototype._STC_HANDLER = function STC_HANDLER() {
    var args = Array.prototype.slice.call(arguments);
    var name = args.shift(), context = args.shift(), callback = args.pop();
    // Run task on a free Worker
    var Candidate = { v: Infinity, i: 'master', refs: 0 };
Severity: Minor
Found in cluster.js - About 1 hr to fix

    Function STC_DEPLOY_CLUSTER has 33 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    SuperTaskCluster.prototype.deploy = function STC_DEPLOY_CLUSTER(maxTotalWorkers) {
        var _this = this;
        this.STC_MAX_TOTAL_WORKERS = maxTotalWorkers || this.STC_MAX_TOTAL_WORKERS || os.cpus().length;
        var WorkersRequired = Math.max(this.STC_MAX_TOTAL_WORKERS - COM.headcount(), 0) || 0;
        
    Severity: Minor
    Found in cluster.js - About 1 hr to fix

      Function STC_COM_INCOMING has 29 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      CommunicationModel.prototype.incoming = function STC_COM_INCOMING(id, message) {
          if(message.ticket && !message.type) {
              this.emit('CLUSTER_CALLBACK::' + id + "::" + message.ticket, message);
          }else if(message.type === 'buffer') {
              var callback = this.callback(id, message.ticket);
      Severity: Minor
      Found in lib/CommunicationModel.js - About 1 hr to fix

        Function STC_COM_CALLBACK has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
        Open

        CommunicationModel.prototype.callback = function STC_COM_CALLBACK(id, ticket) {
            var _this = this;
            return function STC_WORKER_CALLBACK(error, extension) {
                if(error) {
                    _this.send(id, { ticket: ticket, success: false, error: error.message });
        Severity: Minor
        Found in lib/CommunicationModel.js - About 1 hr 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 STC_COM_SEND has 26 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        CommunicationModel.prototype.send = function STC_COM_SEND(id, message, timeout, callback) {
            // Check if ID is valid
            if((!this.cluster.workers[id]) && (id !== 'master')) {
                if(callback) {
                    return callback(new Error('Worker with the given ID was not found.'));
        Severity: Minor
        Found in lib/CommunicationModel.js - About 1 hr to fix

          Function createBufferOnWorker has 7 arguments (exceeds 4 allowed). Consider refactoring.
          Open

          SuperTaskCluster.prototype.createBufferOnWorker = function STC_CREATE_BUFFER(workerID, name, buffer, encoding, mutable, chunky, callback) {
          Severity: Major
          Found in cluster.js - About 50 mins to fix

            Function autoBuffer has 7 arguments (exceeds 4 allowed). Consider refactoring.
            Open

            CommunicationModel.prototype.autoBuffer = function STC_AUTO_BUFFER(workerID, name, buffer, encoding, mutable, sendInChunks, callback) {
            Severity: Major
            Found in lib/CommunicationModel.js - About 50 mins to fix

              Function STC_BUFFER has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

              CommunicationModel.prototype.buffer = function STC_BUFFER(workerID, BufferProtoObject, buffer, sendInChunks, callback) {
                  var _this = this;
                  
                  // Chucks buffer/Creates MD5 checksum etc.
                  BufferProtoObject.setBuffer(buffer);
              Severity: Minor
              Found in lib/CommunicationModel.js - About 45 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

              Function STCBA_GET has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
              Open

              BufferAllocator.prototype.get = function STCBA_GET(offset, length) {
                  "use strict";
                  if(!this.done) return false;
                  if(!offset) offset = 0;
                  else offset = parseInt(offset);
              Severity: Minor
              Found in lib/BufferAllocator.js - About 45 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

              Function BufferAllocator has 5 arguments (exceeds 4 allowed). Consider refactoring.
              Open

              var BufferAllocator = function STCBA_INIT(name, size, encoding, immutable, split) {
              Severity: Minor
              Found in lib/BufferAllocator.js - About 35 mins to fix

                Function allocate has 5 arguments (exceeds 4 allowed). Consider refactoring.
                Open

                CommunicationModel.prototype.allocate = function STC_COM_ALLOCATE(name, size, encoding, mutable, split) {
                Severity: Minor
                Found in lib/CommunicationModel.js - About 35 mins to fix

                  Function buffer has 5 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                  CommunicationModel.prototype.buffer = function STC_BUFFER(workerID, BufferProtoObject, buffer, sendInChunks, callback) {
                  Severity: Minor
                  Found in lib/CommunicationModel.js - About 35 mins to fix

                    Function STC_HANDLER has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                    Open

                    SuperTaskCluster.prototype._STC_HANDLER = function STC_HANDLER() {
                        var args = Array.prototype.slice.call(arguments);
                        var name = args.shift(), context = args.shift(), callback = args.pop();
                        // Run task on a free Worker
                        var Candidate = { v: Infinity, i: 'master', refs: 0 };
                    Severity: Minor
                    Found in cluster.js - About 25 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

                    Missing radix parameter.
                    Open

                        else length = parseInt(length);
                    Severity: Minor
                    Found in lib/BufferAllocator.js by eslint

                    Require Radix Parameter (radix)

                    When using the parseInt() function it is common to omit the second argument, the radix, and let the function try to determine from the first argument what type of number it is. By default, parseInt() will autodetect decimal and hexadecimal (via 0x prefix). Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.

                    This confusion led to the suggestion that you always use the radix parameter to parseInt() to eliminate unintended consequences. So instead of doing this:

                    var num = parseInt("071");      // 57

                    Do this:

                    var num = parseInt("071", 10);  // 71

                    ECMAScript 5 changed the behavior of parseInt() so that it no longer autodetects octal literals and instead treats them as decimal literals. However, the differences between hexadecimal and decimal interpretation of the first parameter causes many developers to continue using the radix parameter to ensure the string is interpreted in the intended way.

                    On the other hand, if the code is targeting only ES5-compliant environments passing the radix 10 may be redundant. In such a case you might want to disallow using such a radix.

                    Rule Details

                    This rule is aimed at preventing the unintended conversion of a string to a number of a different base than intended or at preventing the redundant 10 radix if targeting modern environments only.

                    Options

                    There are two options for this rule:

                    • "always" enforces providing a radix (default)
                    • "as-needed" disallows providing the 10 radix

                    always

                    Examples of incorrect code for the default "always" option:

                    /*eslint radix: "error"*/
                    
                    var num = parseInt("071");
                    
                    var num = parseInt(someValue);
                    
                    var num = parseInt("071", "abc");
                    
                    var num = parseInt();

                    Examples of correct code for the default "always" option:

                    /*eslint radix: "error"*/
                    
                    var num = parseInt("071", 10);
                    
                    var num = parseInt("071", 8);
                    
                    var num = parseFloat(someValue);

                    as-needed

                    Examples of incorrect code for the "as-needed" option:

                    /*eslint radix: ["error", "as-needed"]*/
                    
                    var num = parseInt("071", 10);
                    
                    var num = parseInt("071", "abc");
                    
                    var num = parseInt();

                    Examples of correct code for the "as-needed" option:

                    /*eslint radix: ["error", "as-needed"]*/
                    
                    var num = parseInt("071");
                    
                    var num = parseInt("071", 8);
                    
                    var num = parseFloat(someValue);

                    When Not To Use It

                    If you don't want to enforce either presence or omission of the 10 radix value you can turn this rule off.

                    Further Reading

                    Expected return with your callback function.
                    Open

                                    callback(null, { allocated: true });
                    Severity: Minor
                    Found in lib/CommunicationModel.js by eslint

                    Enforce Return After Callback (callback-return)

                    The callback pattern is at the heart of most I/O and event-driven programming in JavaScript.

                    function doSomething(err, callback) {
                        if (err) {
                            return callback(err);
                        }
                        callback();
                    }

                    To prevent calling the callback multiple times it is important to return anytime the callback is triggered outside of the main function body. Neglecting this technique often leads to issues where you do something more than once. For example, in the case of an HTTP request, you may try to send HTTP headers more than once leading Node.js to throw a Can't render headers after they are sent to the client. error.

                    Rule Details

                    This rule is aimed at ensuring that callbacks used outside of the main function block are always part-of or immediately preceding a return statement. This rule decides what is a callback based on the name of the function being called.

                    Options

                    The rule takes a single option - an array of possible callback names - which may include object methods. The default callback names are callback, cb, next.

                    Default callback names

                    Examples of incorrect code for this rule with the default ["callback", "cb", "next"] option:

                    /*eslint callback-return: "error"*/
                    
                    function foo(err, callback) {
                        if (err) {
                            callback(err);
                        }
                        callback();
                    }

                    Examples of correct code for this rule with the default ["callback", "cb", "next"] option:

                    /*eslint callback-return: "error"*/
                    
                    function foo(err, callback) {
                        if (err) {
                            return callback(err);
                        }
                        callback();
                    }

                    Supplied callback names

                    Examples of incorrect code for this rule with the option ["done", "send.error", "send.success"]:

                    /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
                    
                    function foo(err, done) {
                        if (err) {
                            done(err);
                        }
                        done();
                    }
                    
                    function bar(err, send) {
                        if (err) {
                            send.error(err);
                        }
                        send.success();
                    }

                    Examples of correct code for this rule with the option ["done", "send.error", "send.success"]:

                    /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/
                    
                    function foo(err, done) {
                        if (err) {
                            return done(err);
                        }
                        done();
                    }
                    
                    function bar(err, send) {
                        if (err) {
                            return send.error(err);
                        }
                        send.success();
                    }

                    Known Limitations

                    Because it is difficult to understand the meaning of a program through static analysis, this rule has limitations:

                    • false negatives when this rule reports correct code, but the program calls the callback more than one time (which is incorrect behavior)
                    • false positives when this rule reports incorrect code, but the program calls the callback only one time (which is correct behavior)

                    Passing the callback by reference

                    The static analysis of this rule does not detect that the program calls the callback if it is an argument of a function (for example, setTimeout).

                    Example of a false negative when this rule reports correct code:

                    /*eslint callback-return: "error"*/
                    
                    function foo(err, callback) {
                        if (err) {
                            setTimeout(callback, 0); // this is bad, but WILL NOT warn
                        }
                        callback();
                    }

                    Triggering the callback within a nested function

                    The static analysis of this rule does not detect that the program calls the callback from within a nested function or an immediately-invoked function expression (IIFE).

                    Example of a false negative when this rule reports correct code:

                    /*eslint callback-return: "error"*/
                    
                    function foo(err, callback) {
                        if (err) {
                            process.nextTick(function() {
                                return callback(); // this is bad, but WILL NOT warn
                            });
                        }
                        callback();
                    }

                    If/else statements

                    The static analysis of this rule does not detect that the program calls the callback only one time in each branch of an if statement.

                    Example of a false positive when this rule reports incorrect code:

                    /*eslint callback-return: "error"*/
                    
                    function foo(err, callback) {
                        if (err) {
                            callback(err); // this is fine, but WILL warn
                        } else {
                            callback();    // this is fine, but WILL warn
                        }
                    }

                    When Not To Use It

                    There are some cases where you might want to call a callback function more than once. In those cases this rule may lead to incorrect behavior. In those cases you may want to reserve a special name for those callbacks and not include that in the list of callbacks that trigger warnings.

                    Further Reading

                    Related Rules

                    Severity
                    Category
                    Status
                    Source
                    Language