# Code coverage report for lib/git/hooks/polling.js

## Statements: 100% (17 / 17) Branches: 100% (11 / 11) Functions: 100% (6 / 6) Lines: 100% (16 / 16) Ignored: 1 statement, 3 branches

All files » lib/git/hooks/ » polling.js

    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
    411

    1

    1

    4
    3

    1
    1
    1
    1

    1
    1

    1
    1
    1
    1

    1

     var logger = require('../../logging.js');

    // Set testing mode based on whether we're running in mocha or not.
    var testing = (process.argv[1].indexOf('_mocha'), process.argv[1].length - 6) != -1;

    /**
     * Set up a polling hook to update the git_manager on an interval.  This can
     * be a good failsafe so we aren't totally reliant on hooks making their way
     * to the system.
     */
    exports.init = function(config, repo) {

      if (!config.interval || config.interval < 0 || parseInt(config.interval) != config.interval) {
        throw 'Polling intervals must be positive integers';
      }

      var start_branch_updater = function(branch) {
        logger.trace('Polling branch %s of repo %s', branch.name, repo.name);
        setInterval(function() {
          branch.handleRefChange(null, function(err) {
            /* istanbul ignore next */
            Iif (err) logger.error(err);
            logger.debug('Updates in branch %s complete', branch.name);
          });
        }, testing ? 1000 /* istanbul ignore next */ :
          config.interval * 60 * 1000);
      };

      repo.branch_names.forEach(function(branch_name) {
        var branch = repo.branches[branch_name];
        setTimeout(function() {
          start_branch_updater(branch);
          // Start the branch updater some random number of seconds from 1 to 30.  This is just to space
          // out the updates and reduce load spikes against git servers.
        }, testing ? 10 : /* istanbul ignore next */
          ((Math.floor((Math.random() * 30) + 1) ) * 1000)); // In the common case, start polling a random number of seconds later.
      });

      logger.debug('Polling hook initialized with %s minute intervals', config.interval);
    };