lasthourcloud/app/Jobs/DockerCleanupJob.php

92 lines
3.5 KiB
PHP
Raw Normal View History

2023-07-07 13:50:36 +00:00
<?php
namespace App\Jobs;
2023-09-04 14:59:02 +00:00
use App\Models\ApplicationDeploymentQueue;
2023-07-07 13:50:36 +00:00
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;
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;
public $timeout = 1000;
2023-08-29 08:00:29 +00:00
public ?string $dockerRootFilesystem = null;
public ?int $usageBefore = null;
public function middleware(): array
{
return [(new WithoutOverlapping($this->server->uuid))->dontRelease()];
}
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
{
$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 {
if (!$this->server->isFunctional()) {
return;
}
if (isDev()) {
$this->dockerRootFilesystem = "/";
} else {
$this->dockerRootFilesystem = instant_remote_process(
[
"stat --printf=%m $(docker info --format '{{json .DockerRootDir}}'' |sed 's/\"//g')"
],
$this->server,
false
);
}
if (!$this->dockerRootFilesystem) {
return;
}
$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 {
ray('DockerCleanupJob failed to save disk space on ' . $this->server->name)->color('orange');
2023-07-07 13:50:36 +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
private function getFilesystemUsage()
2023-07-26 11:33:48 +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
}