From 317dc10af47ff47a795698e11c91c9c56aeca0ad Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 14 May 2024 15:19:28 +0200 Subject: [PATCH] fix: improve scheduled task adding/removing --- app/Jobs/ScheduledTaskJob.php | 6 +++- .../Project/Service/Configuration.php | 1 - .../Project/Shared/ScheduledTask/Add.php | 11 +++++++ .../Project/Shared/ScheduledTask/All.php | 19 +++++++++--- .../Project/Shared/ScheduledTask/Show.php | 14 +++++++-- app/Models/Service.php | 11 +++++++ .../project/service/configuration.blade.php | 18 +++++++++--- .../livewire/project/service/index.blade.php | 29 +------------------ .../shared/scheduled-task/add.blade.php | 29 +++++++++++++++---- .../shared/scheduled-task/all.blade.php | 21 +++++++++++--- .../scheduled-task/executions.blade.php | 2 +- .../shared/scheduled-task/show.blade.php | 18 +++++++++--- 12 files changed, 123 insertions(+), 56 deletions(-) diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.php index 4a38a005b..5c7b0d8af 100644 --- a/app/Jobs/ScheduledTaskJob.php +++ b/app/Jobs/ScheduledTaskJob.php @@ -77,8 +77,12 @@ class ScheduledTaskJob implements ShouldQueue $this->containers[] = data_get($application, 'name') . '-' . data_get($this->resource, 'uuid'); } }); + $this->resource->databases()->get()->each(function ($database) { + if (str(data_get($database, 'status'))->contains('running')) { + $this->containers[] = data_get($database, 'name') . '-' . data_get($this->resource, 'uuid'); + } + }); } - if (count($this->containers) == 0) { throw new \Exception('ScheduledTaskJob failed: No containers running.'); } diff --git a/app/Livewire/Project/Service/Configuration.php b/app/Livewire/Project/Service/Configuration.php index 0b26af22f..86c9a8a31 100644 --- a/app/Livewire/Project/Service/Configuration.php +++ b/app/Livewire/Project/Service/Configuration.php @@ -3,7 +3,6 @@ namespace App\Livewire\Project\Service; use App\Actions\Docker\GetContainersStatus; -use App\Jobs\ContainerStatusJob; use App\Models\Service; use Livewire\Component; diff --git a/app/Livewire/Project/Shared/ScheduledTask/Add.php b/app/Livewire/Project/Shared/ScheduledTask/Add.php index 3a7a3fa23..c415ff3e4 100644 --- a/app/Livewire/Project/Shared/ScheduledTask/Add.php +++ b/app/Livewire/Project/Shared/ScheduledTask/Add.php @@ -2,11 +2,14 @@ namespace App\Livewire\Project\Shared\ScheduledTask; +use Illuminate\Support\Collection; use Livewire\Component; class Add extends Component { public $parameters; + public string $type; + public Collection $containerNames; public string $name; public string $command; public string $frequency; @@ -29,6 +32,9 @@ class Add extends Component public function mount() { $this->parameters = get_route_parameters(); + if ($this->containerNames->count() > 0) { + $this->container = $this->containerNames->first(); + } } public function submit() @@ -40,6 +46,11 @@ class Add extends Component $this->dispatch('error', 'Invalid Cron / Human expression.'); return; } + if (empty($this->container) || $this->container == 'null') { + if ($this->type == 'service') { + $this->container = $this->subServiceName; + } + } $this->dispatch('saveScheduledTask', [ 'name' => $this->name, 'command' => $this->command, diff --git a/app/Livewire/Project/Shared/ScheduledTask/All.php b/app/Livewire/Project/Shared/ScheduledTask/All.php index 975d695fa..73b2733b4 100644 --- a/app/Livewire/Project/Shared/ScheduledTask/All.php +++ b/app/Livewire/Project/Shared/ScheduledTask/All.php @@ -3,14 +3,13 @@ namespace App\Livewire\Project\Shared\ScheduledTask; use App\Models\ScheduledTask; +use Illuminate\Support\Collection; use Livewire\Component; -use Visus\Cuid2\Cuid2; -use Illuminate\Support\Str; class All extends Component { public $resource; - public string|null $modalId = null; + public Collection $containerNames; public ?string $variables = null; public array $parameters; protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit']; @@ -18,7 +17,19 @@ class All extends Component public function mount() { $this->parameters = get_route_parameters(); - $this->modalId = new Cuid2(7); + if ($this->resource->type() == 'service') { + $this->containerNames = $this->resource->applications()->pluck('name'); + $this->containerNames = $this->containerNames->merge($this->resource->databases()->pluck('name')); + ray($this->containerNames); + } elseif ($this->resource->type() == 'application') { + if ($this->resource->build_pack === 'dockercompose') { + $parsed = $this->resource->parseCompose(); + $containers = collect($parsed['services'])->keys(); + $this->containerNames = $containers; + } else { + $this->containerNames = collect([]); + } + } } public function refreshTasks() { diff --git a/app/Livewire/Project/Shared/ScheduledTask/Show.php b/app/Livewire/Project/Shared/ScheduledTask/Show.php index 87b752509..7490c7055 100644 --- a/app/Livewire/Project/Shared/ScheduledTask/Show.php +++ b/app/Livewire/Project/Shared/ScheduledTask/Show.php @@ -17,6 +17,7 @@ class Show extends Component public string $type; protected $rules = [ + 'task.enabled' => 'required|boolean', 'task.name' => 'required|string', 'task.command' => 'required|string', 'task.frequency' => 'required|string', @@ -45,9 +46,18 @@ class Show extends Component $this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first(); } + public function instantSave() + { + $this->validateOnly('task.enabled'); + $this->task->save(['enabled' => $this->task->enabled]); + $this->dispatch('success', 'Scheduled task updated.'); + $this->dispatch('refreshTasks'); + } public function submit() { $this->validate(); + $this->task->name = str($this->task->name)->trim()->value(); + $this->task->container = str($this->task->container)->trim()->value(); $this->task->save(); $this->dispatch('success', 'Scheduled task updated.'); $this->dispatch('refreshTasks'); @@ -60,11 +70,9 @@ class Show extends Component if ($this->type == 'application') { return redirect()->route('project.application.configuration', $this->parameters); - } - else { + } else { return redirect()->route('project.service.configuration', $this->parameters); } - } catch (\Exception $e) { return handleError($e); } diff --git a/app/Models/Service.php b/app/Models/Service.php index cd8e578d6..770dfae2e 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -677,6 +677,17 @@ class Service extends BaseModel { return $this->belongsTo(Server::class); } + public function byUuid(string $uuid) { + $app = $this->applications()->whereUuid($uuid)->first(); + if ($app) { + return $app; + } + $db = $this->databases()->whereUuid($uuid)->first(); + if ($db) { + return $db; + } + return null; + } public function byName(string $name) { $app = $this->applications()->whereName($name)->first(); diff --git a/resources/views/livewire/project/service/configuration.blade.php b/resources/views/livewire/project/service/configuration.blade.php index 16cabf689..185fbdb77 100644 --- a/resources/views/livewire/project/service/configuration.blade.php +++ b/resources/views/livewire/project/service/configuration.blade.php @@ -16,6 +16,10 @@ @click.prevent="activeTab = 'storages'; window.location.hash = 'storages'" href="#">Storages + Scheduled Tasks + Settings - - This application will be unavailable during the restart.
Please think again. -
+ @if (str($application->status)->contains('running')) + + This application will be unavailable during the restart.
Please think + again. +
+ @endif @@ -169,6 +176,9 @@ @endforeach +
+ +
diff --git a/resources/views/livewire/project/service/index.blade.php b/resources/views/livewire/project/service/index.blade.php index 318df88d0..0ce919049 100644 --- a/resources/views/livewire/project/service/index.blade.php +++ b/resources/views/livewire/project/service/index.blade.php @@ -10,14 +10,6 @@ General - Storages - - Scheduled Tasks - @if (str($serviceDatabase?->databaseType())->contains('mysql') || str($serviceDatabase?->databaseType())->contains('postgres') || str($serviceDatabase?->databaseType())->contains('mariadb')) @@ -30,28 +22,12 @@
-
-
-

Storages

-
-
Persistent storage to preserve data between deployments.
- Please modify storage layout in your Docker Compose file. - -
+ @endisset @isset($serviceDatabase)
-
-
-

Storages

-
-
Persistent storage to preserve data between deployments.
- Please modify storage layout in your Docker Compose file. - -

Scheduled Backups

@@ -62,9 +38,6 @@
@endisset -
- -
diff --git a/resources/views/livewire/project/shared/scheduled-task/add.blade.php b/resources/views/livewire/project/shared/scheduled-task/add.blade.php index 3226981b8..9e9af07bf 100644 --- a/resources/views/livewire/project/shared/scheduled-task/add.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/add.blade.php @@ -1,10 +1,27 @@
- - - - - + + + + @if ($type === 'application') + @if ($containerNames->count() > 1) + + @foreach ($containerNames as $containerName) + + @endforeach + + @else + + @endif + @elseif ($type === 'service') + + @foreach ($containerNames as $containerName) + + @endforeach + + @endif + + Save diff --git a/resources/views/livewire/project/shared/scheduled-task/all.blade.php b/resources/views/livewire/project/shared/scheduled-task/all.blade.php index 3ed22e5e7..a3c0587ca 100644 --- a/resources/views/livewire/project/shared/scheduled-task/all.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/all.blade.php @@ -1,8 +1,12 @@

Scheduled Tasks

- - + + @if ($resource->type() == 'application') + + @elseif ($resource->type() == 'service') + + @endif
@endif data_get($execution, 'id') == $selectedKey, 'border-green-500' => data_get($execution, 'status') === 'success', diff --git a/resources/views/livewire/project/shared/scheduled-task/show.blade.php b/resources/views/livewire/project/shared/scheduled-task/show.blade.php index 144d22f16..9a08fe04b 100644 --- a/resources/views/livewire/project/shared/scheduled-task/show.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/show.blade.php @@ -1,13 +1,13 @@
-

Scheduled Task

@if ($type === 'application') +

Scheduled Task

@elseif ($type === 'service') @endif
-
+

Scheduled Task

@@ -17,13 +17,23 @@ You will delete scheduled task {{ $task->name }}.
+
+ +
- + @if ($type === 'application') + + @elseif ($type === 'service') + + @endif