From 65f0509213b0da94368d63840744fc45c3a8d9da Mon Sep 17 00:00:00 2001 From: William David Edwards Date: Wed, 11 Dec 2024 11:17:08 +0100 Subject: [PATCH] Add CPU limits and memory limits to daemons + memory limits to FPM pools --- CHANGELOG.md | 10 ++++++++++ src/Client.php | 2 +- src/Endpoints/Daemons.php | 38 ++++++++++++++++++++++++++++++++++++++ src/Endpoints/FpmPools.php | 33 +++++++++++++++++++++++++++++++++ src/Models/Daemon.php | 30 ++++++++++++++++++++++++++++++ src/Models/FpmPool.php | 15 +++++++++++++++ 6 files changed, 127 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30036551..4d6a677a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). this package and not the Core API. See the changelog of the [Core API](https://core-api.cyberfusion.io/redoc#section/Changelog) for detailed information. +## [1.116.0] + +### Added + +- CPU limits and memory limits to daemons. +- Memory limits to FPM pools. + +Note: new `updatePartial` methods were added to update these attributes, as they can only be updated using the new PATCH endpoints +in the Core API (not the now deprecated PUT endpoints, which the existing `update` methods used). + ## [1.115.0] ### Removed diff --git a/src/Client.php b/src/Client.php index 9df4f684..4cb8fee7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -20,7 +20,7 @@ class Client implements ClientContract private const TIMEOUT = 180; - private const VERSION = '1.115.0'; + private const VERSION = '1.116.0'; private const USER_AGENT = 'cyberfusion-cluster-api-client/' . self::VERSION; diff --git a/src/Endpoints/Daemons.php b/src/Endpoints/Daemons.php index 6d069650..9d932ba4 100644 --- a/src/Endpoints/Daemons.php +++ b/src/Endpoints/Daemons.php @@ -69,6 +69,8 @@ public function create(Daemon $daemon): Response 'command', 'nodes_ids', 'unix_user_id', + 'memory_limit', + 'cpu_limit', ]); $request = (new Request()) @@ -80,6 +82,8 @@ public function create(Daemon $daemon): Response 'command', 'unix_user_id', 'nodes_ids', + 'memory_limit', + 'cpu_limit', ]) ); @@ -139,6 +143,40 @@ public function update(Daemon $daemon): Response ]); } + /** + * @throws RequestException + */ + public function updatePartial(Daemon $daemon): Response + { + $this->validateRequired($daemon, 'update', [ + 'memory_limit', + 'cpu_limit', + ]); + + $request = (new Request()) + ->setMethod(Request::METHOD_PATCH) + ->setUrl(sprintf('fpm-pools/%d', $daemon->getId())) + ->setBody( + $this->filterFields($daemon->toArray(), [ + 'memory_limit', + 'cpu_limit', + ]) + ); + + $response = $this + ->client + ->request($request); + if (!$response->isSuccess()) { + return $response; + } + + $daemon = (new Daemon())->fromArray($response->getData()); + + return $response->setData([ + 'daemon' => $daemon, + ]); + } + /** * @throws RequestException */ diff --git a/src/Endpoints/FpmPools.php b/src/Endpoints/FpmPools.php index c3cbbc39..1e85744b 100644 --- a/src/Endpoints/FpmPools.php +++ b/src/Endpoints/FpmPools.php @@ -85,6 +85,7 @@ public function create(FpmPool $fpmPool): Response 'max_requests', 'process_idle_timeout', 'cpu_limit', + 'memory_limit', 'log_slow_requests_threshold', 'is_namespaced', ]) @@ -151,6 +152,38 @@ public function update(FpmPool $fpmPool): Response ]); } + /** + * @throws RequestException + */ + public function updatePartial(FpmPool $fpmPool): Response + { + $this->validateRequired($fpmPool, 'update', [ + 'memory_limit', + ]); + + $request = (new Request()) + ->setMethod(Request::METHOD_PATCH) + ->setUrl(sprintf('fpm-pools/%d', $fpmPool->getId())) + ->setBody( + $this->filterFields($fpmPool->toArray(), [ + 'memory_limit', + ]) + ); + + $response = $this + ->client + ->request($request); + if (!$response->isSuccess()) { + return $response; + } + + $fpmPool = (new FpmPool())->fromArray($response->getData()); + + return $response->setData([ + 'fpmPool' => $fpmPool, + ]); + } + /** * @throws RequestException */ diff --git a/src/Models/Daemon.php b/src/Models/Daemon.php index c99c5f57..251ca8c9 100644 --- a/src/Models/Daemon.php +++ b/src/Models/Daemon.php @@ -10,6 +10,8 @@ class Daemon extends ClusterModel private string $name; private string $command; private int $unixUserId; + private ?int $memoryLimit; + private ?int $cpuLimit; private array $nodesIds; private ?int $id; private int $clusterId; @@ -62,6 +64,30 @@ public function setUnixUserId(int $unixUserId): self return $this; } + public function getCpuLimit(): ?int + { + return $this->cpuLimit; + } + + public function setCpuLimit(?int $cpuLimit): self + { + $this->cpuLimit = $cpuLimit; + + return $this; + } + + public function getMemoryLimit(): ?int + { + return $this->memoryLimit; + } + + public function setMemoryLimit(?int $memoryLimit): self + { + $this->memoryLimit = $memoryLimit; + + return $this; + } + public function getNodesIds(): array { return $this->nodesIds; @@ -128,6 +154,8 @@ public function fromArray(array $data): self ->setName(Arr::get($data, 'name')) ->setCommand(Arr::get($data, 'command')) ->setUnixUserId(Arr::get($data, 'unix_user_id')) + ->setMemoryLimit(Arr::get($data, 'memory_limit')) + ->setCpuLimit(Arr::get($data, 'cpu_limit')) ->setNodesIds(Arr::get($data, 'nodes_ids')) ->setId(Arr::get($data, 'id')) ->setClusterId(Arr::get($data, 'cluster_id')) @@ -141,6 +169,8 @@ public function toArray(): array 'name' => $this->getName(), 'command' => $this->getCommand(), 'unix_user_id' => $this->getUnixUserId(), + 'memory_limit' => $this->getMemoryLimit(), + 'cpu_limit' => $this->getCpuLimit(), 'nodes_ids' => $this->getNodesIds(), 'id' => $this->getId(), 'cluster_id' => $this->getClusterId(), diff --git a/src/Models/FpmPool.php b/src/Models/FpmPool.php index deed441a..2162a7e6 100644 --- a/src/Models/FpmPool.php +++ b/src/Models/FpmPool.php @@ -14,6 +14,7 @@ class FpmPool extends ClusterModel private int $maxRequests = 1000; private int $processIdleTimeout = 10; private ?int $cpuLimit = null; + private ?int $memoryLimit = null; private ?int $logShowRequestsThreshold = null; private bool $isNamespaced = false; private ?int $id = null; @@ -110,6 +111,18 @@ public function setCpuLimit(?int $cpuLimit): self return $this; } + public function getMemoryLimit(): ?int + { + return $this->memoryLimit; + } + + public function setMemoryLimit(?int $memoryLimit): self + { + $this->memoryLimit = $memoryLimit; + + return $this; + } + public function getLogShowRequestsThreshold(): ?int { return $this->logShowRequestsThreshold; @@ -192,6 +205,7 @@ public function fromArray(array $data): self ->setMaxRequests(Arr::get($data, 'max_requests')) ->setProcessIdleTimeout(Arr::get($data, 'process_idle_timeout')) ->setCpuLimit(Arr::get($data, 'cpu_limit')) + ->setMemoryLimit(Arr::get($data, 'memory_limit')) ->setLogShowRequestsThreshold(Arr::get($data, 'log_slow_requests_threshold')) ->setIsNamespaced((bool)Arr::get($data, 'is_namespaced')) ->setId(Arr::get($data, 'id')) @@ -210,6 +224,7 @@ public function toArray(): array 'max_requests' => $this->getMaxRequests(), 'process_idle_timeout' => $this->getProcessIdleTimeout(), 'cpu_limit' => $this->getCpuLimit(), + 'memory_limit' => $this->getMemoryLimit(), 'log_slow_requests_threshold' => $this->getLogShowRequestsThreshold(), 'is_namespaced' => $this->isNamespaced(), 'id' => $this->getId(),