From 7345771df68a30a9f8196c3bde041eec96be80a6 Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Thu, 12 Mar 2015 14:57:26 +1300 Subject: [PATCH 1/2] Add SolrIndexCheck to the list of available checks in the README. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index afed363..2d820ad 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Register checks in your own `_config.php` - see the `_config.php` in this module like static caches, as well as for backup files and folders. * `ExternalURLCheck`: Checks that one or more URLs are reachable via HTTP. * `SMTPConnectCheck`: Checks if the SMTP connection configured through PHP.ini works as expected. + * `SolrIndexCheck`: Checks if the Solr cores of given class are available. ## Authentication From fdd78d1a2696187d1ff64d9906dc0204d9e6316c Mon Sep 17 00:00:00 2001 From: Mateusz Uzdowski Date: Thu, 12 Mar 2015 14:58:03 +1300 Subject: [PATCH 2/2] NEW Add alternative way of setup via the config system. --- README.md | 44 +++++++++++-- code/EnvironmentCheckSuite.php | 109 +++++++++++++++++++++++++-------- 2 files changed, 124 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 2d820ad..e90e5fd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # SilverStripe Environment Checker Module -Developed by Sam Minnée, thanks to Will Rossiter. +Initially developed by Sam Minnée, thanks to Will Rossiter. This module adds an API for running environment checks to your API. @@ -17,11 +17,47 @@ Almost, but not really. Environment checks differ from unit tests in two importa ## Installation +There are two ways to register your checks, both can be used at the same time. The checks will be appended to the suite. + +### Direct method + Register checks in your own `_config.php` - see the `_config.php` in this module for some defaults. - :::php - EnvironmentCheckSuite::register('health', 'DatabaseCheck', "Can we connect to the database?"); - EnvironmentCheckSuite::register('check', 'URLCheck("")', "Is the homepage accessible?"); +```php +EnvironmentCheckSuite::register('health', 'DatabaseCheck', "Can we connect to the database?"); +EnvironmentCheckSuite::register('check', 'URLCheck("")', "Is the homepage accessible?"); +``` + +### Config system method + +Register your checks on the `EnvironmentCheckSuite`. The same named check may be used multiple times. + +```yaml +EnvironmentCheckSuite: + registered_checks: + db: + definition: 'DatabaseCheck("Page")' + title: 'Is the database accessible?' + url: + definition: 'URLCheck()' + title: 'Is the homepage accessible?' + registered_suites: + check: + - db + health: + - db + - url +``` + +You can also disable checks configured this way. This is handy if you want to override a check imposed on your project +by some other module. Just set the "state" property of the check to "disabled" like this: + +```yaml +EnvironmentCheckSuite: + registered_checks: + db: + state: disabled +``` ## Available checks diff --git a/code/EnvironmentCheckSuite.php b/code/EnvironmentCheckSuite.php index 8dea106..f9357b4 100644 --- a/code/EnvironmentCheckSuite.php +++ b/code/EnvironmentCheckSuite.php @@ -1,24 +1,84 @@ run(); */ -class EnvironmentCheckSuite { +class EnvironmentCheckSuite extends Object { + + /** + * Name of this suite. + */ + protected $name; + protected $checks = array(); - + + /** + * Associative array of named checks registered via the config system. Each check should specify: + * - definition (e.g. 'MyHealthCheck("my param")') + * - title (e.g. 'Is my feature working?') + * - state (setting this to 'disabled' will cause suites to skip this check entirely. + */ + private static $registered_checks; + + /** + * Associative array of named suites registered via the config system. Each suite should enumerate + * named checks that have been configured in 'registered_checks'. + */ + private static $registered_suites; + + /** + * Load checks for this suite from the configuration system. This is an alternative to the + * EnvironmentCheckSuite::register - both can be used, checks will be appended to the suite. + * + * @param string $suiteName The name of this suite. + */ + public function __construct($suiteName) { + if (empty($this->config()->registered_suites[$suiteName])) { + // Not registered via config system, but it still may be configured later via self::register. + return; + } + + foreach ($this->config()->registered_suites[$suiteName] as $checkName) { + if (empty($this->config()->registered_checks[$checkName])) { + throw new InvalidArgumentException( + "Bad EnvironmentCheck: '$checkName' - the named check has not been registered." + ); + } + + $check = $this->config()->registered_checks[$checkName]; + + // Existing named checks can be disabled by setting their 'state' to 'disabled'. + // This is handy for disabling checks mandated by modules. + if (!empty($check['state']) && $check['state']==='disabled') continue; + + // Add the check to this suite. + $this->push($check['definition'], $check['title']); + } + } + /** * Run this test suite * @return The result code of the worst result. */ public function run() { $worstResult = 0; - - $result = new EnvironmentCheckSuiteResult; + + $result = new EnvironmentCheckSuiteResult(); foreach($this->checkInstances() as $check) { list($checkClass, $checkTitle) = $check; try { @@ -30,10 +90,10 @@ class EnvironmentCheckSuite { } $result->addResult($status, $message, $checkTitle); } - + return $result; } - + /** * Get instances of all the environment checks */ @@ -56,10 +116,9 @@ class EnvironmentCheckSuite { } return $output; } - + /** * Add a check to this suite. - * */ public function push($check, $title = null) { if(!$title) { @@ -67,22 +126,22 @@ class EnvironmentCheckSuite { } $this->checks[] = array($check, $title); } - + ///////////////////////////////////////////////////////////////////////////////////////////// - + protected static $instances = array(); - + /** * Return a named instance of EnvironmentCheckSuite. */ static function inst($name) { - if(!isset(self::$instances[$name])) self::$instances[$name] = new EnvironmentCheckSuite(); + if(!isset(self::$instances[$name])) self::$instances[$name] = new EnvironmentCheckSuite($name); return self::$instances[$name]; } /** * Register a check against the named check suite. - * + * * @param String|Array */ static function register($names, $check, $title = null) { @@ -101,38 +160,38 @@ class EnvironmentCheckSuiteResult extends ViewableData { parent::__construct(); $this->details = new ArrayList(); } - + function addResult($status, $message, $checkIdentifier) { $this->details->push(new ArrayData(array( 'Check' => $checkIdentifier, 'Status' => $this->statusText($status), 'Message' => $message, ))); - + $this->worst = max($this->worst, $status); } - + /** * Returns true if there are no ERRORs, only WARNINGs or OK */ function ShouldPass() { return $this->worst <= EnvironmentCheck::WARNING; } - + /** * Returns overall (i.e. worst) status as a string. */ function Status() { return $this->statusText($this->worst); } - + /** * Returns detailed status information about each check */ function Details() { return $this->details; } - + /** * Return a text version of a status code */ @@ -145,4 +204,4 @@ class EnvironmentCheckSuiteResult extends ViewableData { default: throw new InvalidArgumentException("Bad environment check status '$status'"); } } -} \ No newline at end of file +}