Created
November 25, 2024 21:46
-
-
Save devhammed/8f7862131893c2b4e14619901e7c822f to your computer and use it in GitHub Desktop.
Laravel Health Diagnosing Listener - https://laravel.com/docs/11.x/deployment#the-health-route
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Listeners; | |
use Exception; | |
use Throwable; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Cache; | |
use Illuminate\Support\Facades\Redis; | |
use Illuminate\Support\Facades\Process; | |
use Illuminate\Foundation\Events\DiagnosingHealth; | |
class DiagnosingHealthListener | |
{ | |
public function __construct() | |
{ | |
// | |
} | |
public function handle(DiagnosingHealth $event): void | |
{ | |
$this->checkFilesystemPermissions(); | |
$this->checkDiskUsage(); | |
$this->checkDatabaseConnection(); | |
$this->checkRedisConnection(); | |
$this->checkCacheConnection(); | |
$this->checkQueueConnection(); | |
$this->checkSchedulerConnection(); | |
$this->checkMailerConnection(); | |
$this->checkWebsocketConnection(); | |
$this->checkOutboundConnection(); | |
} | |
protected function checkFilesystemPermissions(): void | |
{ | |
$paths = [ | |
base_path('bootstrap/cache'), | |
storage_path('app'), | |
storage_path('app/livewire-tmp'), | |
storage_path('app/public'), | |
storage_path('framework'), | |
storage_path('framework/cache'), | |
storage_path('framework/cache/data'), | |
storage_path('framework/sessions'), | |
storage_path('framework/testing'), | |
storage_path('framework/views'), | |
storage_path('logs'), | |
storage_path('logs/laravel.log'), | |
]; | |
foreach ($paths as $path) { | |
if ( ! is_writable($path)) { | |
throw new Exception('Filesystem permissions are incorrect for '.$path, 503); | |
} | |
} | |
} | |
protected function checkDiskUsage(): void | |
{ | |
$diskFreeSpace = disk_free_space('/'); | |
$diskTotalSpace = disk_total_space('/'); | |
$diskUsagePercentage = ($diskTotalSpace - $diskFreeSpace) / $diskTotalSpace * 100; | |
if ($diskUsagePercentage > 90) { | |
throw new Exception('Disk usage is above 90%.', 503); | |
} | |
} | |
protected function checkDatabaseConnection(): void | |
{ | |
try { | |
DB::connection()->getPdo(); | |
} catch (Throwable $e) { | |
throw new Exception('Database connection failed.', 503, $e); | |
} | |
} | |
protected function checkRedisConnection(): void | |
{ | |
try { | |
Redis::ping('health_check'); | |
} catch (Throwable $e) { | |
throw new Exception('Redis connection failed.', 503, $e); | |
} | |
} | |
protected function checkCacheConnection(): void | |
{ | |
try { | |
Cache::get('health_check'); | |
} catch (Throwable $e) { | |
throw new Exception('Cache connection failed.', 503, $e); | |
} | |
} | |
protected function checkQueueConnection(): void | |
{ | |
$process = Process::run(['pgrep', '-f', 'artisan queue:work']); | |
if ($process->successful() && $process->output()) { | |
return; | |
} | |
throw new Exception('Queue is not running.', 503); | |
} | |
protected function checkSchedulerConnection(): void | |
{ | |
$process = Process::run(['pgrep', '-f', 'artisan schedule:work']); | |
if ($process->successful() && $process->output()) { | |
return; | |
} | |
throw new Exception('Scheduler is not running.', 503); | |
} | |
protected function checkMailerConnection(): void | |
{ | |
try { | |
$transport = app('mailer')->getSymfonyTransport(); | |
$transport->start(); | |
$transport->stop(); | |
} catch (Throwable $e) { | |
throw new Exception('Mailer connection failed.', 503, $e); | |
} | |
} | |
protected function checkWebsocketConnection(): void | |
{ | |
$process = Process::run(['pgrep', '-f', 'artisan reverb:start']); | |
if ($process->successful() && $process->output()) { | |
return; | |
} | |
throw new Exception('Websocket is not running.', 503); | |
} | |
protected function checkOutboundConnection(): void | |
{ | |
$process = Process::run(['curl', '-s', 'https://icanhazip.com']); | |
if ($process->successful() && $process->output()) { | |
return; | |
} | |
throw new Exception('Outbound connection failed.', 503); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment