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

167 lines
5 KiB
PHP
Raw Permalink 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\Docker\GetContainersStatus;
2023-09-22 09:23:49 +00:00
use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
use App\Enums\ProcessStatus;
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 Heading extends Component
2023-09-22 09:23:49 +00:00
{
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()
{
$teamId = Auth::user()->currentTeam()->id;
2024-06-10 20:43:34 +00:00
return [
"echo-private:team.{$teamId},ServiceStatusChanged" => 'checkStatus',
"echo-private:team.{$teamId},ServiceChecked" => 'serviceChecked',
'refresh' => '$refresh',
2024-10-17 20:08:23 +00:00
'envsUpdated' => '$refresh',
];
}
2024-06-10 20:43:34 +00:00
public function checkStatus()
{
if ($this->service->server->isFunctional()) {
GetContainersStatus::dispatch($this->service->server);
} else {
$this->dispatch('error', 'Server is not functional.');
2024-04-12 11:15:24 +00:00
}
2024-03-21 11:44:32 +00:00
}
2024-04-12 11:15:24 +00:00
public function manualCheckStatus()
{
$this->checkStatus();
}
public function serviceChecked()
{
try {
$this->service->applications->each(function ($application) {
$application->refresh();
});
$this->service->databases->each(function ($database) {
$database->refresh();
});
if (is_null($this->service->config_hash)) {
$this->service->isConfigurationChanged(true);
}
$this->dispatch('configurationChanged');
} catch (\Exception $e) {
return handleError($e, $this);
} finally {
$this->dispatch('refresh')->self();
}
2024-03-04 07:57:18 +00:00
}
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(function ($q) {
$q->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()
{
try {
StopService::dispatch($this->service, false, $this->docker_cleanup);
} 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.heading', [
2024-09-04 12:34:46 +00:00
'checkboxes' => [
['id' => 'docker_cleanup', 'label' => __('resource.docker_cleanup')],
],
2024-09-04 12:34:46 +00:00
]);
}
2023-09-22 09:23:49 +00:00
}