feat: disk cleanup threshold by server

This commit is contained in:
Andras Bacsai 2023-07-07 21:07:42 +02:00
parent 712151aa80
commit 18d26904e2
6 changed files with 45 additions and 12 deletions

View File

@ -14,12 +14,11 @@ protected function schedule(Schedule $schedule): void
{
if (isDev()) {
$schedule->command('horizon:snapshot')->everyMinute();
// $schedule->job(new DockerCleanupJob)->everyMinute();
// $schedule->job(new DockerCleanupJob)->everyOddHour();
// $schedule->job(new InstanceAutoUpdateJob(true))->everyMinute();
} else {
$schedule->command('horizon:snapshot')->everyFiveMinutes();
$schedule->job(new DockerCleanupJob)->everyFiveMinutes();
$schedule->job(new DockerCleanupJob)->everyTenMinutes();
$schedule->job(new ProxyCheckJob)->everyFiveMinutes();
$schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes();
}
@ -30,4 +29,4 @@ protected function commands(): void
require base_path('routes/console.php');
}
}
}

View File

@ -12,6 +12,7 @@ class Form extends Component
public $uptime;
public $dockerVersion;
public string|null $wildcard_domain = null;
public int $cleanup_after_percentage;
protected $rules = [
'server.name' => 'required|min:6',
@ -35,6 +36,7 @@ class Form extends Component
public function mount()
{
$this->wildcard_domain = $this->server->settings->wildcard_domain;
$this->cleanup_after_percentage = $this->server->settings->cleanup_after_percentage;
}
public function installDocker()
{
@ -91,8 +93,9 @@ public function submit()
// return;
// }
$this->server->settings->wildcard_domain = $this->wildcard_domain;
$this->server->settings->cleanup_after_percentage = $this->cleanup_after_percentage;
$this->server->settings->save();
$this->server->save();
$this->emit('success', 'Server updated successfully.');
}
}
}

View File

@ -38,15 +38,14 @@ public function handle(): void
}
$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();
if (Str::of(data_get($mount_point, 'use%'))->trim()->replace('%', '')->value() > 60) {
continue;
if (Str::of(data_get($mount_point, 'use%'))->trim()->replace('%', '')->value() >= $server->settings->cleanup_after_percentage) {
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);
}
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);
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}
}

View File

@ -27,4 +27,4 @@ public function down(): void
$table->dropColumn('current_process_id');
});
}
};
};

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('server_settings', function (Blueprint $table) {
$table->integer('cleanup_after_percentage')->default(80);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('server_settings', function (Blueprint $table) {
$table->dropColumn('cleanup_after_percentage');
});
}
};

View File

@ -33,6 +33,10 @@
</div>
</div>
</div>
<h3 class="py-4">Settings</h3>
<div class="flex items-center w-64 gap-2">
<x-forms.input id="cleanup_after_percentage" label="Disk Cleanup threshold (%)" required helper="Disk cleanup job will be executed if disk usage is more than this number."/>
</div>
<h3 class="py-4">Actions</h3>
@if ($server->settings->is_reachable)
<div class="flex items-center gap-2">