diff --git a/app/Livewire/Project/Application/Deployment/Index.php b/app/Livewire/Project/Application/Deployment/Index.php index 5b621cb95b..8729fe7a10 100644 --- a/app/Livewire/Project/Application/Deployment/Index.php +++ b/app/Livewire/Project/Application/Deployment/Index.php @@ -2,7 +2,10 @@ namespace App\Livewire\Project\Application\Deployment; +use App\Enums\ApplicationDeploymentStatus; use App\Models\Application; +use App\Models\ApplicationDeploymentQueue; +use App\Models\Server; use Illuminate\Support\Collection; use Livewire\Component; @@ -160,6 +163,71 @@ private function updateCurrentPage() $this->currentPage = intval($this->skip / $this->defaultTake) + 1; } + public function force_start($deployment_uuid) + { + try { + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', $deployment_uuid)->first(); + if ($deployment) { + force_start_deployment($deployment); + $this->loadDeployments(); + } + } catch (\Throwable $e) { + return handleError($e, $this); + } + } + + public function cancel($deployment_uuid) + { + try { + $deployment = ApplicationDeploymentQueue::where('deployment_uuid', $deployment_uuid)->first(); + if (! $deployment) { + return; + } + + $kill_command = "docker rm -f {$deployment_uuid}"; + $build_server_id = $deployment->build_server_id ?? $this->application->destination->server_id; + $server_id = $deployment->server_id ?? $this->application->destination->server_id; + + // First, mark the deployment as cancelled to prevent further processing + $deployment->update([ + 'status' => ApplicationDeploymentStatus::CANCELLED_BY_USER->value, + ]); + + if ($this->application->settings->is_build_server_enabled) { + $server = Server::ownedByCurrentTeam()->find($build_server_id); + } else { + $server = Server::ownedByCurrentTeam()->find($server_id); + } + + // Add cancellation log entry + if ($deployment->logs) { + $previous_logs = json_decode($deployment->logs, associative: true, flags: JSON_THROW_ON_ERROR); + + $new_log_entry = [ + 'command' => $kill_command, + 'output' => 'Deployment cancelled by user.', + 'type' => 'stderr', + 'timestamp' => now()->toIso8601String(), + 'hidden' => false, + 'batch' => 0, + ]; + $previous_logs[] = $new_log_entry; + $deployment->update([ + 'logs' => json_encode($previous_logs, flags: JSON_THROW_ON_ERROR), + ]); + } + + if ($server) { + instant_remote_process([$kill_command], $server, false); + } + + $this->loadDeployments(); + $this->dispatch('success', 'Deployment cancelled.'); + } catch (\Throwable $e) { + return handleError($e, $this); + } + } + public function render() { return view('livewire.project.application.deployment.index'); diff --git a/resources/views/livewire/project/application/deployment/index.blade.php b/resources/views/livewire/project/application/deployment/index.blade.php index 6d3f619adc..d87a613e37 100644 --- a/resources/views/livewire/project/application/deployment/index.blade.php +++ b/resources/views/livewire/project/application/deployment/index.blade.php @@ -173,6 +173,18 @@ class="mt-2 ml-4 text-gray-600 dark:text-gray-400"> Server: {{ data_get($deployment, 'server_name') }} @endif + + + @if (data_get($deployment, 'status') === 'queued') + Force Start + @endif + @if ( + data_get($deployment, 'status') === 'in_progress' || + data_get($deployment, 'status') === 'queued' + ) + Cancel + @endif +