61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\Agent\AgentClient;
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
|
|
class JabaliSyncCgroups extends Command
|
|
{
|
|
public function __construct(private AgentClient $agent)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'jabali:sync-cgroups {--user=}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Sync user processes into Jabali cgroups';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$user = (string) ($this->option('user') ?? '');
|
|
|
|
try {
|
|
$result = $user !== ''
|
|
? $this->agent->cgroupSyncUserProcesses($user)
|
|
: $this->agent->cgroupSyncAllProcesses();
|
|
} catch (Exception $e) {
|
|
$this->error($e->getMessage());
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
if (! ($result['success'] ?? false)) {
|
|
$this->error($result['error'] ?? 'Unknown error');
|
|
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$moved = (int) ($result['moved'] ?? 0);
|
|
$this->info("Synced cgroups, moved {$moved} process(es).");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|