actengage/night-watch

NightWatch服务的官方Laravel包。

v0.5.0 2022-06-10 15:38 UTC

This package is auto-updated.

Last update: 2024-09-17 21:46:54 UTC


README

PHP Composer

什么是NightWatch?

NightWatch是一个监控URL的包,当您不在或睡觉时。NightWatch使用远程Node服务器来检查远程URL的状态。例如,NightWatch可以检查404 URL,或者Google Tag Manager是否已正确安装。

此包包含什么?

此包具有自己的工厂、迁移、模型和调度,以便轻松且可配置地跟踪远程URL和服务状态。

安装

composer require actengage/night-watch

php artisan vendor:publish

配置ENV

这是一个私有应用程序,因此API端点目前未包含在内。请将端点包含在您的ENV文件中。

NIGHTWATCH_ENDPOINT_URI='https://the.night.watch.url.goes.here'

内核调度

NightWatch使得安排命令变得简单。一旦您运行了迁移,将以下单行代码添加到app/Console/Kernel.php

<?php

namespace App\Console;

use Actengage\NightWatch\Watcher;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        Watcher::schedule($schedule);
    }
}

基本示例

手动启动一个监视器,每小时检查Google的主页。

use \Actengage\NightWatch\Watcher;

Watcher::create([
    'url' => 'https://google.com',
    'schedule' => ['hourly']
]);

使用Watchable特性

您可以轻松地将监视器与您的模型相关联。

use Actengage\NightWatch\Watchable;
use Actengage\NightWatch\Watcher;
use Illuminate\Database\Eloquent\Model;

class Url extends Model {
    use Watchable;
    
    protected $fillable = ['url'];

    public static function boot()
    {
        parent::boot();

        static::created(function($model) {
            $watcher = Watcher::create([
                'url' => $model->url
            ]);

            // This is a many to many relationship provided by the trait.
            $model->watchers()->attach($watcher);
        });
    }
}