Skip to content

Commit

Permalink
Merge pull request #199 from reliforp/fix-retry-on-daemon
Browse files Browse the repository at this point in the history
fix retrying on the daemon mode
  • Loading branch information
sj-i committed Apr 30, 2022
2 parents 8b7bff5 + 639faab commit 29d9ce7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, class-string<Exception>> $exception_names
* @param array<int, class-string<Throwable>> $exception_names
*/
public function __construct(
private int $max_retry,
Expand All @@ -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;
}
}
23 changes: 19 additions & 4 deletions src/Lib/Loop/LoopMiddleware/RetryOnExceptionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, class-string<Exception>> $exception_names
* @param array<int, class-string<Throwable>> $exception_names
*/
public function __construct(
private int $max_retry,
Expand All @@ -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;
}
}

0 comments on commit 29d9ce7

Please sign in to comment.