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

83 lines
2.4 KiB
PHP
Raw Normal View History

2023-09-22 09:23:49 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Service;
2023-09-22 09:23:49 +00:00
2023-12-14 13:50:38 +00:00
use App\Actions\Shared\PullImage;
2023-09-22 09:23:49 +00:00
use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
2023-12-08 12:07:42 +00:00
use App\Events\ServiceStatusChanged;
2023-09-22 09:23:49 +00:00
use App\Models\Service;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
2023-09-22 09:23:49 +00:00
class Navbar extends Component
{
public Service $service;
public array $parameters;
public array $query;
public $isDeploymentProgress = false;
public function getListeners()
{
return [
2023-12-08 12:55:55 +00:00
"serviceStatusChanged"
];
}
2023-12-08 11:48:21 +00:00
public function serviceStatusChanged()
{
2024-03-01 09:36:32 +00:00
$this->dispatch('refresh')->self();
2023-12-08 11:48:21 +00:00
}
2024-03-04 07:57:18 +00:00
public function check_status() {
$this->dispatch('check_status');
$this->dispatch('success', 'Service status updated.');
}
2023-09-22 09:23:49 +00:00
public function render()
{
return view('livewire.project.service.navbar');
}
2024-03-01 09:36:32 +00:00
public function checkDeployments()
{
2024-03-01 09:36:32 +00:00
$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;
}
2023-11-05 08:49:23 +00:00
}
2023-09-22 09:23:49 +00:00
public function deploy()
{
$this->checkDeployments();
if ($this->isDeploymentProgress) {
$this->dispatch('error', 'There is a deployment in progress.');
return;
}
2023-09-22 09:23:49 +00:00
$this->service->parse();
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id);
2023-09-22 09:23:49 +00:00
}
2023-11-07 09:18:28 +00:00
public function stop(bool $forceCleanup = false)
2023-09-22 09:23:49 +00:00
{
StopService::run($this->service);
2023-11-07 09:18:28 +00:00
if ($forceCleanup) {
$this->dispatch('success', 'Containers cleaned up.');
2023-11-07 09:18:28 +00:00
} else {
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Service stopped.');
2023-11-07 09:18:28 +00:00
}
2023-12-08 17:32:08 +00:00
ServiceStatusChanged::dispatch();
2023-09-22 09:23:49 +00:00
}
2023-12-14 13:50:38 +00: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 13:50:38 +00:00
}
2023-09-22 09:23:49 +00:00
}