|
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; |
14 | | - |
15 | | - /** @var callable */ |
16 | | - private $onStop; |
17 | | - |
18 | | - public function __construct(Consumer $consumer, int $workerNum, string $queue, string $namespace = 'utopia-queue') |
19 | | - { |
| 12 | + /** @var Process[] */ |
| 13 | + protected array $workers = []; |
| 14 | + protected bool $running = false; |
| 15 | + |
| 16 | + /** @var callable[] */ |
| 17 | + protected array $onWorkerStart = []; |
| 18 | + |
| 19 | + /** @var callable[] */ |
| 20 | + protected array $onWorkerStop = []; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + Consumer $consumer, |
| 24 | + int $workerNum, |
| 25 | + string $queue, |
| 26 | + string $namespace = 'utopia-queue', |
| 27 | + ) { |
20 | 28 | parent::__construct($workerNum, $queue, $namespace); |
21 | | - |
22 | 29 | $this->consumer = $consumer; |
23 | | - $this->pool = new Pool($workerNum); |
24 | 30 | } |
25 | 31 |
|
26 | 32 | public function start(): self |
27 | 33 | { |
28 | | - $this->pool->set(['enable_coroutine' => true]); |
| 34 | + $this->running = true; |
29 | 35 |
|
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 | | - }); |
| 36 | + for ($i = 0; $i < $this->workerNum; $i++) { |
| 37 | + $this->spawnWorker($i); |
| 38 | + } |
36 | 39 |
|
37 | | - pcntl_signal(SIGINT, function () { |
38 | | - Console::info("[Swoole] Received SIGINT, initiating graceful shutdown..."); |
39 | | - $this->stop(); |
40 | | - }); |
| 40 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
41 | 41 |
|
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 | | - } |
| 42 | + Coroutine\run(function () { |
| 43 | + Process::signal(SIGTERM, fn () => $this->stop()); |
| 44 | + Process::signal(SIGINT, fn () => $this->stop()); |
| 45 | + Process::signal(SIGCHLD, fn () => $this->reap()); |
| 46 | + |
| 47 | + while ($this->running && \count($this->workers) > 0) { |
| 48 | + Coroutine::sleep(1); |
| 49 | + } |
| 50 | + }); |
47 | 51 |
|
48 | | - $this->pool->start(); |
49 | 52 | return $this; |
50 | 53 | } |
51 | 54 |
|
52 | | - public function stop(): self |
| 55 | + protected function spawnWorker(int $workerId): void |
53 | 56 | { |
54 | | - if ($this->onStop) { |
55 | | - call_user_func($this->onStop); |
56 | | - } |
| 57 | + $process = new Process(function () use ($workerId) { |
| 58 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
57 | 59 |
|
58 | | - Console::info("[Swoole] Shutting down process pool..."); |
59 | | - $this->pool->shutdown(); |
60 | | - Console::success("[Swoole] Process pool stopped."); |
61 | | - return $this; |
62 | | - } |
| 60 | + Coroutine\run(function () use ($workerId) { |
| 61 | + Process::signal(SIGTERM, fn () => $this->consumer->close()); |
63 | 62 |
|
64 | | - public function workerStart(callable $callback): self |
65 | | - { |
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); |
| 63 | + foreach ($this->onWorkerStart as $callback) { |
| 64 | + $callback((string)$workerId); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + // onWorkerStop runs after Coroutine::run() exits |
| 69 | + foreach ($this->onWorkerStop as $callback) { |
| 70 | + $callback((string)$workerId); |
80 | 71 | } |
| 72 | + }, false, 0, false); |
81 | 73 |
|
82 | | - call_user_func($callback, $workerId); |
83 | | - }); |
| 74 | + $pid = $process->start(); |
| 75 | + $this->workers[$pid] = $process; |
| 76 | + } |
84 | 77 |
|
85 | | - return $this; |
| 78 | + protected function reap(): void |
| 79 | + { |
| 80 | + while (($ret = Process::wait(false)) !== false) { |
| 81 | + unset($this->workers[$ret['pid']]); |
| 82 | + } |
86 | 83 | } |
87 | 84 |
|
88 | | - public function workerStop(callable $callback): self |
| 85 | + public function stop(): self |
89 | 86 | { |
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 | | - }); |
| 87 | + $this->running = false; |
| 88 | + foreach ($this->workers as $pid => $process) { |
| 89 | + Process::kill($pid, SIGTERM); |
| 90 | + } |
| 91 | + return $this; |
| 92 | + } |
94 | 93 |
|
| 94 | + public function workerStart(callable $callback): self |
| 95 | + { |
| 96 | + $this->onWorkerStart[] = $callback; |
95 | 97 | return $this; |
96 | 98 | } |
97 | 99 |
|
98 | | - public function getNative(): Pool |
| 100 | + public function workerStop(callable $callback): self |
99 | 101 | { |
100 | | - return $this->pool; |
| 102 | + $this->onWorkerStop[] = $callback; |
| 103 | + return $this; |
101 | 104 | } |
102 | 105 | } |
0 commit comments