3 Commits

Author SHA1 Message Date
5ab94e7b2d Reset memory 2025-07-12 10:33:50 -04:00
e037c86e6f Run one job at a time. 2025-07-12 10:31:48 -04:00
595a71d7dd System info 2025-07-12 10:30:32 -04:00
4 changed files with 115 additions and 3 deletions

View File

@ -7,5 +7,5 @@
0 2 * * * /code/bin/incoviba money:uf >> /logs/commands 2>&1
0 2 * * * /code/bin/incoviba money:uf:update >> /logs/commands 2>&1
0 2 1 * * /code/bin/incoviba money:ipc >> /logs/commands 2>&1
*/2 * * * * /code/bin/incoviba queue >> /logs/commands 2>&1
*/1 * * * * /code/bin/incoviba queue >> /logs/commands 2>&1
0 3 * * * /code/bin/incoviba external:services >> /logs/commands 2>&1

View File

@ -37,6 +37,8 @@ class BaseLoop extends Console\Command\Command
foreach ($commands as $command) {
$this->runCommand($input, $output, $command);
}
unset($commands);
memory_reset_peak_usage();
$this->waitNextTimeout($output, $start);
}
return self::SUCCESS;

View File

@ -44,10 +44,11 @@ class Queue extends Command
}
$io->writeln("Found {$jobCount} jobs to run");
$result = $this->runJobs($io, $jobs);
$result = $this->runJob($jobs[0]);
/*$result = $this->runJobs($io, $jobs);
foreach ($this->outputs as $output) {
$this->sections['bottom']->writeln($output);
}
}*/
return $result;
}
@ -109,4 +110,28 @@ class Queue extends Command
}
return self::SUCCESS;
}
protected function runJob(int $jobId): int
{
$baseCommand = "{$this->baseCommand} jobs:run";
$command = "{$baseCommand} {$jobId}";
try {
exec($command, $output, $resultCode);
$this->outputs []= $output;
} catch (Throwable $exception) {
$this->logger->error("Failed to run command", [
'command' => $command,
'exception' => $exception
]);
return self::FAILURE;
}
if ($resultCode !== 0) {
$this->logger->error("Failed to run command", [
'command' => $command,
'result_code' => $resultCode
]);
return self::FAILURE;
} else {
return self::SUCCESS;
}
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Incoviba\Service;
class SystemInfo
{
public function getAllInfo(): array
{
return [
'memory' => [
'usage' => $this->getMemoryUsage(),
'peak' => $this->getPeakMemoryUsage()
],
'cpu' => [
'usage' => $this->getCpuUsage(),
'last_15minutes' => $this->getCpuUsageLast15minutes(),
'cores' => $this->getCpuCores()
]
];
}
public function get(string $name): int|null|float
{
return match ($name) {
'memory' => $this->getMemoryUsage(),
'peak_memory' => $this->getPeakMemoryUsage(),
'cpu' => $this->getCpuUsage(),
'cpu_last_15minutes' => $this->getCpuUsageLast15minutes(),
'cpu_cores' => $this->getCpuCores(),
default => null
};
}
public function getMemoryUsage(): float
{
return memory_get_usage(true);
}
public function getPeakMemoryUsage(): float
{
return memory_get_peak_usage(true);
}
public function getCpuUsage(): float
{
return $this->getCpuLoad()[0];
}
public function getCpuUsageLast15minutes(): float
{
return $this->getCpuLoad()[1];
}
protected array $cpuLoad;
protected function getCpuLoad(): array
{
if (isset($this->cpuLoad)) {
$load = sys_getloadavg();
$cores = $this->getCpuCores();
array_walk($load, function (&$value) use ($cores) {
$value = $value / $cores;
});
$this->cpuLoad = $load;
unset($load);
}
return $this->cpuLoad;
}
protected function getCpuCores(): int
{
$cpu_cores = 1;
if (is_file('/proc/cpuinfo')) {
$cpuinfo = file('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuinfo, $matches);
$cpu_cores = count($matches[0]);
}
return $cpu_cores;
}
public function formatMemoryUsage(float $usage, string $unit = 'MB'): string
{
$sizeFactor = match ($unit) {
'MB' => 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
default => 1
};
return number_format($usage / $sizeFactor, 2) . " {$unit}";
}
public function formatCpuLoad(float $load): string
{
return number_format($load * 100, 2) . '%';
}
}