[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/classes/ -> shutdown_manager.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Shutdown management class.
  19   *
  20   * @package    core
  21   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Shutdown management class.
  29   *
  30   * @package    core
  31   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_shutdown_manager {
  35      /** @var array list of custom callbacks */
  36      protected static $callbacks = array();
  37      /** @var bool is this manager already registered? */
  38      protected static $registered = false;
  39  
  40      /**
  41       * Register self as main shutdown handler.
  42       *
  43       * @private to be called from lib/setup.php only!
  44       */
  45      public static function initialize() {
  46          if (self::$registered) {
  47              debugging('Shutdown manager is already initialised!');
  48          }
  49          self::$registered = true;
  50          register_shutdown_function(array('core_shutdown_manager', 'shutdown_handler'));
  51      }
  52  
  53      /**
  54       * Register custom shutdown function.
  55       *
  56       * @param callable $callback
  57       * @param array $params
  58       */
  59      public static function register_function($callback, array $params = null) {
  60          self::$callbacks[] = array($callback, $params);
  61      }
  62  
  63      /**
  64       * @private - do NOT call directly.
  65       */
  66      public static function shutdown_handler() {
  67          global $DB;
  68  
  69          // Custom stuff first.
  70          foreach (self::$callbacks as $data) {
  71              list($callback, $params) = $data;
  72              try {
  73                  if (!is_callable($callback)) {
  74                      error_log('Invalid custom shutdown function detected '.var_export($callback, true));
  75                      continue;
  76                  }
  77                  if ($params === null) {
  78                      call_user_func($callback);
  79                  } else {
  80                      call_user_func_array($callback, $params);
  81                  }
  82              } catch (Exception $e) {
  83                  error_log('Exception ignored in shutdown function '.var_export($callback, true).':'.$e->getMessage());
  84              }
  85          }
  86  
  87          // Handle DB transactions, session need to be written afterwards
  88          // in order to maintain consistency in all session handlers.
  89          if ($DB->is_transaction_started()) {
  90              if (!defined('PHPUNIT_TEST') or !PHPUNIT_TEST) {
  91                  // This should not happen, it usually indicates wrong catching of exceptions,
  92                  // because all transactions should be finished manually or in default exception handler.
  93                  $backtrace = $DB->get_transaction_start_backtrace();
  94                  error_log('Potential coding error - active database transaction detected during request shutdown:'."\n".format_backtrace($backtrace, true));
  95              }
  96              $DB->force_transaction_rollback();
  97          }
  98  
  99          // Close sessions - do it here to make it consistent for all session handlers.
 100          \core\session\manager::write_close();
 101  
 102          // Other cleanup.
 103          self::request_shutdown();
 104  
 105          // Stop profiling.
 106          if (function_exists('profiling_is_running')) {
 107              if (profiling_is_running()) {
 108                  profiling_stop();
 109              }
 110          }
 111  
 112          // NOTE: do not dispose $DB and MUC here, they might be used from legacy shutdown functions.
 113      }
 114  
 115      /**
 116       * Standard shutdown sequence.
 117       */
 118      protected static function request_shutdown() {
 119          global $CFG;
 120  
 121          // Help apache server if possible.
 122          $apachereleasemem = false;
 123          if (function_exists('apache_child_terminate') && function_exists('memory_get_usage') && ini_get_bool('child_terminate')) {
 124              $limit = (empty($CFG->apachemaxmem) ? 64*1024*1024 : $CFG->apachemaxmem); // 64MB default.
 125              if (memory_get_usage() > get_real_size($limit)) {
 126                  $apachereleasemem = $limit;
 127                  @apache_child_terminate();
 128              }
 129          }
 130  
 131          // Deal with perf logging.
 132          if (defined('MDL_PERF') || (!empty($CFG->perfdebug) and $CFG->perfdebug > 7)) {
 133              if ($apachereleasemem) {
 134                  error_log('Mem usage over '.$apachereleasemem.': marking Apache child for reaping.');
 135              }
 136              if (defined('MDL_PERFTOLOG')) {
 137                  $perf = get_performance_info();
 138                  error_log("PERF: " . $perf['txt']);
 139              }
 140              if (defined('MDL_PERFINC')) {
 141                  $inc = get_included_files();
 142                  $ts  = 0;
 143                  foreach ($inc as $f) {
 144                      if (preg_match(':^/:', $f)) {
 145                          $fs = filesize($f);
 146                          $ts += $fs;
 147                          $hfs = display_size($fs);
 148                          error_log(substr($f, strlen($CFG->dirroot)) . " size: $fs ($hfs)", null, null, 0);
 149                      } else {
 150                          error_log($f , null, null, 0);
 151                      }
 152                  }
 153                  if ($ts > 0 ) {
 154                      $hts = display_size($ts);
 155                      error_log("Total size of files included: $ts ($hts)");
 156                  }
 157              }
 158          }
 159      }
 160  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1