From a276d29173f895e2ecf1da251225d1be97eb9e4e Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 16 Apr 2024 15:07:00 +0400 Subject: [PATCH] Handle FS error when profiler files are read --- src/Info.php | 2 +- src/Service/FilesObserver.php | 2 +- src/Service/FilesObserver/Filter/XHProf.php | 1 + src/Service/FilesObserver/Handler.php | 28 +++++++++++++-------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Info.php b/src/Info.php index b5f9104b..dedfa639 100644 --- a/src/Info.php +++ b/src/Info.php @@ -10,7 +10,7 @@ class Info { public const NAME = 'Buggregator Trap'; - public const VERSION = '1.4.6'; + public const VERSION = '1.4.7'; public const LOGO_CLI_COLOR = <<fibers[] = new Fiber(function () use ($config) { - foreach (Handler::generate($config) as $frame) { + foreach (Handler::generate($config, $this->logger) as $frame) { $this->propagateFrame($frame); } }); diff --git a/src/Service/FilesObserver/Filter/XHProf.php b/src/Service/FilesObserver/Filter/XHProf.php index ae449192..b0771bc0 100644 --- a/src/Service/FilesObserver/Filter/XHProf.php +++ b/src/Service/FilesObserver/Filter/XHProf.php @@ -27,6 +27,7 @@ public function convert(FileInfo $file): \Traversable 'filename' => $file->getName(), ]; + /** @psalm-suppress MixedArgumentTypeCoercion */ yield new ProfilerFrame( ProfilerFrame\Payload::new( type: ProfilerFrame\Type::XHProf, diff --git a/src/Service/FilesObserver/Handler.php b/src/Service/FilesObserver/Handler.php index 36258bcd..85258cde 100644 --- a/src/Service/FilesObserver/Handler.php +++ b/src/Service/FilesObserver/Handler.php @@ -5,6 +5,7 @@ namespace Buggregator\Trap\Service\FilesObserver; use Buggregator\Trap\Config\FilesObserver as Config; +use Buggregator\Trap\Logger; use Buggregator\Trap\Proto\Frame; use Buggregator\Trap\Support\Timer; @@ -21,6 +22,7 @@ final class Handler private function __construct( Config $config, + private readonly Logger $logger, ) { $this->path = $config->path; $this->timer = new Timer($config->interval); @@ -30,9 +32,9 @@ private function __construct( /** * @return \Generator */ - public static function generate(Config $config): \Generator + public static function generate(Config $config, Logger $logger): \Generator { - $self = new self($config); + $self = new self($config, $logger); do { foreach ($self->syncFiles() as $info) { yield from $self->converter->convert($info); @@ -71,16 +73,22 @@ private function syncFiles(): array */ private function getFiles(): \Traversable { - /** @var \Iterator<\SplFileInfo> $iterator */ - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($this->path, \RecursiveDirectoryIterator::SKIP_DOTS), - \RecursiveIteratorIterator::SELF_FIRST, - ); + try { + /** @var \Iterator<\SplFileInfo> $iterator */ + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($this->path, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::SELF_FIRST, + ); - foreach ($iterator as $fileInfo) { - if ($fileInfo->isFile() && $this->converter->validate($info = FileInfo::fromSplFileInfo($fileInfo))) { - yield $info; + foreach ($iterator as $fileInfo) { + if ($fileInfo->isFile() && $this->converter->validate($info = FileInfo::fromSplFileInfo($fileInfo))) { + yield $info; + } } + } catch (\Throwable $e) { + $this->logger->info('Failed to read files from path `%s`', $this->path); + $this->logger->exception($e); + return []; } } }