Skip to content

Commit

Permalink
change CommandLineEnumerator to use CatFileReader for now
Browse files Browse the repository at this point in the history
somehow sometimes opening a file in procfs is blocked, this workarounds it
  • Loading branch information
sj-i committed Jul 1, 2020
1 parent f21da20 commit 02fc9e5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/Lib/Process/ProcFileSystem/CommandLineEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
namespace PhpProfiler\Lib\Process\ProcFileSystem;

use IteratorAggregate;
use PhpProfiler\Lib\File\FileReaderInterface;

final class CommandLineEnumerator implements IteratorAggregate
{
private FileReaderInterface $reader;

public function __construct(FileReaderInterface $reader)
{
$this->reader = $reader;
}

/**
* @return \Generator<int, string>
*/
Expand All @@ -27,7 +35,12 @@ public function getIterator()
* @var \SplFileInfo $item
*/
foreach (new \GlobIterator('/proc/*/cmdline') as $full_path => $item) {
yield (int)basename($item->getPath()) => file_get_contents($full_path);
$command_line = $this->reader->readAll($full_path);
if ($command_line === '') {
continue;
}

yield (int)basename($item->getPath()) => $command_line;
}
}
}
14 changes: 13 additions & 1 deletion src/Lib/Process/Search/ProcessSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@

namespace PhpProfiler\Lib\Process\Search;

use PhpProfiler\Lib\File\CatFileReader;
use PhpProfiler\Lib\File\FileReaderInterface;
use PhpProfiler\Lib\Process\ProcFileSystem\CommandLineEnumerator;

final class ProcessSearcher
{
/**
* @var FileReaderInterface
*/
private FileReaderInterface $file_reader;

public function __construct(FileReaderInterface $file_reader)
{
$this->file_reader = $file_reader;
}

/**
* @param string $regex
* @return int[]
Expand All @@ -25,7 +37,7 @@ public function searchByRegex(string $regex): array
{
$result = [];

foreach (new CommandLineEnumerator() as $pid => $command_line) {
foreach (new CommandLineEnumerator($this->file_reader) as $pid => $command_line) {
if (preg_match($regex, $command_line)) {
$result[] = $pid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace PhpProfiler\Lib\Process\ProcFileSystem;

use PhpProfiler\Lib\File\CatFileReader;
use PHPUnit\Framework\TestCase;

class CommandLineEnumeratorTest extends TestCase
Expand Down Expand Up @@ -52,7 +53,7 @@ public function testGetIterator()
$child_pid = $child_status['pid'];

$target = null;
foreach (new CommandLineEnumerator() as $pid => $command_line) {
foreach (new CommandLineEnumerator(new CatFileReader()) as $pid => $command_line) {
if ($pid === $child_pid) {
$target = $command_line;
break;
Expand Down
3 changes: 2 additions & 1 deletion tests/Lib/Process/Search/ProcessSearcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace PhpProfiler\Lib\Process\Search;

use PhpProfiler\Lib\File\CatFileReader;
use PHPUnit\Framework\TestCase;

class ProcessSearcherTest extends TestCase
Expand Down Expand Up @@ -51,7 +52,7 @@ public function testSearch()
$child_status = proc_get_status($this->child);
$child_pid = $child_status['pid'];

$searcher = new ProcessSearcher();
$searcher = new ProcessSearcher(new CatFileReader());
$this->assertSame(
[$child_pid],
$searcher->searchByRegex('/test_ProcessSearcherTest/')
Expand Down

0 comments on commit 02fc9e5

Please sign in to comment.