Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix retrying on the daemon mode #199

Merged
merged 1 commit into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}