code-orange/statuspage

一个Lumen包,使您能够轻松地将状态页添加到现有应用程序或创建一个专门的状态页应用程序。

v2.0 2021-02-04 10:15 UTC

This package is auto-updated.

Last update: 2024-09-04 17:56:23 UTC


README

Statuspage是一个Lumen包,允许您轻松地将状态页添加到现有的Lumen应用程序或创建一个专门的状态页Lumen应用程序。

Demo 状态页的实时演示。此安装是一个用于监视更大Laravel应用程序的专用Lumen应用程序。

安装

composer require code-orange/statuspage

配置

首先,在您的bootstrap/app.php中注册Statuspage的ServiceProvider

$app->register(CodeOrange\Statuspage\StatuspageProvider::class);

默认情况下,Statuspage将在/处注册状态页,并在/json处注册其JSON版本。如果您想覆盖此行为,可以设置STATUSPAGE_ROUTE环境变量。如果您的路由不以斜线结尾,JSON版本将托管在/{$route}.json

Statuspage附带一个默认的HTML页面,用于显示所有已注册检查的状态。如果您想修改此页面,应将视图(从vendor/code-orange/statuspage/src/views/)复制到您自己的视图文件夹。然后,您可以使用STATUSPAGE_VIEW设置Statuspage要渲染的视图。

对于状态页的标题,Statuspage通常会使用您的APP_NAMEconfig('app.name')后跟单词'status'。如果您想使用不同的内容,可以设置STATUSPAGE_HEADER环境变量。如果设置了此变量,页面标题将填充其内容而不是应用程序名称。环境变量可以包含HTML。

所有环境变量都是可选的。

#STATUSPAGE_ROUTE=/
#STATUSPAGE_VIEW=status
#STATUSPAGE_HEADER="<img src='https://odysseyattribution.co/logo.svg' style='height: 1.5em; vertical-align: middle;'> status"

用法

注册检查

Statuspage通过将StatusChecks(单独或分组)与Statuspage注册来工作。我们建议在AppServiceProviderboot方法中这样做。

Statuspage公开了两个相关的方法。

  • registerCheck($label, $check)允许您将带有标签的单个检查注册到状态页。
  • registerSection($label, ['Label' => $check, 'Label 2' => $check2])允许您将带有标题和多个检查的分区注册到状态页。

例如

class AppServiceProvider extends ServiceProvider {
    public function boot(Statuspage $statuspage) {
        $statuspage->registerCheck('Code Orange', new Http200Check('https://code-orange.nl'));
        
        $statuspage->registerSection('Google', [
            'Google 200' => new Http200Check('https://www.google.nl'),
            'Google without www 200 (should fail)' => new Http200Check('https://google.nl'),
            'Google without www 301' => new Http200Check('https://google.nl', 301)
        ]);
        
        $statuspage->registerSection('Database', [
            'Connection' => new DatabaseConnectionCheck(),
            'Non-existing connection' => new DatabaseConnectionCheck('asdf')
        ]);
        
        $statuspage->registerCheck('Dummy succesful', new DummyCheck(Status::$OPERATIONAL));
        
        $statuspage->registerSection('Color check', [
            'Operational' => new DummyCheck(Status::$OPERATIONAL),
            'Maintenance' => new DummyCheck(Status::$MAINTENANCE),
            'Degraded' => new DummyCheck(Status::$DEGRADED),
            'Partial outage' => new DummyCheck(Status::$PARTIAL_OUTAGE),
            'Major outage' => new DummyCheck(Status::$MAJOR_OUTAGE),
        ]);
    }
}

编写自己的检查

虽然Statuspage附带了一些基本但有用的检查,但真正的力量在于您可以轻松定义自己特定的自定义检查。

要这样做,只需从CodeOrange\Statuspage\Checks\StatusCheck开始子类化。检查通常在其构造函数中接受一些参数并在其performCheck方法被调用时执行。检查始终返回CodeOrange\Statuspage\Status的一个实例,可选地带有消息。

后台检查

默认情况下,Statuspage在请求页面时执行所有检查。虽然这在小规模或开发环境中是可行的,但在生产环境中可能不是您想要的。

要配置Statuspage以在后台执行注册的检查,请将以下行添加到您的Kernel的调度函数中

class Kernel extends ConsoleKernel {
    protected function schedule(Schedule $schedule) {
        app(Statuspage::class)->scheduleBackgroundExecution($schedule);
    }
}

注册的检查将每分钟执行一次(确保php artisan schedule:run 已正确配置在您的crontab中)。

检查的结果存储在默认的缓存存储中(键为statuspage_status)。

已知问题

  • 该框架(尚)不处理长时间运行的检查。您负责管理检查的运行时间和设置超时。