oro/health-check-bundle

Oro Health Check Bundle


README

OroHealthCheckBundle 实现了一组针对基于 OroPlatform 的应用程序的健康检查。基于 Liip Monitor Bundle,它提供了使用与应用程序相同的配置和环境执行检查的方法。

目录

目的

使用 OroHealthCheckBundle,您可以通过 Web UI、API 和 CLI 了解环境和配置健康检查结果。它帮助您确保应用程序环境配置正确,与应用程序集成的外部服务处于活动状态且可以从应用程序访问。

它检查以下内容

  • 数据库服务器连接
  • Elasticsearch 服务器连接
  • 文件存储状态
  • 邮件传输连接
  • RabbitMQ 服务器连接
  • Redis 服务器连接
  • WebSocket 服务器连接
  • 维护模式状态

基本用法

您可以通过以下方式使用健康检查

  • CLI。有两个可用的命令

    • bin/console monitor:list --env=prod 命令提供配置的检查列表
    $ bin/console monitor:list --env=prod
    
    doctrine_dbal Check if Doctrine DBAL is available
    mail_transport Check if Mail Transport is available
    rabbitmq_server Check if RabbitMQ is available in case it is configured
    elasticsearch Check if Elasticsearch is available in case it is configured
    websocket Check if WebSocket server is available
    maintenance_mode Check if Maintenance Mode is running and not expired
    fs_cache_prod Check if "/var/www/var/cache/prod" is writable
    fs_logs Check if "/var/www/var/logs" is writable
    fs_var_data Check if "/var/www/var/data" is writable
    fs_web_media Check if "/var/www/public/media" is writable
    redis_cache Check if Redis cache is available
    redis_doctrine_cache Check if Redis doctrine cache is available
    redis_session_storage Check if Redis session storage is available
    • bin/console monitor:health --env=prod 命令执行健康检查
    $ bin/console monitor:health --env=prod
    
    OK Check if Doctrine DBAL is available
    OK Check if Mail Transport is available
    SKIP Check if RabbitMQ is available in case it is configured: RabbitMQ connection is not configured. Check Skipped.
    OK Check if Elasticsearch is available in case it is configured
    FAIL Check if WebSocket server is available: Not available
    FAIL Check if Maintenance Mode is running and not expired: Expired
    OK Check if "/var/www/var/cache/prod" is writable: The path is a writable directory.
    OK Check if "/var/www/var/logs" is writable: The path is a writable directory.
    OK Check if "/var/www/var/data" is writable: The path is a writable directory.
    OK Check if "/var/www/public/media" is writable: The path is a writable directory.
    OK Check if Redis cache is available
    OK Check if Redis doctrine cache is available
    OK Check if Redis session storage is available

如果所有健康检查都成功,则 bin/console monitor:health --env=prod 命令返回 0 代码。如果至少有一个检查失败,则返回 1 代码。

  • Web 界面。所有配置的检查和 REST API 文档都可在 /healthcheck 路径的页面中找到
  • HTTP 状态端点。仅发送 HTTP 状态的响应页面
    • 可以使用 /healthcheck/http_status_checks 获取所有可用检查执行后的状态
    • 可以使用 /healthcheck/http_status_check/<some_check_id> 获取特定检查的状态(使用 bin/console monitor:list --env=prod 获取检查标识符)
  • REST API。文档可在 /healthcheck 路径的页面中找到

注意:对于 OroCommerce 应用程序,请确保在使用健康检查 URL 之前使用了 %web_backend_prefix% 参数。该参数默认值为 /admin。例如

  • /admin/healthcheck
  • /admin/healthcheck/http_status_check/<some_check_id>
  • /admin/healthcheck/http_status_checks

维护模式下的 HealthCheck

请注意,如果您的 Web 服务器配置了 维护 页面,则您将无法使用任何 http 请求到您的 Web 服务器。在这种情况下,您只能使用 CLI 命令,如 基本用法 部分中所示。

当使用健康检查时,您通常会收到 200 或 502 HTTP 状态代码。但是,如果您具有以下列出的任何配置,您将收到 503 HTTP 状态代码。

对于 Apache Web 服务器(在 .htaccess 文件中)

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Maintenance mode rewrites
    RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
    RewriteCond %{DOCUMENT_ROOT}/../var/maintenance/maintenance_lock -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /maintenance.html [R=503,L]
    ErrorDocument 503 /maintenance.html
</IfModule>

对于 Nginx Web 服务器(在主机配置中)

server {
    location / {
        if (-f /var/www/var/maintenance/maintenance_lock) {
            return 503;
        }
    }

    # Error pages
    error_page 503 /maintenance.html;
    location = /maintenance.html {
        root /var/www/;
    }
}

内置检查

数据库服务器连接

通过应用程序配置验证到数据库的连接。

Elasticsearch 服务器连接

验证 Elasticsearch 服务器是否可访问,并且可能通过应用程序功能连接。

文件存储状态

验证特定目录是否可写

  • 缓存
  • 日志
  • 数据
  • 媒体

邮件传输连接

验证邮件传输是否正确配置且可访问。

RabbitMQ 服务器连接

通过应用配置验证与RabbitMQ服务器的连接。

Redis 服务器连接

通过应用配置验证与Redis服务器的连接。

WebSocket 服务器连接

验证服务是否配置正确且正在运行。

维护模式状态

使用OroHealthCheckBundle,维护模式将经历以下变更

维护模式下的健康检查

该包配置将/healthcheck/http_status_checks路径保留为API调用的端点。如果所有健康检查都成功,则/healthcheck/http_status_checks请求返回200响应代码。如果至少有一个检查失败,则返回502响应代码。/healthcheck/http_status_check/<some_check_id>路径用于单个检查,响应代码相同。

  • 使用Oro\Bundle\MaintenanceBundle\Drivers\FileDriver类作为维护驱动器。

以下示例说明了可以在config.yml中使用的配置,以更改维护模式的行为

oro_health_check:
    maintenance_driver:
        options:
            file_path: %kernel.project_dir%/var/maintenance/maintenance_lock

构建自己的检查

每个健康检查类必须实现Laminas\Diagnostics\Check\CheckInterface接口。

<?php

namespace Oro\Bundle\HealthCheckBundle\Check;

use Laminas\Diagnostics\Check\CheckInterface;
use Laminas\Diagnostics\Result\ResultInterface;
use Laminas\Diagnostics\Result\Success;
use Laminas\Diagnostics\Result\Failure;

class CustomCheck implements CheckInterface
{
    /**
     * {@inheritdoc}
     */
    public function check(): ResultInterface
    {
        $result = <result of some check>;
        
        return $result ? new Success() : new Failure();
    }

    /**
     * {@inheritdoc}
     */
    public function getLabel(): string
    {
        return 'Custom check verifies ...';
    }
}

该类必须定义为Symfony服务,并标记为liip_monitor.check,以便由健康检查运行器捕获。

oro_health_check.check.custom:
    class: Oro\Bundle\HealthCheckBundle\Check\CustomCheck
    tags:
        - { name: liip_monitor.check, alias: custom }

或者,如果您需要在单个服务中运行各种检查,实现Laminas\Diagnostics\Check\CheckCollectionInterface接口。该接口的getChecks()方法返回需要执行的健康检查数组。

<?php

namespace Oro\Bundle\HealthCheckBundle\Check;

use Laminas\Diagnostics\Check\CheckCollectionInterface;

class CustomCheckCollection implements CheckCollectionInterface
{
    /**
     * {@inheritdoc}
     */
    public function getChecks(): array
    {
        return [new CustomCheck()];
    }
}

然后,将其标记为liip_monitor.check_collection

oro_health_check.check.custom_collection:
    class: Oro\Bundle\HealthCheckBundle\Check\CustomCheckCollection
    tags:
        - { name: liip_monitor.check_collection, alias: custom_collection }

资源