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-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-09-29 12:26:42 +00:00
|
|
|
public $timeout = 1000;
|
2023-08-29 08:00:29 +00:00
|
|
|
public ?string $dockerRootFilesystem = null;
|
|
|
|
public ?int $usageBefore = null;
|
2023-09-01 08:11:00 +00:00
|
|
|
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
2023-11-07 08:51:48 +00:00
|
|
|
return [(new WithoutOverlapping($this->server->uuid))];
|
2023-09-01 08:11:00 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
|
|
|
|
public function uniqueId(): string
|
|
|
|
{
|
|
|
|
return $this->server->uuid;
|
|
|
|
}
|
|
|
|
public function __construct(public Server $server)
|
2023-07-07 13:50:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
public function handle(): void
|
|
|
|
{
|
2023-09-29 12:26:42 +00:00
|
|
|
$queuedCount = 0;
|
|
|
|
$this->server->applications()->each(function ($application) use ($queuedCount) {
|
|
|
|
$count = data_get($application->deployments(), 'count', 0);
|
|
|
|
$queuedCount += $count;
|
|
|
|
});
|
|
|
|
if ($queuedCount > 0) {
|
2023-09-04 14:59:02 +00:00
|
|
|
ray('DockerCleanupJob: ApplicationDeploymentQueue is not empty, skipping')->color('orange');
|
|
|
|
return;
|
|
|
|
}
|
2023-07-07 13:50:36 +00:00
|
|
|
try {
|
2023-09-29 12:26:42 +00:00
|
|
|
if (!$this->server->isFunctional()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-13 13:16:33 +00:00
|
|
|
$this->dockerRootFilesystem = "/";
|
2023-09-29 12:26:42 +00:00
|
|
|
$this->usageBefore = $this->getFilesystemUsage();
|
|
|
|
if ($this->usageBefore >= $this->server->settings->cleanup_after_percentage) {
|
|
|
|
ray('Cleaning up ' . $this->server->name)->color('orange');
|
|
|
|
instant_remote_process(['docker image prune -af'], $this->server);
|
|
|
|
instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $this->server);
|
|
|
|
instant_remote_process(['docker builder prune -af'], $this->server);
|
|
|
|
$usageAfter = $this->getFilesystemUsage();
|
|
|
|
if ($usageAfter < $this->usageBefore) {
|
|
|
|
ray('Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name)->color('orange');
|
|
|
|
send_internal_notification('DockerCleanupJob done: Saved ' . ($this->usageBefore - $usageAfter) . '% disk space on ' . $this->server->name);
|
2023-08-29 08:00:29 +00:00
|
|
|
} else {
|
2023-09-29 12:26:42 +00:00
|
|
|
ray('DockerCleanupJob failed to save disk space on ' . $this->server->name)->color('orange');
|
2023-07-07 13:50:36 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
} else {
|
|
|
|
ray('No need to clean up ' . $this->server->name)->color('orange');
|
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-08-29 08:00:29 +00:00
|
|
|
ray($e->getMessage())->color('orange');
|
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
|
|
|
|
2023-09-29 12:26:42 +00:00
|
|
|
private function getFilesystemUsage()
|
2023-07-26 11:33:48 +00:00
|
|
|
{
|
2023-09-29 12:26:42 +00:00
|
|
|
return instant_remote_process(["df '{$this->dockerRootFilesystem}'| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $this->server, false);
|
2023-07-07 19:35:29 +00:00
|
|
|
}
|
2023-07-26 11:33:48 +00:00
|
|
|
}
|