Skip to content

Commit

Permalink
Merge pull request #122 from CyberfusionIO/feature/add-limits
Browse files Browse the repository at this point in the history
Add CPU limits and memory limits to daemons + memory limits to FPM pools
  • Loading branch information
WilliamDEdwards authored Dec 11, 2024
2 parents 1d16f1a + 65f0509 commit 47c08c8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
38 changes: 38 additions & 0 deletions src/Endpoints/Daemons.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function create(Daemon $daemon): Response
'command',
'nodes_ids',
'unix_user_id',
'memory_limit',
'cpu_limit',
]);

$request = (new Request())
Expand All @@ -80,6 +82,8 @@ public function create(Daemon $daemon): Response
'command',
'unix_user_id',
'nodes_ids',
'memory_limit',
'cpu_limit',
])
);

Expand Down Expand Up @@ -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
*/
Expand Down
33 changes: 33 additions & 0 deletions src/Endpoints/FpmPools.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
])
Expand Down Expand Up @@ -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
*/
Expand Down
30 changes: 30 additions & 0 deletions src/Models/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'))
Expand All @@ -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(),
Expand Down
15 changes: 15 additions & 0 deletions src/Models/FpmPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'))
Expand All @@ -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(),
Expand Down

0 comments on commit 47c08c8

Please sign in to comment.