spatie / laravel-failed-job-monitor
当队列中的作业失败时接收通知
Requires
- php: ^8.0
- laravel/framework: ^7.0|^8.0|^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.6
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^5.0|^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.34
Suggests
- guzzlehttp/guzzle: Allows notifications to be sent to Slack
- laravel/slack-notification-channel: Required for sending notifications via Slack
README
此包在作业失败时发送通知。默认情况下,它可以通过邮件和/或Slack发送通知。它利用Laravel的原生通知系统。
支持我们
我们投入了大量资源来创建 最佳开源包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感激您从家乡寄给我们明信片,注明您正在使用我们哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将发布所有收到的明信片在我们的 虚拟明信片墙 上。
安装
对于5.8和6.x版本的Laravel,请使用此包的v3.x版本。
您可以通过composer安装此包
composer require spatie/laravel-failed-job-monitor
如果您打算使用Slack通知,也应安装guzzle客户端
composer require guzzlehttp/guzzle
服务提供程序将自动注册。
接下来,您必须发布配置文件
php artisan vendor:publish --tag=failed-job-monitor-config
这是默认配置文件的正文。在这里,您可以指定应发送通知的通知接收者。默认通知接收者将使用此配置文件中指定的变量。
return [ /** * The notification that will be sent when a job fails. */ 'notification' => \Spatie\FailedJobMonitor\Notification::class, /** * The notifiable to which the notification will be sent. The default * notifiable will use the mail and slack configuration specified * in this config file. */ 'notifiable' => \Spatie\FailedJobMonitor\Notifiable::class, /* * By default notifications are sent for all failures. You can pass a callable to filter * out certain notifications. The given callable will receive the notification. If the callable * return false, the notification will not be sent. */ 'notificationFilter' => null, /** * The channels to which the notification will be sent. */ 'channels' => ['mail', 'slack'], 'mail' => [ 'to' => ['email@example.com'], ], 'slack' => [ 'webhook_url' => env('FAILED_JOB_SLACK_WEBHOOK_URL'), ], ];
配置
自定义通知
此包提供的默认通知类支持邮件和Slack。
如果您想自定义通知,可以在配置文件中指定您自己的通知类。
// config/failed-job-monitor.php return [ ... 'notification' => \App\Notifications\CustomNotificationForFailedJobMonitor::class, ...
自定义通知接收者
此包提供的默认通知接收者类使用 channels
、mail
和 slack
键从 config
文件中确定通知的发送方式
如果您想自定义通知接收者,可以在配置文件中指定您自己的通知接收者类。
// config/failed-job-monitor.php return [ 'notifiable' => \App\CustomNotifiableForFailedJobMonitor::class, ...
过滤通知
要过滤通知,将闭包传递给 notificationFilter
。
// config/failed-job-monitor.php return [ ... 'notificationFilter' => function (Spatie\FailedJobMonitor\Notification $notification): bool { return true; } ...
上述方法在Laravel不支持闭包序列化时才有效。因此,当您运行 php artisan config:cache
时,您将得到以下错误
LogicException : Your configuration files are not serializable.
因此,最好创建一个单独的类,并使用可调用作为回调。
<?php namespace App\Notifications; use Spatie\FailedJobMonitor\Notification; class FailedJobNotification { public static function notificationFilter(Notification $notification): bool { return true; } }
并在配置文件中引用它。
// config/failed-job-monitor.php return [ ... 'notificationFilter' => [App\Notifications\FailedJobNotification::class, 'notificationFilter'], ...
用法
如果您正确配置了该包,那么您就完成了。当队列中的作业失败时,您将收到通知。
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
测试
composer test
贡献
请参阅 CONTRIBUTING 以获取详细信息。
安全
如果您发现了关于安全性的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
鸣谢
衷心感谢 Egor Talantsev 在创建包的 v2
版本时提供的帮助。
许可证
MIT许可证。请参阅许可文件以获取更多信息。