|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Actions\Worker; |
| 4 | + |
| 5 | +use App\Actions\Site\BroadcastSiteUpdate; |
| 6 | +use App\Enums\WorkerStatus; |
| 7 | +use App\Models\Server; |
| 8 | +use App\Models\Service; |
| 9 | +use App\Models\Site; |
| 10 | +use App\Models\Worker; |
| 11 | +use App\Services\ProcessManager\ProcessManager; |
| 12 | +use App\Traits\HandlesWorkerFailure; |
| 13 | + |
| 14 | +class SyncWorkerStatuses |
| 15 | +{ |
| 16 | + use HandlesWorkerFailure; |
| 17 | + |
| 18 | + private const LOG_TYPE = 'sync-worker-statuses-failed'; |
| 19 | + |
| 20 | + public function sync(Server $server, ?Site $site = null): int |
| 21 | + { |
| 22 | + /** @var Service $service */ |
| 23 | + $service = $server->processManager(); |
| 24 | + /** @var ProcessManager $handler */ |
| 25 | + $handler = $service->handler(); |
| 26 | + |
| 27 | + $workers = $server->workers() |
| 28 | + ->when($site, fn ($query) => $query->where('site_id', $site->id)) |
| 29 | + ->whereNotIn('status', [WorkerStatus::CREATING, WorkerStatus::DELETING]) |
| 30 | + ->get(); |
| 31 | + |
| 32 | + if ($workers->isEmpty()) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + |
| 36 | + $statuses = $handler->statuses(); |
| 37 | + |
| 38 | + $changed = $workers->filter(fn (Worker $worker): bool => $this->settle($worker, $statuses[$worker->id] ?? [])); |
| 39 | + |
| 40 | + $changed->loadMissing('site') |
| 41 | + ->pluck('site') |
| 42 | + ->filter() |
| 43 | + ->unique('id') |
| 44 | + ->each(fn (Site $workerSite) => app(BroadcastSiteUpdate::class)->broadcast($workerSite)); |
| 45 | + |
| 46 | + return $changed->count(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param array<string, array{state: string, description: string}> $processes |
| 51 | + */ |
| 52 | + private function settle(Worker $worker, array $processes): bool |
| 53 | + { |
| 54 | + [$status, $error] = $this->target($processes); |
| 55 | + |
| 56 | + if ($worker->status === $status && $worker->error === $error) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if ($status === WorkerStatus::FAILED) { |
| 61 | + $this->failWorker($worker, $error, self::LOG_TYPE, (string) $error); |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + $worker->status = $status; |
| 67 | + $worker->error = null; |
| 68 | + $worker->save(); |
| 69 | + |
| 70 | + $this->broadcastWorkerUpdate($worker); |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array<string, array{state: string, description: string}> $processes |
| 77 | + * @return array{0: WorkerStatus, 1: ?string} |
| 78 | + */ |
| 79 | + private function target(array $processes): array |
| 80 | + { |
| 81 | + if ($processes === []) { |
| 82 | + return [WorkerStatus::FAILED, 'Process not found in supervisor']; |
| 83 | + } |
| 84 | + |
| 85 | + $worst = WorkerStatus::RUNNING; |
| 86 | + $errors = []; |
| 87 | + |
| 88 | + foreach ($processes as $process => $info) { |
| 89 | + $status = $this->mapState($info['state']); |
| 90 | + |
| 91 | + if ($status === WorkerStatus::FAILED) { |
| 92 | + $errors[] = trim("{$process}: {$info['state']} {$info['description']}"); |
| 93 | + } |
| 94 | + |
| 95 | + if ($this->severity($status) > $this->severity($worst)) { |
| 96 | + $worst = $status; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if ($worst === WorkerStatus::FAILED) { |
| 101 | + return [WorkerStatus::FAILED, mb_substr(implode("\n", $errors), 0, 500)]; |
| 102 | + } |
| 103 | + |
| 104 | + return [$worst, null]; |
| 105 | + } |
| 106 | + |
| 107 | + private function mapState(string $state): WorkerStatus |
| 108 | + { |
| 109 | + return match ($state) { |
| 110 | + 'RUNNING' => WorkerStatus::RUNNING, |
| 111 | + 'STARTING' => WorkerStatus::STARTING, |
| 112 | + 'STOPPING' => WorkerStatus::STOPPING, |
| 113 | + 'STOPPED', 'EXITED' => WorkerStatus::STOPPED, |
| 114 | + default => WorkerStatus::FAILED, |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + private function severity(WorkerStatus $status): int |
| 119 | + { |
| 120 | + return match ($status) { |
| 121 | + WorkerStatus::FAILED => 4, |
| 122 | + WorkerStatus::STOPPED => 3, |
| 123 | + WorkerStatus::STOPPING => 2, |
| 124 | + WorkerStatus::STARTING => 1, |
| 125 | + default => 0, |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
0 commit comments