-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from reliforp/auto-context-recovering
Automatically respawn workers on the daemon mode
- Loading branch information
Showing
13 changed files
with
689 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Inspector\Daemon; | ||
|
||
use Amp\Parallel\Context\ContextException; | ||
use Reli\Lib\Amphp\ContextInterface; | ||
use Reli\Lib\Amphp\MessageProtocolInterface; | ||
use Reli\Lib\Log\Log; | ||
|
||
/** | ||
* @template T of MessageProtocolInterface | ||
* @implements AutoContextRecoveringInterface<T> | ||
*/ | ||
final class AutoContextRecovering implements AutoContextRecoveringInterface | ||
{ | ||
private const RETRY_COUNT = 10; | ||
|
||
/** @var ContextInterface<T>|null */ | ||
private ?ContextInterface $context = null; | ||
|
||
private ?\Closure $on_recover_callback = null; | ||
|
||
/** | ||
* @param \Closure():ContextInterface<T> $context_factory | ||
*/ | ||
public function __construct( | ||
private readonly \Closure $context_factory, | ||
) { | ||
} | ||
|
||
public function recreateContext(): void | ||
{ | ||
if (!is_null($this->context) and $this->context->isRunning()) { | ||
$this->context->stop(); | ||
} | ||
$this->context = null; | ||
} | ||
|
||
public function getContext(): ContextInterface | ||
{ | ||
if (is_null($this->context)) { | ||
$this->context = $this->context_factory->__invoke(); | ||
if (!is_null($this->on_recover_callback)) { | ||
$this->on_recover_callback->__invoke(); | ||
} | ||
} | ||
return $this->context; | ||
} | ||
|
||
public function withAutoRecover( | ||
\Closure $callback, | ||
string $log_message_on_retry, | ||
): mixed { | ||
for ($i = 0; $i < self::RETRY_COUNT; $i++) { | ||
try { | ||
return $callback($this->getContext()->getProtocol()); | ||
} catch (ContextException $e) { | ||
Log::info( | ||
$log_message_on_retry, | ||
[ | ||
'exception' => $e, | ||
] | ||
); | ||
$this->recreateContext(); | ||
} | ||
} | ||
assert($e instanceof \Throwable); | ||
throw $e; | ||
} | ||
|
||
public function onRecover(\Closure $callback): void | ||
{ | ||
$this->on_recover_callback = $callback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Inspector\Daemon; | ||
|
||
use Reli\Lib\Amphp\ContextInterface; | ||
use Reli\Lib\Amphp\MessageProtocolInterface; | ||
|
||
/** @template T of MessageProtocolInterface */ | ||
interface AutoContextRecoveringInterface | ||
{ | ||
public function recreateContext(): void; | ||
|
||
/** @return ContextInterface<T> */ | ||
public function getContext(): ContextInterface; | ||
|
||
/** | ||
* @template TReturn | ||
* @param \Closure(T):TReturn $callback | ||
* @return TReturn | ||
*/ | ||
public function withAutoRecover( | ||
\Closure $callback, | ||
string $log_message_on_retry, | ||
): mixed; | ||
|
||
/** @param \Closure():void $callback */ | ||
public function onRecover(\Closure $callback): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.