kieranajp / healthz
PHP应用的健康检查。
v0.0.1
2019-08-22 10:19 UTC
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ^4.8
This package is auto-updated.
Last update: 2024-09-22 23:03:03 UTC
README
PHP Healthz
PHP应用的健康检查。
轻松了解您应用程序的健康状况!为负载均衡器或自己的心理平衡实现健康检查端点:)
所有荣誉归generationtux的上游版本所有。这是一个修改版本,移除了很多东西。特别是
设置
$ composer require generationtux/healthz
构建健康检查的实例
<?php use Gentux\Healthz\Healthz; $memcached = (new MemcachedHealthCheck())->addServer('127.0.0.1'); $healthz = new Healthz([$memcached]);
运行检查并查看结果
// @var $results Gentux\Healthz\ResultStack $results = $healthz->run(); if ($results->hasFailures()) { // oh no } if ($results->hasWarnings()) { // hmm } foreach ($results->all() as $result) { // @var $result Gentux\Healthz\HealthResult if ($result->passed() || $result->warned() || $result->failed()) { echo "it did one of those things at least"; } echo "{$result->title()}: {$result->status()} ({$result->description()})"; }
编写检查
注意:检查可能有三种状态之一(success
、warning
或failure
)。任何成功和警告的组合以及整个堆栈都将被视为成功。然而,任何单个失败都将使整个堆栈被视为失败。
要创建自定义健康检查,您应该扩展Gentux\Healthz\HealthCheck
并实现一个抽象方法run()
。
<?php use Gentux\Healthz\HealthCheck; class MyCustomCheck extends HealthCheck { /** @var string Optionally set a title, otherwise the class name will be used */ protected $title = ''; /** @var string Optionally set a description, just to provide more info on the UI */ protected $description = ''; public function run() { // any exception that is thrown will consider the check unhealthy } }
如果没有抛出异常,则假定检查成功。否则,异常的消息将用作失败检查的status
。
public function run() { throw new Exception('Heres why the check failed.'); }
如果您希望检查显示warning
而不是完全失败,请抛出Gentux\Healthz\Exceptions\HealthWarningException
的实例。
use Gentux\Healthz\Exceptions\HealthWarningException; public function run() { throw new HealthWarningException("The check didn't fail, but here ye be warned."); }