Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/Livewire/Project/Application/Deployment/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ class="mt-2 ml-4 text-gray-600 dark:text-gray-400">
Server: {{ data_get($deployment, 'server_name') }}
</div>
@endif

<a href='#' class='flex items-center gap-2 mt-2'>
@if (data_get($deployment, 'status') === 'queued')
<x-forms.button wire:click.stop.prevent="force_start('{{ data_get($deployment, 'deployment_uuid') }}')">Force Start</x-forms.button>
@endif
@if (
data_get($deployment, 'status') === 'in_progress' ||
data_get($deployment, 'status') === 'queued'
)
<x-forms.button isError wire:click.stop.prevent="cancel('{{ data_get($deployment, 'deployment_uuid') }}')">Cancel</x-forms.button>
@endif
</a>
</div>
</a>
</div>
Expand Down