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

90 lines
2.7 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;
2024-03-21 11:44:32 +00:00
public function getListeners()
{
2024-03-21 11:44:32 +00:00
$userId = auth()->user()->id;
return [
2024-03-21 11:44:32 +00:00
"echo-private:user.{$userId},ServiceStatusChanged" => 'serviceStarted',
2023-12-08 12:55:55 +00:00
"serviceStatusChanged"
];
}
2024-03-21 11:44:32 +00:00
public function serviceStarted() {
2024-03-22 10:34:15 +00:00
$this->dispatch('success', 'Service status changed.');
2024-03-21 11:44:32 +00:00
}
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-21 11:44:32 +00:00
public function check_status()
{
2024-03-04 07:57:18 +00:00
$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
}
2024-03-21 11:44:32 +00:00
public function start()
2023-09-22 09:23:49 +00:00
{
$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);
StopService::run($this->service);
$this->service->parse();
2024-03-21 11:44:32 +00:00
$this->dispatch('imagePulled');
2023-12-14 13:50:38 +00:00
$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
}