refactor: simplify service start and restart workflows

This commit is contained in:
Andras Bacsai
2025-02-04 14:34:34 +01:00
parent 8b8ae17c1a
commit d632eb2be9
4 changed files with 16 additions and 47 deletions

View File

@@ -12,19 +12,24 @@ class StartService
public string $jobQueue = 'high'; public string $jobQueue = 'high';
public function handle(Service $service) public function handle(Service $service, bool $pullLatestImages = false, bool $stopBeforeStart = false)
{ {
$service->parse();
if ($stopBeforeStart) {
StopService::run(service: $service, dockerCleanup: false);
}
$service->saveComposeConfigs(); $service->saveComposeConfigs();
$commands[] = 'cd '.$service->workdir(); $commands[] = 'cd '.$service->workdir();
$commands[] = "echo 'Saved configuration files to {$service->workdir()}.'"; $commands[] = "echo 'Saved configuration files to {$service->workdir()}.'";
if ($pullLatestImages) {
$commands[] = "echo 'Pulling images.'";
$commands[] = 'docker compose pull';
}
if ($service->networks()->count() > 0) { if ($service->networks()->count() > 0) {
$commands[] = "echo 'Creating Docker network.'"; $commands[] = "echo 'Creating Docker network.'";
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid"; $commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
} }
$commands[] = 'echo Starting service.'; $commands[] = 'echo Starting service.';
$commands[] = "echo 'Pulling images.'";
$commands[] = 'docker compose pull';
$commands[] = "echo 'Starting containers.'";
$commands[] = 'docker compose up -d --remove-orphans --force-recreate --build'; $commands[] = 'docker compose up -d --remove-orphans --force-recreate --build';
$commands[] = "docker network connect $service->uuid coolify-proxy >/dev/null 2>&1 || true"; $commands[] = "docker network connect $service->uuid coolify-proxy >/dev/null 2>&1 || true";
if (data_get($service, 'connect_to_docker_network')) { if (data_get($service, 'connect_to_docker_network')) {

View File

@@ -1,28 +0,0 @@
<?php
namespace App\Actions\Shared;
use App\Models\Service;
use Lorisleiva\Actions\Concerns\AsAction;
class PullImage
{
use AsAction;
public function handle(Service $resource)
{
$resource->saveComposeConfigs();
$commands[] = 'cd '.$resource->workdir();
$commands[] = "echo 'Saved configuration files to {$resource->workdir()}.'";
$commands[] = 'docker compose pull';
$server = data_get($resource, 'server');
if (! $server) {
return;
}
instant_remote_process($commands, $resource->server);
}
}

View File

@@ -4,7 +4,6 @@ namespace App\Livewire\Project\Service;
use App\Actions\Service\StartService; use App\Actions\Service\StartService;
use App\Actions\Service\StopService; use App\Actions\Service\StopService;
use App\Actions\Shared\PullImage;
use App\Enums\ProcessStatus; use App\Enums\ProcessStatus;
use App\Events\ServiceStatusChanged; use App\Events\ServiceStatusChanged;
use App\Models\Service; use App\Models\Service;
@@ -85,8 +84,7 @@ class Navbar extends Component
public function start() public function start()
{ {
$this->service->parse(); $activity = StartService::run($this->service, pullLatestImages: true);
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id); $this->dispatch('activityMonitor', $activity->id);
} }
@@ -98,8 +96,7 @@ class Navbar extends Component
$activity->properties->status = ProcessStatus::ERROR->value; $activity->properties->status = ProcessStatus::ERROR->value;
$activity->save(); $activity->save();
} }
$this->service->parse(); $activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id); $this->dispatch('activityMonitor', $activity->id);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->dispatch('error', $e->getMessage()); $this->dispatch('error', $e->getMessage());
@@ -129,10 +126,7 @@ class Navbar extends Component
return; return;
} }
StopService::run(service: $this->service, dockerCleanup: false); $activity = StartService::run($this->service, stopBeforeStart: true);
$this->service->parse();
$this->dispatch('imagePulled');
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id); $this->dispatch('activityMonitor', $activity->id);
} }
@@ -144,11 +138,7 @@ class Navbar extends Component
return; return;
} }
PullImage::run($this->service); $activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
StopService::run(service: $this->service, dockerCleanup: false);
$this->service->parse();
$this->dispatch('imagePulled');
$activity = StartService::run($this->service);
$this->dispatch('activityMonitor', $activity->id); $this->dispatch('activityMonitor', $activity->id);
} }

View File

@@ -165,10 +165,12 @@
return; return;
} }
$wire.$dispatch('info', 'Service restart in progress.'); $wire.$dispatch('info', 'Service restart in progress.');
window.dispatchEvent(new CustomEvent('startservice'));
$wire.$call('restart'); $wire.$call('restart');
}); });
$wire.$on('pullAndRestartEvent', () => { $wire.$on('pullAndRestartEvent', () => {
$wire.$dispatch('info', 'Pulling new images.'); $wire.$dispatch('info', 'Pulling new images and restarting service.');
window.dispatchEvent(new CustomEvent('startservice'));
$wire.$call('pullAndRestartEvent'); $wire.$call('pullAndRestartEvent');
}); });
$wire.on('imagePulled', () => { $wire.on('imagePulled', () => {