Skip to content

Commit 00e8bad

Browse files
committed
Add healthcheck to status page
1 parent 6f86988 commit 00e8bad

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

controllers/Status.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
namespace BNETDocs\Controllers;
44

5+
use \BNETDocs\Libraries\Cache;
56
use \BNETDocs\Libraries\Common;
67
use \BNETDocs\Libraries\Controller;
8+
use \BNETDocs\Libraries\Database;
9+
use \BNETDocs\Libraries\DatabaseDriver;
710
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
811
use \BNETDocs\Libraries\Router;
912
use \BNETDocs\Models\Status as StatusModel;
1013
use \BNETDocs\Views\StatusJSON as StatusJSONView;
1114
use \BNETDocs\Views\StatusPlain as StatusPlainView;
1215
use \DateTime;
1316
use \DateTimeZone;
17+
use \StdClass;
1418

1519
class Status extends Controller {
1620

@@ -26,21 +30,34 @@ public function run(Router &$router) {
2630
throw new UnspecifiedViewException();
2731
}
2832
$model = new StatusModel();
29-
$this->getStatus($model);
33+
$code = (!$this->getStatus($model) ? 500 : 200);
3034
ob_start();
3135
$view->render($model);
32-
$router->setResponseCode(200);
36+
$router->setResponseCode($code);
3337
$router->setResponseTTL(300);
3438
$router->setResponseHeader("Content-Type", $view->getMimeType());
3539
$router->setResponseContent(ob_get_contents());
3640
ob_end_clean();
3741
}
3842

3943
protected function getStatus(StatusModel &$model) {
44+
if (!isset(Common::$database)) {
45+
Common::$database = DatabaseDriver::getDatabaseObject();
46+
}
47+
48+
$healthcheck = new StdClass();
49+
$healthcheck->database = (Common::$database instanceof Database);
50+
$healthcheck->memcache = (Common::$cache instanceof Cache );
51+
52+
$model->healthcheck = $healthcheck;
4053
$model->remote_address = getenv("REMOTE_ADDR");
4154
$model->remote_geoinfo = geoip_record_by_name($model->remote_address);
4255
$model->timestamp = new DateTime("now", new DateTimeZone("UTC"));
4356
$model->version_info = Common::$version;
57+
58+
foreach ($healthcheck as $key => $val) {
59+
if (is_bool($val) && !$val) return false;
60+
}
4461
return true;
4562
}
4663

views/StatusJSON.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function render(Model &$model) {
2020
throw new IncorrectModelException();
2121
}
2222
echo json_encode([
23+
"healthcheck" => $model->healthcheck,
2324
"remote_address" => $model->remote_address,
2425
"remote_geoinfo" => $model->remote_geoinfo,
2526
"timestamp" => $model->timestamp->format("r"),

views/StatusPlain.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public function render(Model &$model) {
1919
if (!$model instanceof StatusModel) {
2020
throw new IncorrectModelException();
2121
}
22+
foreach ($model->healthcheck as $key => $val) {
23+
if (is_bool($val))
24+
echo "healthcheck_" . $key . " " . ($val ? "true" : "false") . "\n";
25+
else if (is_null($val))
26+
echo "healthcheck_" . $key . " null\n";
27+
else if (is_scalar($val))
28+
echo "healthcheck_" . $key . " " . $val . "\n";
29+
else
30+
echo "healthcheck_" . $key . " " . gettype($val) . "\n";
31+
}
2232
echo "remote_address " . $model->remote_address . "\n";
2333
if ($model->remote_geoinfo) {
2434
foreach ($model->remote_geoinfo as $key => $val) {

0 commit comments

Comments
 (0)