41 lines
1020 B
PHP
41 lines
1020 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\System;
|
|
|
|
use App\Models\UserResourceLimit;
|
|
use App\Services\Agent\AgentClient;
|
|
use RuntimeException;
|
|
|
|
class ResourceLimitService
|
|
{
|
|
public function apply(UserResourceLimit $limit): void
|
|
{
|
|
$user = $limit->user;
|
|
if (! $user) {
|
|
throw new RuntimeException('User not found for resource limit.');
|
|
}
|
|
|
|
$agent = new AgentClient;
|
|
$agent->cgroupApplyUserLimits(
|
|
$user->username,
|
|
$limit->cpu_limit_percent ? (int) $limit->cpu_limit_percent : null,
|
|
$limit->memory_limit_mb ? (int) $limit->memory_limit_mb : null,
|
|
$limit->io_limit_mb ? (int) $limit->io_limit_mb : null,
|
|
(bool) $limit->is_active
|
|
);
|
|
}
|
|
|
|
public function clear(UserResourceLimit $limit): void
|
|
{
|
|
$user = $limit->user;
|
|
if (! $user) {
|
|
return;
|
|
}
|
|
|
|
$agent = new AgentClient;
|
|
$agent->cgroupClearUserLimits($user->username);
|
|
}
|
|
}
|