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

123 lines
3.5 KiB
PHP
Raw Normal View History

2024-01-07 15:23:41 +00:00
<?php
namespace App\Livewire\Project\Service;
use App\Models\Service;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
2024-01-07 15:23:41 +00:00
use Livewire\Component;
class Configuration extends Component
{
use AuthorizesRequests;
2024-12-17 10:13:13 +00:00
public $currentRoute;
public $project;
public $environment;
2024-01-31 12:46:40 +00:00
public ?Service $service = null;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public $applications;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public $databases;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public array $query;
2024-06-10 20:43:34 +00:00
2024-12-17 10:13:13 +00:00
public array $parameters;
2024-01-07 15:23:41 +00:00
public function getListeners()
{
$teamId = Auth::user()->currentTeam()->id;
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
return [
"echo-private:team.{$teamId},ServiceChecked" => 'serviceChecked',
fix: prevent duplicate services on image change and enable real-time UI refresh This commit addresses two critical issues with Docker Compose service management: ## Issue 1: Duplicate Services Created on Image Change When changing the image in a docker-compose file, the parser was creating new ServiceApplication/ServiceDatabase records instead of updating existing ones. **Root Cause**: The parsers used `firstOrCreate()` with `['name', 'image', 'service_id']`, meaning any image change would create a new record. **Fix**: Remove `image` from `firstOrCreate()` queries and update it separately after finding or creating the service record. **Changes**: - `bootstrap/helpers/parsers.php` (serviceParser v3): Fixed in presave loop (lines 1188-1203) and main parsing loop (lines 1519-1539) - `bootstrap/helpers/shared.php` (parseDockerComposeFile v2): Fixed null check logic (lines 1308-1348) ## Issue 2: UI Not Refreshing After Changes When compose file or domain was modified, the Configuration component wasn't receiving events to refresh its data, requiring manual page refresh to see updates. **Root Cause**: The Configuration component wasn't listening for refresh events dispatched by child components (StackForm, EditDomain). **Fix**: Add event listeners and dispatchers to enable real-time UI updates. **Changes**: - `app/Livewire/Project/Service/Configuration.php`: Added listeners for `refreshServices` and `refresh` events (lines 36-37) - `app/Livewire/Project/Service/EditDomain.php`: Added `refreshServices` dispatch (line 76) - Note: `app/Livewire/Project/Service/StackForm.php` already had the dispatch ## Tests Added - `tests/Unit/ServiceParserImageUpdateTest.php`: 4 tests verifying no duplicates created - `tests/Unit/ServiceConfigurationRefreshTest.php`: 4 tests verifying event dispatching All 8 new tests pass, and all existing unit tests continue to pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 08:12:36 +00:00
'refreshServices' => 'refreshServices',
'refresh' => 'refreshServices',
2024-01-07 15:23:41 +00:00
];
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function render()
{
return view('livewire.project.service.configuration');
}
2024-06-10 20:43:34 +00:00
2024-01-07 15:23:41 +00:00
public function mount()
{
try {
$this->parameters = get_route_parameters();
$this->currentRoute = request()->route()->getName();
$this->query = request()->query();
$project = currentTeam()
->projects()
->select('id', 'uuid', 'team_id')
->where('uuid', request()->route('project_uuid'))
->firstOrFail();
$environment = $project->environments()
->select('id', 'uuid', 'name', 'project_id')
->where('uuid', request()->route('environment_uuid'))
->firstOrFail();
$this->service = $environment->services()->whereUuid(request()->route('service_uuid'))->firstOrFail();
$this->authorize('view', $this->service);
$this->project = $project;
$this->environment = $environment;
$this->applications = $this->service->applications->sort();
$this->databases = $this->service->databases->sort();
} catch (\Throwable $e) {
return handleError($e, $this);
}
2024-01-07 15:23:41 +00:00
}
2024-06-10 20:43:34 +00:00
public function refreshServices()
{
$this->service->refresh();
$this->applications = $this->service->applications->sort();
$this->databases = $this->service->databases->sort();
}
2024-03-25 10:33:38 +00:00
public function restartApplication($id)
{
try {
$this->authorize('update', $this->service);
2024-03-25 10:33:38 +00:00
$application = $this->service->applications->find($id);
if ($application) {
$application->restart();
2024-09-03 14:59:51 +00:00
$this->dispatch('success', 'Service application restarted successfully.');
2024-03-25 10:33:38 +00:00
}
} catch (\Exception $e) {
2024-03-25 10:33:38 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2024-03-25 10:33:38 +00:00
public function restartDatabase($id)
{
try {
$this->authorize('update', $this->service);
2024-03-25 10:33:38 +00:00
$database = $this->service->databases->find($id);
if ($database) {
$database->restart();
2024-09-03 14:59:51 +00:00
$this->dispatch('success', 'Service database restarted successfully.');
2024-03-25 10:33:38 +00:00
}
} catch (\Exception $e) {
2024-03-25 10:33:38 +00:00
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
public function serviceChecked()
2024-01-07 15:23:41 +00:00
{
2024-03-14 08:21:48 +00:00
try {
2024-08-29 11:00:43 +00:00
$this->service->applications->each(function ($application) {
$application->refresh();
});
$this->service->databases->each(function ($database) {
$database->refresh();
});
} catch (\Exception $e) {
2024-03-14 08:21:48 +00:00
return handleError($e, $this);
}
2024-01-07 15:23:41 +00:00
}
}