codete/health-check

提供统一方式运行应用健康检查的库

1.0.0 2017-04-07 11:49 UTC

This package is not auto-updated.

Last update: 2024-09-09 06:39:23 UTC


README

Build Status

目的

HealthCheck 库旨在提供一种简单的方式来编写任意系统检查,这些检查会定期运行。对于库来说,你检查什么并不重要,重要的是检查结果是否为OK、WARNING或ERROR,以及库如何帮助你通知结果。

要求

库的唯一要求是您使用PHP版本不早于7.1。库还提供了一些开箱即用的集成,您可以在require-dev部分进行检查。此外,我们还为所有喜欢Symfony的人提供了一个bundle,它将库集成变得轻而易举。

HealthCheck

任何健康检查都必须实现\Codete\HealthCheck\HealthCheck接口。要在库中注册您的检查,如果您使用Symfony,可以给服务添加hc.health_check标签,或者手动将其添加到\Codete\HealthCheck\HealthCheckRegistry

为了履行HealthCheck协议,您的实现必须提供2个方法

  • getName - 检查的可读名称
  • check - 检查的实际逻辑

此外,库还提供了\Codete\HealthCheck\ExpiringHealthCheck,允许您为检查设置一个过期日期。这在使用已知版本将不再被支持时特别有用。实际上,OK但有效日期即将到期的检查将转换为警告。

check方法必须返回一个\Codete\HealthCheck\HealthStatus对象,该对象封装了检查结果(OK、WARNING或ERROR)和可选的消息。根据状态结果,结果处理器可以相应地采取行动。

结果处理器

结果处理器是处理健康检查结果的服务。任何结果处理器都必须实现\Codete\HealthCheck\ResultHandler接口。要在库中注册您的处理器,如果您使用Symfony,可以给服务添加hc.result_handler标签,并提供唯一的id参数,或者手动将其添加到\Codete\HealthCheck\ResultHandlerRegistry。例如实现,您可以查看\Codete\HealthCheck\ResultHandler\命名空间。

命令

Bundle提供两个命令

  • health-check:run <fqcn>运行一个健康检查并输出其状态
  • health-check:run-all运行所有注册的健康检查,并以一种良好的方式输出结果

Bundle配置参考

health_check:
  handlers:
    psr3:
      type: psr3                      # PSR3 compatible handler
      id: logger                      # service identifier
      level: warning                  # level that should be used for reporting
    chain:
      type: chain                     # groups together many handlers
      members: [ psr3, name_of_your_tagged_handler ]
    elephpant:
      type: remembering               # aggregates results to be fetched later
    slack:
      type: slack                     # posts result to Slack channel
      url: https://hooks.slack.com/   # endpoint for an incoming webhook
      channel: dev                    # channel to post in
      username: notifier              # username for bot
      icon: ':angel:'                 # icon for bot
  status:                             # choose which handler should be called for each result status
    green: ~                          # no reporting
    yellow: elephpant                 # use "elephpant" handler
    red: chain                        # use "chain" handler