|
3 | 3 | namespace Utopia\Queue; |
4 | 4 |
|
5 | 5 | use Exception; |
| 6 | +use Swoole\Coroutine; |
6 | 7 | use Throwable; |
7 | 8 | use Utopia\DI\Container; |
8 | 9 | use Utopia\Servers\Hook; |
@@ -214,14 +215,16 @@ public function init(): Hook |
214 | 215 | */ |
215 | 216 | public function start(): self |
216 | 217 | { |
217 | | - $this->workerContainer = new Container($this->container); |
| 218 | + if ($this->coroutines && Coroutine::getCid() !== -1) { |
| 219 | + Coroutine::getContext()[self::WORKER_CONTAINER_CONTEXT_KEY] = new Container($this->container); |
| 220 | + } |
218 | 221 |
|
219 | 222 | try { |
220 | 223 | $this->adapter->workerStart(function (string $workerId) { |
221 | | - $this->workerContainer->set('workerId', fn () => $workerId); |
| 224 | + $this->getContainer()->set('workerId', fn () => $workerId); |
222 | 225 |
|
223 | 226 | foreach ($this->workerStartHooks as $hook) { |
224 | | - $hook->getAction()(...$this->getArguments($this->workerContainer, $hook)); |
| 227 | + $hook->getAction()(...$this->getArguments($this->getContainer(), $hook)); |
225 | 228 | } |
226 | 229 |
|
227 | 230 | $this->adapter->consumer->consume( |
@@ -322,13 +325,13 @@ function (?Message $message, Throwable $th) { |
322 | 325 | }); |
323 | 326 |
|
324 | 327 | $this->adapter->workerStop(function (string $workerId) { |
325 | | - $this->workerContainer->set('workerId', fn () => $workerId); |
| 328 | + $this->getContainer()->set('workerId', fn () => $workerId); |
326 | 329 |
|
327 | 330 | try { |
328 | 331 | // Call user-defined workerStop hooks |
329 | 332 | foreach ($this->workerStopHooks as $hook) { |
330 | 333 | try { |
331 | | - $hook->getAction()(...$this->getArguments($this->workerContainer, $hook)); |
| 334 | + $hook->getAction()(...$this->getArguments($this->getContainer(), $hook)); |
332 | 335 | } catch (Throwable $e) { |
333 | 336 | } |
334 | 337 | } |
|
0 commit comments