2023-07-07 13:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\Server;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2023-09-14 08:12:44 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
2023-07-07 13:50:36 +00:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2023-09-01 08:11:00 +00:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2023-07-07 13:50:36 +00:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-11-10 09:55:23 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-11-22 15:39:16 +00:00
|
|
|
use RuntimeException;
|
2023-07-07 13:50:36 +00:00
|
|
|
|
2023-09-14 08:12:44 +00:00
|
|
|
class DockerCleanupJob implements ShouldQueue, ShouldBeEncrypted
|
2023-07-07 13:50:36 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-11-10 09:34:28 +00:00
|
|
|
public $timeout = 300;
|
2023-08-29 08:00:29 +00:00
|
|
|
public ?int $usageBefore = null;
|
2023-09-01 08:11:00 +00:00
|
|
|
|
2023-09-29 12:26:42 +00:00
|
|
|
public function __construct(public Server $server)
|
2023-07-07 13:50:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
public function handle(): void
|
|
|
|
{
|
|
|
|
try {
|
2023-11-16 10:53:37 +00:00
|
|
|
$isInprogress = false;
|
|
|
|
$this->server->applications()->each(function ($application) use (&$isInprogress) {
|
|
|
|
if ($application->isDeploymentInprogress()) {
|
|
|
|
$isInprogress = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if ($isInprogress) {
|
2023-11-22 15:39:16 +00:00
|
|
|
throw new RuntimeException('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping...');
|
2023-11-16 10:53:37 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
if (!$this->server->isFunctional()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-16 13:29:01 +00:00
|
|
|
$this->usageBefore = $this->server->getDiskUsage();
|
|
|
|
ray('Usage before: ' . $this->usageBefore);
|
2023-09-29 12:26:42 +00:00
|
|
|
if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) {
|
2023-11-10 09:34:28 +00:00
|
|
|
ray('Cleaning up ' . $this->server->name);
|
2023-11-16 13:29:01 +00:00
|
|
|
instant_remote_process(['docker image prune -af'], $this->server, false);
|
|
|
|
instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $this->server, false);
|
|
|
|
instant_remote_process(['docker builder prune -af'], $this->server, false);
|
2023-11-16 10:53:37 +00:00
|
|
|
$usageAfter = $this->server->getDiskUsage();
|
2023-09-29 12:26:42 +00:00
|
|
|
if ($usageAfter < $this->usageBefore) {
|
2023-11-10 09:34:28 +00:00
|
|
|
ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
|
2023-09-29 12:26:42 +00:00
|
|
|
send_internal_notification('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
|
2023-11-10 09:55:23 +00:00
|
|
|
Log::info('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
|
2023-08-29 08:00:29 +00:00
|
|
|
} else {
|
2023-11-10 09:55:23 +00:00
|
|
|
Log::info('DockerCleanupJob failed to save disk space on ' . $this->server->name);
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
} else {
|
2023-11-10 09:34:28 +00:00
|
|
|
ray('No need to clean up ' . $this->server->name);
|
2023-11-10 09:55:23 +00:00
|
|
|
Log::info('No need to clean up ' . $this->server->name);
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-08-24 14:14:09 +00:00
|
|
|
send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage());
|
2023-11-10 09:34:28 +00:00
|
|
|
ray($e->getMessage());
|
2023-08-24 19:09:58 +00:00
|
|
|
throw $e;
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-26 11:33:48 +00:00
|
|
|
}
|