-
-
Notifications
You must be signed in to change notification settings - Fork 408
[4.x] Worker QoL #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[4.x] Worker QoL #1147
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| namespace App\Actions\Site; | ||
|
|
||
| use App\Actions\Worker\UpdateWorkerEnvironment; | ||
| use App\Actions\Worker\WorkerEnvironmentUpdateResult; | ||
| use App\Exceptions\SSHError; | ||
| use App\Models\Site; | ||
| use App\SiteTypes\AbstractProxiedSiteType; | ||
| use Illuminate\Support\Facades\Validator; | ||
|
|
||
| class UpdateSiteWorkerEnvironment | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $input | ||
| * | ||
| * @throws SSHError | ||
| */ | ||
| public function update(Site $site, array $input): WorkerEnvironmentUpdateResult | ||
| { | ||
| $type = $site->type(); | ||
| $worker = $type instanceof AbstractProxiedSiteType ? $type->bootstrapWorker() : null; | ||
|
|
||
| if ($worker !== null) { | ||
| return app(UpdateWorkerEnvironment::class)->update($worker, $input); | ||
| } | ||
|
|
||
| $validated = Validator::make($input, UpdateWorkerEnvironment::rules())->validate(); | ||
|
|
||
| $site->worker_environment = UpdateWorkerEnvironment::processVariables( | ||
| $validated['variables'], | ||
| $site->worker_environment, | ||
| ); | ||
| $site->save(); | ||
|
|
||
| return WorkerEnvironmentUpdateResult::PreFirstDeploy; | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace App\Actions\Worker; | ||
|
|
||
| use App\Enums\WorkerStatus; | ||
| use App\Jobs\Worker\RestartAllJob; | ||
| use App\Models\Server; | ||
| use App\Models\Site; | ||
| use App\Models\Worker; | ||
|
|
||
| class RestartAllWorkers | ||
| { | ||
| public function restart(Server $server, ?Site $site = null): void | ||
| { | ||
| $server->workers() | ||
| ->when($site, fn ($query) => $query->where('site_id', $site->id)) | ||
| ->whereNotIn('status', [WorkerStatus::CREATING, WorkerStatus::DELETING]) | ||
| ->get() | ||
| ->each(function (Worker $worker): void { | ||
| $worker->status = WorkerStatus::RESTARTING; | ||
| $worker->error = null; | ||
| $worker->save(); | ||
| }); | ||
|
|
||
| dispatch(new RestartAllJob($server, $site))->onQueue('ssh'); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| <?php | ||
|
|
||
| namespace App\Actions\Worker; | ||
|
|
||
| use App\Actions\Site\BroadcastSiteUpdate; | ||
| use App\Enums\WorkerStatus; | ||
| use App\Models\Server; | ||
| use App\Models\Service; | ||
| use App\Models\Site; | ||
| use App\Models\Worker; | ||
| use App\Services\ProcessManager\ProcessManager; | ||
| use App\Traits\HandlesWorkerFailure; | ||
|
|
||
| class SyncWorkerStatuses | ||
| { | ||
| use HandlesWorkerFailure; | ||
|
|
||
| private const LOG_TYPE = 'sync-worker-statuses-failed'; | ||
|
|
||
| public function sync(Server $server, ?Site $site = null): int | ||
| { | ||
| /** @var Service $service */ | ||
| $service = $server->processManager(); | ||
| /** @var ProcessManager $handler */ | ||
| $handler = $service->handler(); | ||
|
|
||
| $workers = $server->workers() | ||
| ->when($site, fn ($query) => $query->where('site_id', $site->id)) | ||
| ->whereNotIn('status', [WorkerStatus::CREATING, WorkerStatus::DELETING]) | ||
| ->get(); | ||
|
|
||
| if ($workers->isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| $statuses = $handler->statuses(); | ||
|
|
||
| $changed = $workers->filter(fn (Worker $worker): bool => $this->settle($worker, $statuses[$worker->id] ?? [])); | ||
|
|
||
| $changed->loadMissing('site') | ||
| ->pluck('site') | ||
| ->filter() | ||
| ->unique('id') | ||
| ->each(fn (Site $workerSite) => app(BroadcastSiteUpdate::class)->broadcast($workerSite)); | ||
|
|
||
| return $changed->count(); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array{state: string, description: string}> $processes | ||
| */ | ||
| private function settle(Worker $worker, array $processes): bool | ||
| { | ||
| [$status, $error] = $this->target($processes); | ||
|
|
||
| if ($worker->status === $status && $worker->error === $error) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($status === WorkerStatus::FAILED) { | ||
| $this->failWorker($worker, $error, self::LOG_TYPE, (string) $error); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| $worker->status = $status; | ||
| $worker->error = null; | ||
| $worker->save(); | ||
|
|
||
| $this->broadcastWorkerUpdate($worker); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array{state: string, description: string}> $processes | ||
| * @return array{0: WorkerStatus, 1: ?string} | ||
| */ | ||
| private function target(array $processes): array | ||
| { | ||
| if ($processes === []) { | ||
| return [WorkerStatus::FAILED, 'Process not found in supervisor']; | ||
| } | ||
|
|
||
| $worst = WorkerStatus::RUNNING; | ||
| $errors = []; | ||
|
|
||
| foreach ($processes as $process => $info) { | ||
| $status = $this->mapState($info['state']); | ||
|
|
||
| if ($status === WorkerStatus::FAILED) { | ||
| $errors[] = trim("{$process}: {$info['state']} {$info['description']}"); | ||
| } | ||
|
|
||
| if ($this->severity($status) > $this->severity($worst)) { | ||
| $worst = $status; | ||
| } | ||
| } | ||
|
|
||
| if ($worst === WorkerStatus::FAILED) { | ||
| return [WorkerStatus::FAILED, mb_substr(implode("\n", $errors), 0, 500)]; | ||
| } | ||
|
|
||
| return [$worst, null]; | ||
| } | ||
|
|
||
| private function mapState(string $state): WorkerStatus | ||
| { | ||
| return match ($state) { | ||
| 'RUNNING' => WorkerStatus::RUNNING, | ||
| 'STARTING' => WorkerStatus::STARTING, | ||
| 'STOPPING' => WorkerStatus::STOPPING, | ||
| 'STOPPED', 'EXITED' => WorkerStatus::STOPPED, | ||
| default => WorkerStatus::FAILED, | ||
| }; | ||
| } | ||
|
|
||
| private function severity(WorkerStatus $status): int | ||
| { | ||
| return match ($status) { | ||
| WorkerStatus::FAILED => 4, | ||
| WorkerStatus::STOPPED => 3, | ||
| WorkerStatus::STOPPING => 2, | ||
| WorkerStatus::STARTING => 1, | ||
| default => 0, | ||
| }; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| namespace App\Actions\Worker; | ||
|
|
||
| use App\Exceptions\SSHError; | ||
| use App\Helpers\EnvParser; | ||
| use App\Models\Worker; | ||
| use App\Services\ProcessManager\ProcessManager; | ||
| use Illuminate\Support\Facades\Validator; | ||
| use Illuminate\Validation\ValidationException; | ||
|
|
||
| class UpdateWorkerEnvironment | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $input | ||
| * | ||
| * @throws SSHError | ||
| * @throws ValidationException | ||
| */ | ||
| public function update(Worker $worker, array $input): WorkerEnvironmentUpdateResult | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| { | ||
| $validated = Validator::make($input, [ | ||
| ...self::rules(), | ||
| 'restart' => ['sometimes', 'boolean'], | ||
| ])->validate(); | ||
|
|
||
| $worker->environment = self::processVariables($validated['variables'], $worker->environment); | ||
| $worker->save(); | ||
|
|
||
| /** @var ProcessManager $processManager */ | ||
| $processManager = $worker->server->processManager()->handler(); | ||
| $processManager->writeConfig($worker); | ||
|
|
||
| if ($validated['restart'] ?? false) { | ||
| app(ManageWorker::class)->restart($worker); | ||
|
|
||
| return WorkerEnvironmentUpdateResult::Restarting; | ||
| } | ||
|
|
||
| return WorkerEnvironmentUpdateResult::PendingRestart; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array<int, string>> | ||
| */ | ||
| public static function rules(string $attribute = 'variables'): array | ||
| { | ||
| return [ | ||
| $attribute => ['present', 'array', 'max:100'], | ||
| ...self::nestedRules($attribute), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array<int, string>> | ||
| */ | ||
| public static function nestedRules(string $attribute = 'variables'): array | ||
| { | ||
| return [ | ||
| "{$attribute}.*.key" => ['required', 'string', 'max:255', 'regex:/^[A-Za-z_][A-Za-z0-9_]*$/', 'distinct'], | ||
| "{$attribute}.*.value" => ['present', 'nullable', 'string', 'max:10000', 'regex:/\A[^\x00-\x1F\x7F"]*\z/'], | ||
| "{$attribute}.*.is_secret" => ['required', 'boolean'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array<string, mixed>> $incoming | ||
| * @param ?array<int, array{key: string, value: string, is_secret: bool}> $stored | ||
| * @return array<int, array{key: string, value: string, is_secret: bool}> | ||
| */ | ||
| public static function processVariables(array $incoming, ?array $stored): array | ||
| { | ||
| $normalized = array_map(fn (array $variable): array => [ | ||
| 'key' => (string) $variable['key'], | ||
| 'value' => (string) ($variable['value'] ?? ''), | ||
| 'is_secret' => (bool) ($variable['is_secret'] ?? false), | ||
| ], $incoming); | ||
|
|
||
| return EnvParser::mergeWithStored($normalized, $stored); | ||
| } | ||
| } | ||
|
|
||
| enum WorkerEnvironmentUpdateResult | ||
| { | ||
| case PreFirstDeploy; | ||
| case PendingRestart; | ||
| case Restarting; | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.