95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Enterprise\Services;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Throwable;
|
|
|
|
final class SystemHealthService
|
|
{
|
|
public function run(): array
|
|
{
|
|
$checks = [
|
|
$this->database(),
|
|
$this->cache(),
|
|
$this->storage(),
|
|
$this->queue(),
|
|
$this->environment(),
|
|
];
|
|
|
|
return [
|
|
'status' => collect($checks)->contains(fn (array $check): bool => $check['status'] === 'failed') ? 'failed' : (collect($checks)->contains(fn (array $check): bool => $check['status'] === 'warning') ? 'warning' : 'healthy'),
|
|
'checks' => $checks,
|
|
'generated_at' => now()->toIso8601String(),
|
|
];
|
|
}
|
|
|
|
private function database(): array
|
|
{
|
|
try {
|
|
DB::connection()->getPdo();
|
|
|
|
return $this->result('Database', 'healthy', DB::connection()->getDriverName());
|
|
} catch (Throwable $e) {
|
|
return $this->result('Database', 'failed', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function cache(): array
|
|
{
|
|
try {
|
|
$key = 'enterprise-health-'.bin2hex(random_bytes(6));
|
|
Cache::put($key, 'ok', 10);
|
|
$healthy = Cache::get($key) === 'ok';
|
|
Cache::forget($key);
|
|
|
|
return $this->result('Cache', $healthy ? 'healthy' : 'failed', config('cache.default'));
|
|
} catch (Throwable $e) {
|
|
return $this->result('Cache', 'failed', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function storage(): array
|
|
{
|
|
try {
|
|
Storage::disk('local')->put('enterprise-health.txt', now()->toIso8601String());
|
|
Storage::disk('local')->delete('enterprise-health.txt');
|
|
$freeMb = (int) floor(disk_free_space(storage_path()) / 1024 / 1024);
|
|
$minimum = (int) config('enterprise.health.minimum_free_disk_mb', 512);
|
|
|
|
return $this->result('Storage', $freeMb < $minimum ? 'warning' : 'healthy', $freeMb.' MB free');
|
|
} catch (Throwable $e) {
|
|
return $this->result('Storage', 'failed', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function queue(): array
|
|
{
|
|
$failed = Schema::hasTable('failed_jobs') ? DB::table('failed_jobs')->count() : 0;
|
|
$maximum = (int) config('enterprise.health.maximum_failed_jobs', 0);
|
|
|
|
return $this->result('Queue', $failed > $maximum ? 'warning' : 'healthy', $failed.' failed jobs');
|
|
}
|
|
|
|
private function environment(): array
|
|
{
|
|
$warnings = [];
|
|
if (app()->environment('production') && config('app.debug')) {
|
|
$warnings[] = 'APP_DEBUG is enabled';
|
|
}
|
|
if (! app()->environment('production')) {
|
|
$warnings[] = 'Not running in production';
|
|
}
|
|
|
|
return $this->result('Environment', $warnings ? 'warning' : 'healthy', $warnings ? implode('; ', $warnings) : app()->environment());
|
|
}
|
|
|
|
private function result(string $name, string $status, string $message): array
|
|
{
|
|
return compact('name', 'status', 'message');
|
|
}
|
|
}
|