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