lasthourcloud/app/Http/Livewire/Project/Application/DeploymentNavbar.php

62 lines
2.5 KiB
PHP
Raw Normal View History

2023-05-30 13:52:17 +00:00
<?php
namespace App\Http\Livewire\Project\Application;
2023-06-30 13:57:40 +00:00
use App\Enums\ApplicationDeploymentStatus;
2023-05-30 13:52:17 +00:00
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
2023-06-30 19:22:14 +00:00
use Illuminate\Support\Carbon;
2023-06-30 13:57:40 +00:00
use Illuminate\Support\Facades\Process;
2023-05-30 13:52:17 +00:00
use Livewire\Component;
2023-06-30 13:57:40 +00:00
use Illuminate\Support\Str;
2023-05-30 13:52:17 +00:00
2023-05-31 10:38:36 +00:00
class DeploymentNavbar extends Component
2023-05-30 13:52:17 +00:00
{
2023-05-31 12:42:37 +00:00
protected $listeners = ['deploymentFinished'];
2023-06-30 13:57:40 +00:00
public ApplicationDeploymentQueue $application_deployment_queue;
2023-05-31 12:42:37 +00:00
public function deploymentFinished()
{
2023-06-30 13:57:40 +00:00
$this->application_deployment_queue->refresh();
}
public function show_debug()
{
$application = Application::find($this->application_deployment_queue->application_id);
$application->settings->is_debug_enabled = !$application->settings->is_debug_enabled;
$application->settings->save();
$this->emit('refreshQueue');
2023-05-31 12:42:37 +00:00
}
2023-05-30 13:52:17 +00:00
public function cancel()
{
try {
2023-06-30 19:22:14 +00:00
$kill_command = "kill -9 {$this->application_deployment_queue->current_process_id}";
2023-06-30 13:57:40 +00:00
$application = Application::find($this->application_deployment_queue->application_id);
$server = $application->destination->server;
if ($this->application_deployment_queue->current_process_id) {
$process = Process::run("ps -p {$this->application_deployment_queue->current_process_id} -o command --no-headers");
if (Str::of($process->output())->contains([$server->ip, 'EOF-COOLIFY-SSH'])) {
2023-06-30 19:22:14 +00:00
Process::run($kill_command);
2023-06-30 13:57:40 +00:00
}
2023-06-30 19:22:14 +00:00
$previous_logs = json_decode($this->application_deployment_queue->logs, associative: true, flags: JSON_THROW_ON_ERROR);
$new_log_entry = [
'command' => $kill_command,
'output' => "Deployment cancelled by user.",
'type' => 'stderr',
'order' => count($previous_logs) + 1,
'timestamp' => Carbon::now('UTC'),
'hidden' => false,
];
$previous_logs[] = $new_log_entry;
2023-06-30 13:57:40 +00:00
$this->application_deployment_queue->update([
2023-06-30 19:22:14 +00:00
'logs' => json_encode($previous_logs, flags: JSON_THROW_ON_ERROR),
2023-06-30 13:57:40 +00:00
'current_process_id' => null,
'status' => ApplicationDeploymentStatus::CANCELLED_BY_USER->value,
]);
}
2023-06-09 13:55:21 +00:00
} catch (\Throwable $e) {
return general_error_handler(err: $e, that: $this);
2023-05-30 13:52:17 +00:00
}
}
}