From 639faabd751579a20ab4172f1d6f81a6555ce8aa Mon Sep 17 00:00:00 2001 From: sji Date: Sat, 30 Apr 2022 17:20:49 +0900 Subject: [PATCH] fix retrying on the daemon mode also align logging to the trace mode --- .../RetryOnExceptionMiddlewareAsync.php | 31 +++++++++++++++---- .../RetryOnExceptionMiddleware.php | 23 +++++++++++--- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Lib/Loop/AsyncLoopMiddleware/RetryOnExceptionMiddlewareAsync.php b/src/Lib/Loop/AsyncLoopMiddleware/RetryOnExceptionMiddlewareAsync.php index 9845bbf7..14de7393 100644 --- a/src/Lib/Loop/AsyncLoopMiddleware/RetryOnExceptionMiddlewareAsync.php +++ b/src/Lib/Loop/AsyncLoopMiddleware/RetryOnExceptionMiddlewareAsync.php @@ -13,16 +13,16 @@ namespace PhpProfiler\Lib\Loop\AsyncLoopMiddleware; -use Exception; use PhpProfiler\Lib\Log\Log; use PhpProfiler\Lib\Loop\AsyncLoopMiddlewareInterface; +use Throwable; final class RetryOnExceptionMiddlewareAsync implements AsyncLoopMiddlewareInterface { private int $current_retry_count = 0; /** - * @param array> $exception_names + * @param array> $exception_names */ public function __construct( private int $max_retry, @@ -36,18 +36,37 @@ public function invoke(): \Generator while ($this->current_retry_count <= $this->max_retry or $this->max_retry === -1) { try { yield from $this->chain->invoke(); - } catch (Exception $e) { + } catch (Throwable $e) { Log::debug($e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTrace() ]); - if (in_array(get_class($e), $this->exception_names, true)) { - $this->current_retry_count++; - continue; + foreach ($this->exception_names as $exception_name) { + /** @psalm-suppress DocblockTypeContradiction */ + if (is_a($e, $exception_name)) { + $this->current_retry_count++; + Log::debug( + $e->getMessage(), + [ + 'retry_count' => $this->current_retry_count, + 'trace' => $e->getTrace() + ] + ); + continue 2; + } } throw $e; } $this->current_retry_count = 0; } + assert(isset($e) and $e instanceof Throwable); + Log::error( + $e->getMessage(), + [ + 'retry_count' => $this->current_retry_count, + 'trace' => $e->getTrace() + ] + ); + throw $e; } } diff --git a/src/Lib/Loop/LoopMiddleware/RetryOnExceptionMiddleware.php b/src/Lib/Loop/LoopMiddleware/RetryOnExceptionMiddleware.php index 485fb35e..cb7948ec 100644 --- a/src/Lib/Loop/LoopMiddleware/RetryOnExceptionMiddleware.php +++ b/src/Lib/Loop/LoopMiddleware/RetryOnExceptionMiddleware.php @@ -13,15 +13,16 @@ namespace PhpProfiler\Lib\Loop\LoopMiddleware; -use Exception; +use PhpProfiler\Lib\Log\Log; use PhpProfiler\Lib\Loop\LoopMiddlewareInterface; +use Throwable; final class RetryOnExceptionMiddleware implements LoopMiddlewareInterface { private int $current_retry_count = 0; /** - * @param array> $exception_names + * @param array> $exception_names */ public function __construct( private int $max_retry, @@ -37,18 +38,32 @@ public function invoke(): bool $result = $this->chain->invoke(); $this->current_retry_count = 0; return $result; - } catch (\Throwable $e) { + } catch (Throwable $e) { foreach ($this->exception_names as $exception_name) { /** @psalm-suppress DocblockTypeContradiction */ if (is_a($e, $exception_name)) { $this->current_retry_count++; + Log::debug( + $e->getMessage(), + [ + 'retry_count' => $this->current_retry_count, + 'trace' => $e->getTrace() + ] + ); continue 2; } } throw $e; } } - assert(isset($e)); + assert(isset($e) and $e instanceof Throwable); + Log::error( + $e->getMessage(), + [ + 'retry_count' => $this->current_retry_count, + 'trace' => $e->getTrace() + ] + ); throw $e; } }