From 1f185429607e80a77947138a000faca0ae20e41a Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 14 Feb 2024 15:14:06 +0100 Subject: [PATCH] fix: cleanup scheduled tasks --- app/Actions/Service/DeleteService.php | 3 +++ app/Console/Commands/CleanupStuckedResources.php | 12 ++++++++++++ app/Console/Kernel.php | 4 ++-- app/Models/Application.php | 3 +++ .../project/shared/scheduled-task/show.blade.php | 2 +- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/Actions/Service/DeleteService.php b/app/Actions/Service/DeleteService.php index 1a411cf59..420f40f3b 100644 --- a/app/Actions/Service/DeleteService.php +++ b/app/Actions/Service/DeleteService.php @@ -45,6 +45,9 @@ class DeleteService foreach ($service->databases()->get() as $database) { $database->forceDelete(); } + foreach ($service->scheduled_tasks as $task) { + $task->delete(); + } $service->tags()->detach(); } } diff --git a/app/Console/Commands/CleanupStuckedResources.php b/app/Console/Commands/CleanupStuckedResources.php index 38d87e24b..d2882e9b3 100644 --- a/app/Console/Commands/CleanupStuckedResources.php +++ b/app/Console/Commands/CleanupStuckedResources.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use App\Models\Application; +use App\Models\ScheduledTask; use App\Models\Service; use App\Models\ServiceApplication; use App\Models\ServiceDatabase; @@ -108,6 +109,17 @@ class CleanupStuckedResources extends Command } catch (\Throwable $e) { echo "Error in cleaning stuck serviceapp: {$e->getMessage()}\n"; } + try { + $scheduled_tasks = ScheduledTask::all(); + foreach ($scheduled_tasks as $scheduled_task) { + if (!$scheduled_task->service && !$scheduled_task->application) { + echo "Deleting stuck scheduledtask: {$scheduled_task->name}\n"; + $scheduled_task->delete(); + } + } + } catch (\Throwable $e) { + echo "Error in cleaning stuck scheduledtasks: {$e->getMessage()}\n"; + } // Cleanup any resources that are not attached to any environment or destination or server try { diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 593ce8fe1..bba2da818 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -120,8 +120,8 @@ class Kernel extends ConsoleKernel return; } foreach ($scheduled_tasks as $scheduled_task) { - $service = $scheduled_task->service()->first(); - $application = $scheduled_task->application()->first(); + $service = $scheduled_task->service; + $application = $scheduled_task->application; if (!$application && !$service) { ray('application/service attached to scheduled task does not exist'); diff --git a/app/Models/Application.php b/app/Models/Application.php index 0ba1d24d1..b37476565 100644 --- a/app/Models/Application.php +++ b/app/Models/Application.php @@ -48,6 +48,9 @@ class Application extends BaseModel $application->persistentStorages()->delete(); $application->environment_variables()->delete(); $application->environment_variables_preview()->delete(); + foreach ($application->scheduled_tasks as $task) { + $task->delete(); + } $application->tags()->detach(); }); } 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 74fcc3085..171cc964d 100644 --- a/resources/views/livewire/project/shared/scheduled-task/show.blade.php +++ b/resources/views/livewire/project/shared/scheduled-task/show.blade.php @@ -28,7 +28,7 @@
-

Recent executions

+

Recent executions (click to check output)