coolify/app/Livewire/Project/Service/Navbar.php

88 lines
2.6 KiB
PHP
Raw Normal View History

2023-09-22 11:23:49 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Project\Service;
2023-09-22 11:23:49 +02:00
2023-12-14 14:50:38 +01:00
use App\Actions\Shared\PullImage;
2023-09-22 11:23:49 +02:00
use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
2023-12-08 13:07:42 +01:00
use App\Events\ServiceStatusChanged;
use App\Jobs\ContainerStatusJob;
2023-09-22 11:23:49 +02:00
use App\Models\Service;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
2023-09-22 11:23:49 +02:00
class Navbar extends Component
{
public Service $service;
public array $parameters;
public array $query;
public $isDeploymentProgress = false;
2023-09-22 11:23:49 +02:00
2023-12-08 13:07:42 +01:00
public function checkDeployments()
{
$activity = Activity::where('properties->type_uuid', $this->service->uuid)->latest()->first();
$status = data_get($activity, 'properties.status');
if ($status === 'queued' || $status === 'in_progress') {
$this->isDeploymentProgress = true;
} else {
$this->isDeploymentProgress = false;
}
}
public function getListeners()
{
return [
2023-12-08 13:55:55 +01:00
"serviceStatusChanged"
];
}
2023-12-08 12:48:21 +01:00
public function serviceStatusChanged()
{
$this->service->refresh();
}
2023-09-22 11:23:49 +02:00
public function render()
{
return view('livewire.project.service.navbar');
}
public function check_status($showNotification = false)
{
dispatch_sync(new ContainerStatusJob($this->service->destination->server));
2023-11-05 09:49:23 +01:00
$this->service->refresh();
if ($showNotification) $this->dispatch('success', 'Service status updated.');
2023-11-05 09:49:23 +01:00
}
2023-09-22 11:23:49 +02:00
public function deploy()
{
$this->checkDeployments();
if ($this->isDeploymentProgress) {
$this->dispatch('error', 'There is a deployment in progress.');
return;
}
2023-09-22 11:23:49 +02:00
$this->service->parse();
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id);
2023-09-22 11:23:49 +02:00
}
2023-11-07 10:18:28 +01:00
public function stop(bool $forceCleanup = false)
2023-09-22 11:23:49 +02:00
{
StopService::run($this->service);
$this->service->refresh();
2023-11-07 10:18:28 +01:00
if ($forceCleanup) {
$this->dispatch('success', 'Containers cleaned up.');
2023-11-07 10:18:28 +01:00
} else {
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Service stopped.');
2023-11-07 10:18:28 +01:00
}
2023-12-08 18:32:08 +01:00
ServiceStatusChanged::dispatch();
2023-09-22 11:23:49 +02:00
}
2023-12-14 14:50:38 +01:00
public function restart()
{
$this->checkDeployments();
if ($this->isDeploymentProgress) {
$this->dispatch('error', 'There is a deployment in progress.');
return;
}
PullImage::run($this->service);
$this->dispatch('image-pulled');
StopService::run($this->service);
$this->service->parse();
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id);
2023-12-14 14:50:38 +01:00
}
2023-09-22 11:23:49 +02:00
}