coolify/app/Jobs/DeleteResourceJob.php

78 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
use App\Actions\Application\StopApplication;
use App\Actions\Database\StopDatabase;
2023-12-08 18:32:08 +01:00
use App\Actions\Service\DeleteService;
2024-01-31 09:58:41 +01:00
use App\Actions\Service\StopService;
use App\Models\Application;
use App\Models\Service;
2024-04-10 15:00:46 +02:00
use App\Models\StandaloneClickhouse;
use App\Models\StandaloneDragonfly;
use App\Models\StandaloneKeydb;
2023-10-24 14:31:28 +02:00
use App\Models\StandaloneMariadb;
2023-10-19 13:32:03 +02:00
use App\Models\StandaloneMongodb;
2023-10-24 14:31:28 +02:00
use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;
2024-06-10 22:43:34 +02:00
class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2024-07-11 12:50:12 +02:00
public function __construct(
public Application|Service|StandalonePostgresql|StandaloneRedis|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse $resource,
public bool $deleteConfigurations = false,
public bool $deleteVolumes = false) {}
public function handle()
{
try {
2024-07-11 12:38:54 +02:00
$persistentStorages = collect();
switch ($this->resource->type()) {
case 'application':
2024-07-11 12:38:54 +02:00
$persistentStorages = $this->resource?->persistentStorages()?->get();
StopApplication::run($this->resource, previewDeployments: true);
break;
case 'standalone-postgresql':
case 'standalone-redis':
2023-10-19 13:32:03 +02:00
case 'standalone-mongodb':
2023-10-24 14:31:28 +02:00
case 'standalone-mysql':
case 'standalone-mariadb':
2024-04-10 15:00:46 +02:00
case 'standalone-keydb':
case 'standalone-dragonfly':
case 'standalone-clickhouse':
2024-07-11 12:38:54 +02:00
$persistentStorages = $this->resource?->persistentStorages()?->get();
2023-10-24 14:31:28 +02:00
StopDatabase::run($this->resource);
break;
case 'service':
2024-01-31 09:58:41 +01:00
StopService::run($this->resource);
DeleteService::run($this->resource);
break;
2023-12-08 18:32:08 +01:00
}
2024-07-11 12:38:54 +02:00
if ($this->deleteVolumes && $this->resource->type() !== 'service') {
$this->resource?->delete_volumes($persistentStorages);
}
if ($this->deleteConfigurations) {
$this->resource?->delete_configurations();
}
} catch (\Throwable $e) {
ray($e->getMessage());
2024-06-10 22:43:34 +02:00
send_internal_notification('ContainerStoppingJob failed with: '.$e->getMessage());
throw $e;
} finally {
2024-07-11 12:38:54 +02:00
$this->resource->forceDelete();
Artisan::queue('cleanup:stucked-resources');
}
}
}