-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
UnsetContextInTaskWorkerListener
which can be used to unset c…
…onnection context when using non-coroutine task worker. Co-authored-by: Deeka Wong <[email protected]> Co-authored-by: 李铭昕 <[email protected]>
- Loading branch information
1 parent
9647e9e
commit 3c5eb95
Showing
1 changed file
with
53 additions
and
0 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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\DbConnection\Listener; | ||
|
||
use Hyperf\Context\Context; | ||
use Hyperf\Contract\ConfigInterface; | ||
use Hyperf\Database\ConnectionResolverInterface; | ||
use Hyperf\Event\Contract\ListenerInterface; | ||
use Hyperf\Framework\Event\BeforeWorkerStart; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class UnsetContextInTaskWorkerListener implements ListenerInterface | ||
{ | ||
public function __construct( | ||
private ContainerInterface $container, | ||
private ConfigInterface $config | ||
) { | ||
} | ||
|
||
public function listen(): array | ||
{ | ||
return [ | ||
BeforeWorkerStart::class, | ||
]; | ||
} | ||
|
||
/** | ||
* @param BeforeWorkerStart $event | ||
*/ | ||
public function process(object $event): void | ||
{ | ||
if (! $event instanceof BeforeWorkerStart || ! $event->server->taskworker) { | ||
return; | ||
} | ||
|
||
$connectionResolver = $this->container->get(ConnectionResolverInterface::class); | ||
$databases = (array) $this->config->get('databases', []); | ||
|
||
foreach ($databases as $name => $_) { | ||
$contextKey = (fn () => $this->getContextKey($name))->call($connectionResolver); | ||
Context::destroy($contextKey); | ||
} | ||
} | ||
} |