Skip to content

Commit

Permalink
MDL-45724 cache: admin screen now shows store warnings
Browse files Browse the repository at this point in the history
Implemented a means by which cache store instance can communicate
warnings due to things such as conflicting configurations etc.
  • Loading branch information
Sam Hemelryk committed Jul 20, 2014
1 parent a9e0505 commit 67a77fa
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 10 deletions.
25 changes: 16 additions & 9 deletions cache/admin.php
Expand Up @@ -52,7 +52,7 @@

$title = new lang_string('cacheadmin', 'cache');
$mform = null;
$notification = null;
$notifications = array();
$notifysuccess = true;

if (!empty($action) && confirm_sesskey()) {
Expand Down Expand Up @@ -110,10 +110,10 @@

if (!array_key_exists($store, $stores)) {
$notifysuccess = false;
$notification = get_string('invalidstore', 'cache');
$notifications[] = array(get_string('invalidstore', 'cache'), false);
} else if ($stores[$store]['mappings'] > 0) {
$notifysuccess = false;
$notification = get_string('deletestorehasmappings', 'cache');
$notifications[] = array(get_string('deletestorehasmappings', 'cache'), false);
}

if ($notifysuccess) {
Expand Down Expand Up @@ -250,10 +250,10 @@
$confirm = optional_param('confirm', false, PARAM_BOOL);
if (!array_key_exists($lock, $locks)) {
$notifysuccess = false;
$notification = get_string('invalidlock', 'cache');
$notifications[] = array(get_string('invalidlock', 'cache'), false);
} else if ($locks[$lock]['uses'] > 0) {
$notifysuccess = false;
$notification = get_string('deletelockhasuses', 'cache');
$notifications[] = array(get_string('deletelockhasuses', 'cache'), false);
}
if ($notifysuccess) {
if (!$confirm) {
Expand All @@ -280,17 +280,24 @@
}
}

// Stores can add notices to the cache configuration screen for things like conflicting configurations etc.
// Here we check each cache to see if it has warnings.
foreach ($stores as $store) {
if (!empty($store['warnings']) && is_array($store['warnings'])) {
foreach ($store['warnings'] as $warning) {
$notifications[] = array($warning, false);
}
}
}

$PAGE->set_title($title);
$PAGE->set_heading($SITE->fullname);
/* @var core_cache_renderer $renderer */
$renderer = $PAGE->get_renderer('core_cache');

echo $renderer->header();
echo $renderer->heading($title);

if (!is_null($notification)) {
echo $renderer->notification($notification, ($notifysuccess)?'notifysuccess' : 'notifyproblem');
}
echo $renderer->notififications($notifications);

if ($mform instanceof moodleform) {
$mform->display();
Expand Down
12 changes: 12 additions & 0 deletions cache/classes/store.php
Expand Up @@ -340,4 +340,16 @@ public function create_clone(array $details = array()) {
// Any stores that have an issue with this will need to override the create_clone method.
return clone($this);
}

/**
* Can be overridden to return any warnings this store instance should make to the admin.
*
* This should be used to notify things like configuration conflicts etc.
* The warnings returned here will be displayed on the cache configuration screen.
*
* @return string[] Returns an array of warnings (strings)
*/
public function get_warnings() {
return array();
}
}
3 changes: 2 additions & 1 deletion cache/locallib.php
Expand Up @@ -709,7 +709,8 @@ public static function get_store_instance_summaries() {
'nativelocking' => ($store instanceof cache_is_lockable),
'keyawareness' => ($store instanceof cache_is_key_aware),
'searchable' => ($store instanceof cache_is_searchable)
)
),
'warnings' => $store->get_warnings()
);
if (empty($details['default'])) {
$return[$name] = $record;
Expand Down
20 changes: 20 additions & 0 deletions cache/renderer.php
Expand Up @@ -372,4 +372,24 @@ public function lock_summaries(array $locks) {
$html .= html_writer::end_tag('div');
return $html;
}

/**
* Renders an array of notifications for the cache configuration screen.
*
* @param array $notifications
* @return string
*/
public function notififications(array $notifications = array()) {
if (count($notifications) === 0) {
// There are no notifications to render.
return '';
}
$html = html_writer::start_div('notifications');
foreach ($notifications as $notification) {
list($message, $notifysuccess) = $notification;
$html .= $this->notification($message, ($notifysuccess) ? 'notifysuccess' : 'notifyproblem');
}
$html .= html_writer::end_div();
return $html;
}
}
1 change: 1 addition & 0 deletions cache/stores/memcache/lang/en/cachestore_memcache.php
Expand Up @@ -43,6 +43,7 @@
ipaddress:port
servername:port:weight
</pre>';
$string['sessionhandlerconflict'] = 'Warning: A memcache instance ({$a}) has being configured to use the same memcached server as sessions. Purging all caches will lead to sessions also being purged.';
$string['testservers'] = 'Test servers';
$string['testservers_desc'] = 'The test servers get used for unit tests and for performance tests. It is entirely optional to set up test servers. Servers should be defined one per line and consist of a server address and optionally a port and weight.
If no port is provided then the default port (11211) is used.';
24 changes: 24 additions & 0 deletions cache/stores/memcache/lib.php
Expand Up @@ -437,4 +437,28 @@ public static function initialise_test_instance(cache_definition $definition) {
public function my_name() {
return $this->name;
}

/**
* Used to notify of configuration conflicts.
*
* The warnings returned here will be displayed on the cache configuration screen.
*
* @return string[] Returns an array of warnings (strings)
*/
public function get_warnings() {
global $CFG;
$warnings = array();
if (isset($CFG->session_memcached_save_path) && count($this->servers)) {
$bits = explode(':', $CFG->session_memcached_save_path, 3);
$host = array_shift($bits);
$port = (count($bits)) ? array_shift($bits) : '11211';
foreach ($this->servers as $server) {
if ($server[0] === $host && $server[1] === $port) {
$warnings[] = get_string('sessionhandlerconflict', 'cachestore_memcache', $this->my_name());
break;
}
}
}
return $warnings;
}
}
1 change: 1 addition & 0 deletions cache/stores/memcached/lang/en/cachestore_memcached.php
Expand Up @@ -57,6 +57,7 @@
ipaddress:port
servername:port:weight
</pre>';
$string['sessionhandlerconflict'] = 'Warning: A memcached instance ({$a}) has being configured to use the same memcached server as sessions. Purging all caches will lead to sessions also being purged.';
$string['testservers'] = 'Test servers';
$string['testservers_desc'] = 'The test servers get used for unit tests and for performance tests. It is entirely optional to set up test servers. Servers should be defined one per line and consist of a server address and optionally a port and weight.
If no port is provided then the default port (11211) is used.';
Expand Down
25 changes: 25 additions & 0 deletions cache/stores/memcached/lib.php
Expand Up @@ -494,4 +494,29 @@ public static function initialise_test_instance(cache_definition $definition) {
public function my_name() {
return $this->name;
}

/**
* Used to notify of configuration conflicts.
*
* The warnings returned here will be displayed on the cache configuration screen.
*
* @return string[] Returns an array of warnings (strings)
*/
public function get_warnings() {
global $CFG;
$warnings = array();
if (isset($CFG->session_memcached_save_path) && count($this->servers)) {
$bits = explode(':', $CFG->session_memcached_save_path, 3);
$host = array_shift($bits);
$port = (count($bits)) ? array_shift($bits) : '11211';

foreach ($this->servers as $server) {
if ((string)$server[0] === $host && (string)$server[1] === $port) {
$warnings[] = get_string('sessionhandlerconflict', 'cachestore_memcached', $this->my_name());
break;
}
}
}
return $warnings;
}
}

0 comments on commit 67a77fa

Please sign in to comment.