Skip to content

Commit

Permalink
refactor: simplify service start and restart workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Feb 4, 2025
1 parent 8b8ae17 commit d632eb2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 47 deletions.
13 changes: 9 additions & 4 deletions app/Actions/Service/StartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ class StartService

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();
$commands[] = 'cd '.$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) {
$commands[] = "echo 'Creating Docker network.'";
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
}
$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 network connect $service->uuid coolify-proxy >/dev/null 2>&1 || true";
if (data_get($service, 'connect_to_docker_network')) {
Expand Down
28 changes: 0 additions & 28 deletions app/Actions/Shared/PullImage.php

This file was deleted.

18 changes: 4 additions & 14 deletions app/Livewire/Project/Service/Navbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Actions\Service\StartService;
use App\Actions\Service\StopService;
use App\Actions\Shared\PullImage;
use App\Enums\ProcessStatus;
use App\Events\ServiceStatusChanged;
use App\Models\Service;
Expand Down Expand Up @@ -85,8 +84,7 @@ public function checkDeployments()

public function start()
{
$this->service->parse();
$activity = StartService::run($this->service);
$activity = StartService::run($this->service, pullLatestImages: true);
$this->dispatch('activityMonitor', $activity->id);
}

Expand All @@ -98,8 +96,7 @@ public function forceDeploy()
$activity->properties->status = ProcessStatus::ERROR->value;
$activity->save();
}
$this->service->parse();
$activity = StartService::run($this->service);
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
Expand Down Expand Up @@ -129,10 +126,7 @@ public function restart()

return;
}
StopService::run(service: $this->service, dockerCleanup: false);
$this->service->parse();
$this->dispatch('imagePulled');
$activity = StartService::run($this->service);
$activity = StartService::run($this->service, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
}

Expand All @@ -144,11 +138,7 @@ public function pullAndRestartEvent()

return;
}
PullImage::run($this->service);
StopService::run(service: $this->service, dockerCleanup: false);
$this->service->parse();
$this->dispatch('imagePulled');
$activity = StartService::run($this->service);
$activity = StartService::run($this->service, pullLatestImages: true, stopBeforeStart: true);
$this->dispatch('activityMonitor', $activity->id);
}

Expand Down
4 changes: 3 additions & 1 deletion resources/views/livewire/project/service/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@
return;
}
$wire.$dispatch('info', 'Service restart in progress.');
window.dispatchEvent(new CustomEvent('startservice'));
$wire.$call('restart');
});
$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.on('imagePulled', () => {
Expand Down

0 comments on commit d632eb2

Please sign in to comment.