update dockercleanupjob

This commit is contained in:
Andras Bacsai 2023-08-29 10:00:29 +02:00
parent 6cb3d7167f
commit d04d41bc23
2 changed files with 23 additions and 22 deletions

View File

@ -10,6 +10,7 @@ class SubscriptionValid
{
public function handle(Request $request, Closure $next): Response
{
ray()->showQueries()->color('orange');
if (isInstanceAdmin()) {
return $next($request);
}

View File

@ -15,21 +15,15 @@ class DockerCleanupJob implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 500;
/**
* Create a new job instance.
*/
public ?string $dockerRootFilesystem = null;
public ?int $usageBefore = null;
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
try {
ray()->showQueries()->color('orange');
$servers = Server::all();
foreach ($servers as $server) {
if (
@ -38,33 +32,39 @@ public function handle(): void
continue;
}
if (isDev()) {
$docker_root_filesystem = "/";
$this->dockerRootFilesystem = "/";
} else {
$docker_root_filesystem = instant_remote_process(['stat --printf=%m $(docker info --format "{{json .DockerRootDir}}" |sed \'s/"//g\')'], $server);
$this->dockerRootFilesystem = instant_remote_process(["stat --printf=%m $(docker info --format '{{json .DockerRootDir}}'' |sed 's/\"//g')"], $server, false);
}
$disk_percentage_before = $this->get_disk_usage($server, $docker_root_filesystem);
if ($disk_percentage_before >= $server->settings->cleanup_after_percentage) {
if (!$this->dockerRootFilesystem) {
continue;
}
$this->usageBefore = $this->getFilesystemUsage($server);
if ($this->usageBefore >= $server->settings->cleanup_after_percentage) {
ray('Cleaning up ' . $server->name)->color('orange');
instant_remote_process(['docker image prune -af'], $server);
instant_remote_process(['docker container prune -f --filter "label=coolify.managed=true"'], $server);
instant_remote_process(['docker builder prune -af'], $server);
$disk_percentage_after = $this->get_disk_usage($server, $docker_root_filesystem);
if ($disk_percentage_after < $disk_percentage_before) {
ray('Saved ' . ($disk_percentage_before - $disk_percentage_after) . '% disk space on ' . $server->name);
$usageAfter = $this->getFilesystemUsage($server);
if ($usageAfter < $this->usageBefore) {
ray('Saved ' . ( $this->usageBefore - $usageAfter) . '% disk space on ' . $server->name)->color('orange');
send_internal_notification('DockerCleanupJob done: Saved ' . ( $this->usageBefore - $usageAfter) . '% disk space on ' . $server->name);
}else {
ray('DockerCleanupJob failed to save disk space on ' . $server->name)->color('orange');
}
} else {
ray('No need to clean up ' . $server->name)->color('orange');
}
}
} catch (\Exception $e) {
send_internal_notification('DockerCleanupJob failed with: ' . $e->getMessage());
ray($e->getMessage());
ray($e->getMessage())->color('orange');
throw $e;
}
}
private function get_disk_usage(Server $server, string $docker_root_filesystem)
private function getFilesystemUsage(Server $server)
{
$disk_usage = json_decode(instant_remote_process(['df -hP | awk \'BEGIN {printf"{\\"disks\\":["}{if($1=="Filesystem")next;if(a)printf",";printf"{\\"mount\\":\\""$6"\\",\\"size\\":\\""$2"\\",\\"used\\":\\""$3"\\",\\"avail\\":\\""$4"\\",\\"use%\\":\\""$5"\\"}";a++;}END{print"]}";}\''], $server), true);
$mount_point = collect(data_get($disk_usage, 'disks'))->where('mount', $docker_root_filesystem)->first();
ray($mount_point);
return Str::of(data_get($mount_point, 'use%'))->trim()->replace('%', '')->value();
return instant_remote_process(["df '{$this->dockerRootFilesystem}'| tail -1 | awk '{ print $5}' | sed 's/%//g'"], $server, false);
}
}