From f9da6dbfaff716f6b537b421d762982ed1c455f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B8=85=E8=A5=BF?= <928200407@qq.com> Date: Thu, 4 Jan 2024 10:42:11 +0800 Subject: [PATCH] Added `UnsetContextInTaskWorkerListener` which can be used to unset connection context when using non-coroutine task worker. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-3.1.md | 4 ++ .../UnsetContextInTaskWorkerListener.php | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/db-connection/src/Listener/UnsetContextInTaskWorkerListener.php diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index d772def9a..3588e2d5e 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -4,6 +4,10 @@ - [#6423](https://github.com/hyperf/hyperf/pull/6423) Fixed bug that the timezone of crontab task cannot work. +## Added + +- [#6431](https://github.com/hyperf/hyperf/pull/6431) Added `UnsetContextInTaskWorkerListener` which can be used to unset connection context when using non-coroutine task worker. + # v3.1.4 - 2023-12-29 ## Fixed diff --git a/src/db-connection/src/Listener/UnsetContextInTaskWorkerListener.php b/src/db-connection/src/Listener/UnsetContextInTaskWorkerListener.php new file mode 100644 index 000000000..3991f36f6 --- /dev/null +++ b/src/db-connection/src/Listener/UnsetContextInTaskWorkerListener.php @@ -0,0 +1,53 @@ +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); + } + } +}