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

155 lines
4.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
use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
use App\Enums\ProcessStatus;
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 Illuminate\Support\Facades\Auth;
2023-09-22 09:23:49 +00:00
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;
2023-09-22 09:23:49 +00:00
class Navbar extends Component
{
public Service $service;
2024-06-10 20:43:34 +00:00
2023-09-22 09:23:49 +00:00
public array $parameters;
2024-06-10 20:43:34 +00:00
2023-09-22 09:23:49 +00:00
public array $query;
2024-06-10 20:43:34 +00:00
public $isDeploymentProgress = false;
2024-03-21 11:44:32 +00:00
2024-09-04 12:34:46 +00:00
public $docker_cleanup = true;
public $title = 'Configuration';
2024-09-04 12:34:46 +00:00
public function mount()
{
if (str($this->service->status)->contains('running') && is_null($this->service->config_hash)) {
$this->service->isConfigurationChanged(true);
$this->dispatch('configurationChanged');
}
}
2024-06-10 20:43:34 +00:00
public function getListeners()
{
$userId = Auth::id();
2024-06-10 20:43:34 +00:00
return [
2024-03-21 11:44:32 +00:00
"echo-private:user.{$userId},ServiceStatusChanged" => 'serviceStarted',
2024-10-17 20:08:23 +00:00
'envsUpdated' => '$refresh',
'refreshStatus' => '$refresh',
];
}
2024-06-10 20:43:34 +00:00
public function serviceStarted()
{
// $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
public function check_status_without_notification()
{
$this->dispatch('check_status');
}
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.');
}
2024-06-10 20:43:34 +00:00
2024-03-01 09:36:32 +00:00
public function checkDeployments()
{
2024-07-25 19:51:53 +00:00
try {
$activity = Activity::where('properties->type_uuid', $this->service->uuid)->latest()->first();
2024-07-25 19:51:53 +00:00
$status = data_get($activity, 'properties.status');
if ($status === ProcessStatus::QUEUED->value || $status === ProcessStatus::IN_PROGRESS->value) {
$this->isDeploymentProgress = true;
} else {
$this->isDeploymentProgress = false;
}
} catch (\Throwable) {
2024-03-01 09:36:32 +00:00
$this->isDeploymentProgress = false;
}
return $this->isDeploymentProgress;
2023-11-05 08:49:23 +00:00
}
2024-06-10 20:43:34 +00:00
2024-03-21 11:44:32 +00:00
public function start()
2023-09-22 09:23:49 +00:00
{
$activity = StartService::run($this->service, pullLatestImages: true);
$this->dispatch('activityMonitor', $activity->id);
2023-09-22 09:23:49 +00:00
}
2024-06-10 20:43:34 +00:00
public function forceDeploy()
2023-09-22 09:23:49 +00:00
{
try {
$activities = Activity::where('properties->type_uuid', $this->service->uuid)->where('properties->status', ProcessStatus::IN_PROGRESS->value)->orWhere('properties->status', ProcessStatus::QUEUED->value)->get();
foreach ($activities as $activity) {
$activity->properties->status = ProcessStatus::ERROR->value;
$activity->save();
}
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
}
public function stop($cleanupContainers = false)
{
try {
StopService::run($this->service, false, $this->docker_cleanup);
ServiceStatusChanged::dispatch();
if ($cleanupContainers) {
$this->dispatch('success', 'Containers cleaned up.');
} else {
$this->dispatch('success', 'Service stopped.');
}
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
2023-09-22 09:23:49 +00:00
}
2024-06-10 20:43:34 +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.');
2024-06-10 20:43:34 +00:00
2023-12-14 13:50:38 +00:00
return;
}
$activity = StartService::run($this->service, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
}
public function pullAndRestartEvent()
{
$this->checkDeployments();
if ($this->isDeploymentProgress) {
$this->dispatch('error', 'There is a deployment in progress.');
return;
}
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
2023-12-14 13:50:38 +00:00
}
2024-09-04 12:34:46 +00:00
public function render()
{
return view('livewire.project.service.navbar', [
'checkboxes' => [
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
],
2024-09-04 12:34:46 +00:00
]);
}
2023-09-22 09:23:49 +00:00
}