Enhance resource management in Proxmox: implement caching for available resources in Node model; add cache invalidation on server updates and deletions
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace Paymenter\Extensions\Servers\Proxmox\Models;
|
namespace Paymenter\Extensions\Servers\Proxmox\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class Node extends Model
|
class Node extends Model
|
||||||
{
|
{
|
||||||
@@ -56,7 +57,8 @@ class Node extends Model
|
|||||||
|
|
||||||
public function resourcesAvailable()
|
public function resourcesAvailable()
|
||||||
{
|
{
|
||||||
$servers = $this->servers()->with('service.product')->get();
|
return Cache::remember('proxmox-node-resources-' . $this->id, 30, function () {
|
||||||
|
$servers = $this->servers()->with('service.product.settings')->get();
|
||||||
|
|
||||||
$totalMemory = $this->memory;
|
$totalMemory = $this->memory;
|
||||||
$totalDisk = $this->disk;
|
$totalDisk = $this->disk;
|
||||||
@@ -65,9 +67,10 @@ class Node extends Model
|
|||||||
$usedDisk = 0;
|
$usedDisk = 0;
|
||||||
$usedCpu = 0;
|
$usedCpu = 0;
|
||||||
foreach ($servers as $server) {
|
foreach ($servers as $server) {
|
||||||
$usedMemory += (int) ($server->service->product->settings()->where('key', 'memory')->first()?->value ?? 0);
|
$productSettings = $server->service->product->settings->keyBy('key');
|
||||||
$usedDisk += (int) ($server->service->product->settings()->where('key', 'disk')->first()?->value ?? 0);
|
$usedMemory += (int) ($productSettings->get('memory')?->value ?? 0);
|
||||||
$usedCpu += (int) ($server->service->product->settings()->where('key', 'cpu')->first()?->value ?? 0);
|
$usedDisk += (int) ($productSettings->get('disk')?->value ?? 0);
|
||||||
|
$usedCpu += (int) ($productSettings->get('cpu')?->value ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -75,6 +78,12 @@ class Node extends Model
|
|||||||
'disk' => $totalDisk == 0 ? null : $totalDisk - $usedDisk,
|
'disk' => $totalDisk == 0 ? null : $totalDisk - $usedDisk,
|
||||||
'cpu' => $totalCpu == 0 ? null : $totalCpu - $usedCpu,
|
'cpu' => $totalCpu == 0 ? null : $totalCpu - $usedCpu,
|
||||||
];
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function forgetResourceCache(): void
|
||||||
|
{
|
||||||
|
Cache::forget('proxmox-node-resources-' . $this->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canStoreServer($memory, $disk, $cpu)
|
public function canStoreServer($memory, $disk, $cpu)
|
||||||
|
|||||||
@@ -1040,6 +1040,7 @@ class Proxmox extends ClassServer
|
|||||||
$server->hostname = $properties['hostname'];
|
$server->hostname = $properties['hostname'];
|
||||||
$server->password = $password;
|
$server->password = $password;
|
||||||
$server->save();
|
$server->save();
|
||||||
|
$node->forgetResourceCache();
|
||||||
|
|
||||||
// Update the IPs
|
// Update the IPs
|
||||||
foreach ($ipv4->ips as $ip) {
|
foreach ($ipv4->ips as $ip) {
|
||||||
@@ -1162,7 +1163,9 @@ class Proxmox extends ClassServer
|
|||||||
// Release IP addresses so they can be reallocated
|
// Release IP addresses so they can be reallocated
|
||||||
$server->ipAddresses()->update(['server_id' => null]);
|
$server->ipAddresses()->update(['server_id' => null]);
|
||||||
// Delete server
|
// Delete server
|
||||||
|
$node = $server->node;
|
||||||
$server->delete();
|
$server->delete();
|
||||||
|
$node->forgetResourceCache();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1193,6 +1196,7 @@ class Proxmox extends ClassServer
|
|||||||
} else {
|
} else {
|
||||||
$this->syncServerSettings($server, $settings, null);
|
$this->syncServerSettings($server, $settings, null);
|
||||||
}
|
}
|
||||||
|
$server->node->forgetResourceCache();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user