From cf8133c4576130f3f4a788a737492db3d02fb1f6 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Thu, 5 Apr 2007 05:04:06 +0000 Subject: [PATCH] lib/setup moodelib: Introducing moodle_request_shutdown() Now Moodle will have a 'shutdown_function' function. Right now registered (and useful) only when running under apache + mod_php. This initial function checks whether memory usage in this process exceeded 10MiB or $CFG->apachemaxmem (int, in bytes). This is really useful to prevent using more RAM than we have. --- lib/moodlelib.php | 25 +++++++++++++++++++++++++ lib/setup.php | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 9b8b0405c2..7e02ee3475 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6499,6 +6499,31 @@ function fullclone($thing) { } +/* + * This function expects to called during shutdown + * should be set via register_shutdown_function() + * in lib/setup.php . + * + * Right now we do it only if we are under apache, to + * make sure apache children that hog too much mem are + * killed. + * + */ +function moodle_request_shutdown() { + + // initially, we are only ever called under apache + // but check just in case + if (function_exists('apache_child_terminate') && function_exists('memory_get_usage')) { + if (empty($CFG->apachemaxmem)) { + $CFG->apachemaxmem = 10000000; // default 10MiB + } + if (memory_get_usage() > (int)$CFG->apachemaxmem) { + trigger_error('Mem usage over $CFG->apachemaxmem: marking child for reaping.'); + apache_child_terminate(); + } + } +} + /** * If new messages are waiting for the current user, then return * Javascript code to create a popup window diff --git a/lib/setup.php b/lib/setup.php index 74405d6115..57c8587c81 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -224,6 +224,11 @@ global $HTTPSPAGEREQUIRED; } +/// For now, only needed under apache (and probably unstable in other contexts) + if (function_exists('apache_child_terminate')) { + register_shutdown_function('moodle_request_shutdown'); + } + //// Defining the site if ($SITE = get_site()) { /** -- 2.39.5