Skip to content

Commit 17efc3f

Browse files
committed
init worker container
1 parent 58f59a7 commit 17efc3f

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/Queue/Server.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Queue;
44

55
use Exception;
6+
use Swoole\Coroutine;
67
use Throwable;
78
use Utopia\DI\Container;
89
use Utopia\Servers\Hook;
@@ -214,14 +215,16 @@ public function init(): Hook
214215
*/
215216
public function start(): self
216217
{
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+
}
218221

219222
try {
220223
$this->adapter->workerStart(function (string $workerId) {
221-
$this->workerContainer->set('workerId', fn () => $workerId);
224+
$this->getContainer()->set('workerId', fn () => $workerId);
222225

223226
foreach ($this->workerStartHooks as $hook) {
224-
$hook->getAction()(...$this->getArguments($this->workerContainer, $hook));
227+
$hook->getAction()(...$this->getArguments($this->getContainer(), $hook));
225228
}
226229

227230
$this->adapter->consumer->consume(
@@ -322,13 +325,13 @@ function (?Message $message, Throwable $th) {
322325
});
323326

324327
$this->adapter->workerStop(function (string $workerId) {
325-
$this->workerContainer->set('workerId', fn () => $workerId);
328+
$this->getContainer()->set('workerId', fn () => $workerId);
326329

327330
try {
328331
// Call user-defined workerStop hooks
329332
foreach ($this->workerStopHooks as $hook) {
330333
try {
331-
$hook->getAction()(...$this->getArguments($this->workerContainer, $hook));
334+
$hook->getAction()(...$this->getArguments($this->getContainer(), $hook));
332335
} catch (Throwable $e) {
333336
}
334337
}

0 commit comments

Comments
 (0)