Init
This commit is contained in:
92
Career/Others/Horizon.md
Normal file
92
Career/Others/Horizon.md
Normal file
@@ -0,0 +1,92 @@
|
||||
---
|
||||
created: 2025-03-13 14:23
|
||||
updated: 2025-07-04 07:23
|
||||
tags:
|
||||
- Laravel/Packages
|
||||
---
|
||||
|
||||
``` 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();
|
||||
```
|
||||
Reference in New Issue
Block a user