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\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger; use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
final class Monitor extends Command 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 ...'); }); register_shutdown_function(function () { $this->logger->info('Stopping 5268AC ping monitor ...'); });
while ($input->isInteractive()) { while ($input->isInteractive()) {
if ($this->ping()) { if (!$this->ping()) {
$this->ngControl->ngRmHook(); $this->ngControl->ngRmHook();
} }
elseif (!$this->ngControl->ngIsConnected()) { elseif (!$this->ngControl->ngIsConnected()) {
@ -50,12 +51,15 @@ final class Monitor extends Command
return Command::SUCCESS; return Command::SUCCESS;
} }
private function ping(): int private function ping(): bool
{ {
$process = proc_open('/sbin/ping -t2 -q -c1 ' . $this->config->getPingHost(), [], $pipes); $process = new Process([
if ($process) { '/bin/ping',
return proc_close($process); '-t2',
} '-q',
return 1; '-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. // @todo Inject these. Maybe with a factory or container.
$this->config = new Config('', '', ''); $this->config = new Config('', '', '');
$this->logger = new Logger('pfatt-5268AC'); $this->logger = new Logger('pfatt');
$this->ngControl = new NgController($this->logger); $this->ngControl = new NgController($this->logger);
} }

View file

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