Make process methods make more sense

This commit is contained in:
James Gilliland 2022-03-04 22:23:04 -06:00
parent 7789388cf2
commit e8724f4513
3 changed files with 27 additions and 33 deletions

View file

@ -11,6 +11,7 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
final class Monitor extends Command
{
@ -39,7 +40,7 @@ final class Monitor extends Command
register_shutdown_function(function () { $this->logger->info('Stopping 5268AC ping monitor ...'); });
while ($input->isInteractive()) {
if ($this->ping()) {
if (!$this->ping()) {
$this->ngControl->ngRmHook();
}
elseif (!$this->ngControl->ngIsConnected()) {
@ -50,12 +51,15 @@ final class Monitor extends Command
return Command::SUCCESS;
}
private function ping(): int
private function ping(): bool
{
$process = proc_open('/sbin/ping -t2 -q -c1 ' . $this->config->getPingHost(), [], $pipes);
if ($process) {
return proc_close($process);
}
return 1;
$process = new Process([
'/bin/ping',
'-t2',
'-q',
'-c1',
$this->config->getPingHost()
]);
return (bool) $process->run();
}
}

View file

@ -24,7 +24,7 @@ final class Startup extends Command
{
// @todo Inject these. Maybe with a factory or container.
$this->config = new Config('', '', '');
$this->logger = new Logger('pfatt-5268AC');
$this->logger = new Logger('pfatt');
$this->ngControl = new NgController($this->logger);
}

View file

@ -6,6 +6,7 @@ namespace Pfatt;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;
class NgController implements LoggerAwareInterface
{
@ -19,7 +20,7 @@ class NgController implements LoggerAwareInterface
/**
* Use ngctl to check connection.
*/
public function ngIsConnected(): int
public function ngIsConnected(): bool
{
return $this->ngCtlRun('show laneapfilter:eapout');
}
@ -49,16 +50,17 @@ class NgController implements LoggerAwareInterface
/**
* Use ngctl to setup virtual connection.
*
* @todo are all the ifs reversed?
*
* @param string $ont
* ONT network connection interface.
* @param string $rg
* Residential Gateway network connection interface.
* @return int|void
* ...
* @todo are all the ifs reversed?
*
*/
public function createNodes(string $ont, string $rg, string $rgMac) {
public function createNodes(string $ont, string $rg, string $rgMac)
{
$this->logger->info('Building netgraph nodes.');
$this->logger->info(' creating ng_one2many...');
@ -190,31 +192,19 @@ class NgController implements LoggerAwareInterface
* @param string $arg
* Additional arguments to pass to ngctl.
*
* @return int
* @return bool
* Process return code.
*/
private function ngCtlRun(string $arg): int
private function ngCtlRun(string $arg): bool
{
$descriptorspec = [
// 0 => ['pipe', 'r'],
// 1 => ['pipe', 'w'],
// 2 => ['pipe', 'r'],
];
$cwd = '/tmp';
$env = [];
$process = proc_open(
'/usr/sbin/ngctl ' . $arg,
$descriptorspec,
$pipes,
$cwd,
$env
$process = new Process(
[
'/usr/sbin/ngctl',
$arg
],
'/tmp'
);
if ($process) {
return proc_close($process);
}
return 1;
return (bool)$process->run();
}
/**