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

102 lines
3.2 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 mount()
{
if (str($this->service->status())->contains('running') && is_null($this->service->config_hash)) {
ray('isConfigurationChanged init');
$this->service->isConfigurationChanged(true);
$this->dispatch('configurationChanged');
}
}
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',
2024-04-12 11:15:24 +00:00
"updateStatus"=> '$refresh',
];
}
public function serviceStarted()
{
2024-03-22 10:34:15 +00:00
$this->dispatch('success', 'Service status changed.');
2024-04-12 11:15:24 +00:00
if (is_null($this->service->config_hash) || $this->service->isConfigurationChanged()) {
$this->service->isConfigurationChanged(true);
$this->dispatch('configurationChanged');
} else {
$this->dispatch('configurationChanged');
}
2024-03-21 11:44:32 +00:00
}
2024-04-12 11:15:24 +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
}