<commit_before>/**
 * Module dependencies.
 */

var async = require('async');
var _ = require('lodash');


/**
 * Sails.prototype.lower()
 *
 * The inverse of `lift()`, this method
 * shuts down all attached servers.
 *
 * It also unbinds listeners and terminates child processes.
 *
 * @api public
 */

module.exports = function lower(cb) {
  var sails = this;

  sails.log.verbose('Lowering sails...');
  // Callback is optional
  cb = cb || function(err) {
    if (err) return sails.log.error(err);
  };

  // Flag `sails._exiting` as soon as the app has begun to shutdown.
  // This may be used by hooks and other parts of core.
  // (e.g. to stop handling HTTP requests and prevent ugly error msgs)
  sails._exiting = true;

  var beforeShutdown = (sails.config && sails.config.beforeShutdown) || function(cb) {
    return cb();
  };

  // Wait until beforeShutdown logic runs
  beforeShutdown(function(err) {

    // If an error occurred, don't stop-- still try to kill the child processes.
    if (err) {
      sails.log.error(err);
    }

    // Kill all child processes
    _.each(sails.childProcesses, function kill(childProcess) {
      sails.log.verbose('Sent kill signal to child process (' + childProcess.pid + ')...');
      try {
        childProcess.kill('SIGINT');
      } catch (e) {
        sails.log.warn('Error received killing child process: ', e.message);
      }
    });

    // Shut down HTTP server
    // TODO: defer this to the http and sockets hooks-- use sails.emit('lowering')
    // Shut down Socket server
    // wait for all attached servers to stop
    sails.emit('lower');
    var log = sails.log.verbose;

    async.series([

      function shutdownSockets(cb) {
        if (!sails.hooks || !sails.hooks.sockets) {
          return cb();
        }

        try {
          log('Shutting down socket server...');
          var timeOut = setTimeout(cb, 100);
          sails.io.server.unref();
          sails.io.server.close();
          sails.io.server.on('close', function() {
            log('Socket server shut down successfully.');
            clearTimeout(timeOut);
            cb();
          });
        } catch (e) {
          clearTimeout(timeOut);
          cb();
        }
      },

      function shutdownHTTP(cb) {

        if (!sails.hooks.http) {
          return cb();
        }

        try {
          log('Shutting down HTTP server...');
          var timeOut = setTimeout(cb, 100);
          sails.hooks.http.server.unref();
          sails.hooks.http.server.close();
          sails.hooks.http.server.on('close', function() {
            log('HTTP server shut down successfully.');
            clearTimeout(timeOut);
            cb();
          });
        } catch (e) {
          clearTimeout(timeOut);
          cb();
        }
      },

      function removeListeners(cb) {
        // Manually remove all event listeners
        for (var key in sails._events) {
          sails.removeAllListeners(key);
        }

        // If `sails.config.process.removeAllListeners` is set, do that.
        if (sails.config && sails.config.process && sails.config.process.removeAllListeners) {
          process.removeAllListeners();<commit_msg>In sails.lower, remove all listeners without letting the eloop go through a cycle.  This prevents ornery loops and such from displaying ugly errors on lower.<commit_after>/**
 * Module dependencies.
 */

var async = require('async');
var _ = require('lodash');


/**
 * Sails.prototype.lower()
 *
 * The inverse of `lift()`, this method
 * shuts down all attached servers.
 *
 * It also unbinds listeners and terminates child processes.
 *
 * @api public
 */

module.exports = function lower(cb) {
  var sails = this;

  sails.log.verbose('Lowering sails...');
  // Callback is optional
  cb = cb || function(err) {
    if (err) return sails.log.error(err);
  };

  // Flag `sails._exiting` as soon as the app has begun to shutdown.
  // This may be used by hooks and other parts of core.
  // (e.g. to stop handling HTTP requests and prevent ugly error msgs)
  sails._exiting = true;

  var beforeShutdown = (sails.config && sails.config.beforeShutdown) || function(cb) {
    return cb();
  };

  // Wait until beforeShutdown logic runs
  beforeShutdown(function(err) {

    // If an error occurred, don't stop-- still try to kill the child processes.
    if (err) {
      sails.log.error(err);
    }

    // Kill all child processes
    _.each(sails.childProcesses, function kill(childProcess) {
      sails.log.verbose('Sent kill signal to child process (' + childProcess.pid + ')...');
      try {
        childProcess.kill('SIGINT');
      } catch (e) {
        sails.log.warn('Error received killing child process: ', e.message);
      }
    });

    // Shut down HTTP server
    // TODO: defer this to the http and sockets hooks-- use sails.emit('lowering')
    // Shut down Socket server
    // wait for all attached servers to stop
    sails.emit('lower');
    var log = sails.log.verbose;

    async.series([

      /////////////////////////////////////////////////////////////////////////////////////
      // Removed this in v0.11 since it seems to be handled by the removing of listeners
      // below.  All tests pass, but please tweet @mikermcneil if you encounter any issues.
      // (the only issues we foresee would be in some kind of unexpected automated testing
      //  scenario)
      /////////////////////////////////////////////////////////////////////////////////////
      /////////////////////////////////////////////////////////////////////////////////////
      /////////////////////////////////////////////////////////////////////////////////////
      //
      // function shutdownSockets(cb) {
      //   if (!sails.hooks || !sails.hooks.sockets) {
      //     return cb();
      //   }

      //   try {
      //     log('Shutting down socket server...');
      //     var timeOut = setTimeout(cb, 100);
      //     sails.io.server.unref();
      //     sails.io.server.close();
      //     sails.io.server.on('close', function() {
      //       log('Socket server shut down successfully.');
      //       clearTimeout(timeOut);
      //       cb();
      //     });
      //   } catch (e) {
      //     clearTimeout(timeOut);
      //     cb();
      //   }
      // },

      // function shutdownHTTP(cb) {

      //   if (!sails.hooks.http) {
      //     return cb();
      //   }

      //   try {
      //     log('Shutting down HTTP server...');
      //     var timeOut = setTimeout(cb, 100);
      //     sails.hooks.http.server.unref();
      //     sails.hooks.http.server.close();
      //     sails.hooks.http.server.on('close', function() {
      //       log('HTTP server shut down successfully.');
      //       clearTimeout(timeOut);
      //       cb();
      //     });
      //   } catch (e) {
      //     clearTimeout(timeOut);
      //     cb();
      //   }
      // },
      //
      /////////////////////////////////////////////////////////////////////////////////////
      /////////////////////////////////////////////////////////////////////////////////////
      /////////////////////////////////////////////////////////////////////////////////////

      function removeListeners(cb) {
        // Manually remove all event listeners
        for (var key in sails._events) {
          sails.removeAllListeners(key);
        }

        // If `sails.config.process.removeAllListeners` is set, do that.
        if (sails.config && sails.config.process && sails.config.process.removeAllListeners) {
          process.removeAllListeners();
{'fraction_non_alphanumeric': 0.20178900289397528, 'fraction_numerical': 0.001973164956590371, 'mean_word_length': 1.5556302521008403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7272482', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2040, 'n_words': 570}