|
2 | 2 |
|
3 | 3 | namespace Utopia\Queue\Adapter; |
4 | 4 |
|
5 | | -use Swoole\Constant; |
6 | | -use Swoole\Process\Pool; |
7 | | -use Utopia\CLI\Console; |
| 5 | +use Swoole\Coroutine; |
| 6 | +use Swoole\Process; |
8 | 7 | use Utopia\Queue\Adapter; |
9 | 8 | use Utopia\Queue\Consumer; |
10 | 9 |
|
11 | 10 | class Swoole extends Adapter |
12 | 11 | { |
13 | | - protected Pool $pool; |
| 12 | + /** @var Process[] */ |
| 13 | + protected array $workers = []; |
14 | 14 |
|
15 | | - /** @var callable */ |
16 | | - private $onStop; |
| 15 | + /** @var callable[] */ |
| 16 | + protected array $onWorkerStart = []; |
17 | 17 |
|
18 | | - public function __construct(Consumer $consumer, int $workerNum, string $queue, string $namespace = 'utopia-queue') |
19 | | - { |
20 | | - parent::__construct($workerNum, $queue, $namespace); |
| 18 | + /** @var callable[] */ |
| 19 | + protected array $onWorkerStop = []; |
21 | 20 |
|
| 21 | + public function __construct( |
| 22 | + Consumer $consumer, |
| 23 | + int $workerNum, |
| 24 | + string $queue, |
| 25 | + string $namespace = 'utopia-queue', |
| 26 | + ) { |
| 27 | + parent::__construct($workerNum, $queue, $namespace); |
22 | 28 | $this->consumer = $consumer; |
23 | | - $this->pool = new Pool($workerNum); |
24 | 29 | } |
25 | 30 |
|
26 | 31 | public function start(): self |
27 | 32 | { |
28 | | - $this->pool->set(['enable_coroutine' => true]); |
| 33 | + for ($i = 0; $i < $this->workerNum; $i++) { |
| 34 | + $this->spawnWorker($i); |
| 35 | + } |
29 | 36 |
|
30 | | - // Register signal handlers in the main process before starting pool |
31 | | - if (extension_loaded('pcntl')) { |
32 | | - pcntl_signal(SIGTERM, function () { |
33 | | - Console::info("[Swoole] Received SIGTERM, initiating graceful shutdown..."); |
34 | | - $this->stop(); |
35 | | - }); |
| 37 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
36 | 38 |
|
37 | | - pcntl_signal(SIGINT, function () { |
38 | | - Console::info("[Swoole] Received SIGINT, initiating graceful shutdown..."); |
39 | | - $this->stop(); |
40 | | - }); |
| 39 | + Coroutine\run(function () { |
| 40 | + Process::signal(SIGTERM, fn () => $this->stop()); |
| 41 | + Process::signal(SIGINT, fn () => $this->stop()); |
| 42 | + Process::signal(SIGCHLD, fn () => $this->reap()); |
41 | 43 |
|
42 | | - // Enable async signals |
43 | | - pcntl_async_signals(true); |
44 | | - } else { |
45 | | - Console::warning("[Swoole] pcntl extension is not loaded, worker will not shutdown gracefully."); |
46 | | - } |
| 44 | + while (\count($this->workers) > 0) { |
| 45 | + Coroutine::sleep(1); |
| 46 | + } |
| 47 | + }); |
47 | 48 |
|
48 | | - $this->pool->start(); |
49 | 49 | return $this; |
50 | 50 | } |
51 | 51 |
|
52 | | - public function stop(): self |
| 52 | + protected function spawnWorker(int $workerId): void |
53 | 53 | { |
54 | | - if ($this->onStop) { |
55 | | - call_user_func($this->onStop); |
56 | | - } |
| 54 | + $process = new Process(function () use ($workerId) { |
| 55 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
57 | 56 |
|
58 | | - Console::info("[Swoole] Shutting down process pool..."); |
59 | | - $this->pool->shutdown(); |
60 | | - Console::success("[Swoole] Process pool stopped."); |
61 | | - return $this; |
| 57 | + Coroutine\run(function () use ($workerId) { |
| 58 | + Process::signal(SIGTERM, fn () => $this->consumer->close()); |
| 59 | + |
| 60 | + foreach ($this->onWorkerStart as $callback) { |
| 61 | + $callback((string)$workerId); |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($this->onWorkerStop as $callback) { |
| 65 | + $callback((string)$workerId); |
| 66 | + } |
| 67 | + }); |
| 68 | + }, false, 0, false); |
| 69 | + |
| 70 | + $pid = $process->start(); |
| 71 | + $this->workers[$pid] = $process; |
62 | 72 | } |
63 | 73 |
|
64 | | - public function workerStart(callable $callback): self |
| 74 | + protected function reap(): void |
65 | 75 | { |
66 | | - $this->pool->on(Constant::EVENT_WORKER_START, function (Pool $pool, string $workerId) use ($callback) { |
67 | | - // Register signal handlers in each worker process for graceful shutdown |
68 | | - if (extension_loaded('pcntl')) { |
69 | | - pcntl_signal(SIGTERM, function () use ($workerId) { |
70 | | - Console::info("[Worker] Worker {$workerId} received SIGTERM, closing consumer..."); |
71 | | - $this->consumer->close(); |
72 | | - }); |
73 | | - |
74 | | - pcntl_signal(SIGINT, function () use ($workerId) { |
75 | | - Console::info("[Worker] Worker {$workerId} received SIGINT, closing consumer..."); |
76 | | - $this->consumer->close(); |
77 | | - }); |
78 | | - |
79 | | - pcntl_async_signals(true); |
80 | | - } |
81 | | - |
82 | | - call_user_func($callback, $workerId); |
83 | | - }); |
| 76 | + while (($ret = Process::wait(false)) !== false) { |
| 77 | + unset($this->workers[$ret['pid']]); |
| 78 | + } |
| 79 | + } |
84 | 80 |
|
| 81 | + public function stop(): self |
| 82 | + { |
| 83 | + foreach ($this->workers as $pid => $process) { |
| 84 | + Process::kill($pid, SIGTERM); |
| 85 | + } |
85 | 86 | return $this; |
86 | 87 | } |
87 | 88 |
|
88 | | - public function workerStop(callable $callback): self |
| 89 | + public function workerStart(callable $callback): self |
89 | 90 | { |
90 | | - $this->onStop = $callback; |
91 | | - $this->pool->on(Constant::EVENT_WORKER_STOP, function (Pool $pool, string $workerId) use ($callback) { |
92 | | - call_user_func($callback, $workerId); |
93 | | - }); |
94 | | - |
| 91 | + $this->onWorkerStart[] = $callback; |
95 | 92 | return $this; |
96 | 93 | } |
97 | 94 |
|
98 | | - public function getNative(): Pool |
| 95 | + public function workerStop(callable $callback): self |
99 | 96 | { |
100 | | - return $this->pool; |
| 97 | + $this->onWorkerStop[] = $callback; |
| 98 | + return $this; |
101 | 99 | } |
102 | 100 | } |
0 commit comments