devolicious/oh-dear-app-health-bundle

一个简单的 Symfony 扩展包,用于集成 OhDear 应用程序健康监控

1.0.1 2023-12-04 13:22 UTC

This package is auto-updated.

Last update: 2024-09-07 16:16:23 UTC


README

一个简单的 Symfony 扩展包,用于集成 OhDear 应用程序健康监控。

安装

步骤 1: 下载扩展包

composer require devolicious/oh-dear-app-health-bundle

如果你使用 Symfony Flex,扩展包将被自动启用。否则,通过将其添加到你的 bundles.php 文件中启用它。

// bundles.php

return [
    // ...
    Devolicious\OhDearAppHealthBundle\OhDearAppHealthBundle::class => ['all' => true],
];

步骤 2: 配置路由

将以下内容添加到你的 config/routes.yaml 文件中

_health_check:
    resource: '@OhDearAppHealthBundle/Resources/config/routing.yaml'

或者,如果你想自定义路由,只需在 config/routes/ 中添加一个包含以下内容的自定义路由 yaml 文件

my_custom_health_check:
    path: /my-custom-health-check-route
    controller: Devolicious\OhDearAppHealthBundle\Controller\HealthController
    methods: [GET, HEAD]

如何使用

通过实现 Devolicious\OhDearAppHealthBundle\Checker\CheckerInterface 创建自己的检查器,并使用 oh_dear_app_health.checker 标记它们

# config/services.yaml
services:

    _instanceof:
        ...
        Devolicious\OhDearAppHealthBundle\Checker\CheckerInterface:
          tags: [ 'oh_dear_app_health.checker' ]

就这样!当调用健康检查路由时,你的检查器将被自动执行。

但是,你还可以缓存检查器的结果。当你运行 health:check 命令时,所有检查器都会运行,默认情况下将存储在默认缓存池中。你可以通过用任何实现该接口的服务覆盖 ResultStore 接口来更改此设置。

services:
    ...
    # default configuration (already configured in the bundle)
    Devolicious\OhDearAppHealthBundle\Store\RequestStore: '@Devolicious\OhDearAppHealthBundle\Store\CachePoolStore'

    # custom configuration
    Devolicious\OhDearAppHealthBundle\Store\RequestStore: '@App\Store\MyCustomStore'

在每个检查器中,你可以定义检查器应该多久执行一次(以秒为单位)的频率。

内置检查器

DoctrineDatabaseChecker

此检查器包含在库中。默认情况下它没有被启用,因为你可能不需要它,或者你正在使用不同的数据库配置。它还允许在存在多个数据库连接时重用检查器。假设你已经添加了上述描述的 _instanceof 配置,你可以将以下内容添加到你的 services.yaml 文件中

# config/services.yaml
services:
    ...
    # Example for simple setup with 1 database connection and autowire enabled
    Devolicious\OhDearAppHealthBundle\Checker\DoctrineConnectionChecker: ~

如果你有多个数据库连接,可以将以下内容添加到你的 services.yaml 文件中

# config/services.yaml
services:
    ...
    # Example for default connection with MySQL
    app.mysql_database_checker:
        class: Devolicious\OhDearAppHealthBundle\Checker\DoctrineConnectionChecker
        arguments:
            - '@doctrine.orm.default_entity_manager'
            - 'MySQL Database' #optional, defaults to 'Database'

    # Example for an extra connection with a MSSQL database
    app.mssql_database_checker:
        class: Devolicious\OhDearAppHealthBundle\Checker\DoctrineConnectionChecker
        arguments:
            - '@doctrine.orm.mssql_entity_manager'
            - 'MSSQL Database' #optional, defaults to 'Database'