84 lines
2.2 KiB
Markdown
84 lines
2.2 KiB
Markdown
``` bash
|
|
php artisan make:command CheckStatusCommand && php artisan make:notification HorizonIsInactiveNotification
|
|
```
|
|
|
|
``` php CheckStatusCommand
|
|
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Notifications\HorizonIsInactiveNotification;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
|
|
|
|
class CheckStatusCommand extends Command
|
|
{
|
|
protected $description = 'Command description';
|
|
protected $signature = 'horizon:check-status';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
if (!$this->shouldBeActive()) {
|
|
return;
|
|
}
|
|
|
|
if ($this->isHorizonActive()) {
|
|
return;
|
|
}
|
|
|
|
$this->error('Horizon is inactive on: ' . config('app.name'));
|
|
|
|
Flare::report(new Exception('Horizon is inactive'));
|
|
|
|
Notification::route('mail', 'dev@strixi.nl')
|
|
->notify(new HorizonIsInactiveNotification());
|
|
}
|
|
|
|
protected function isHorizonActive(): bool
|
|
{
|
|
if (!$masters = app(MasterSupervisorRepository::class)->all()) {
|
|
return false;
|
|
}
|
|
|
|
return collect($masters)->some(fn($master): bool => $master->status !== 'paused');
|
|
}
|
|
|
|
protected function shouldBeActive(): bool
|
|
{
|
|
return config('queue.default') === 'redis';
|
|
}
|
|
}
|
|
```
|
|
|
|
``` php HorizonIsInactiveNotification
|
|
<?php
|
|
|
|
namespace App\Notifications;
|
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
use Illuminate\Notifications\Notification;
|
|
|
|
class HorizonIsInactiveNotification extends Notification
|
|
{
|
|
public function via(object $notifiable): array
|
|
{
|
|
return ['mail'];
|
|
}
|
|
|
|
public function toMail(object $notifiable): MailMessage
|
|
{
|
|
return (new MailMessage)
|
|
->subject('Horizon is inactive on: ' . config('app.name'))
|
|
->line('Horizon is inactive on: ' . config('app.name'))
|
|
->action('Goto site', url(config('app.url')));
|
|
}
|
|
}
|
|
```
|
|
|
|
``` php kernel or bootstrap
|
|
$schedule->command(\App\Console\Commands\CheckStatusCommand::class)->everyFifteenMinutes();
|
|
``` |