|
2 | 2 |
|
3 | 3 | namespace Utopia\Queue\Adapter; |
4 | 4 |
|
5 | | -use Swoole\Process\Pool; |
| 5 | +use Swoole\Coroutine; |
| 6 | +use Swoole\Process; |
6 | 7 | use Utopia\Queue\Adapter; |
7 | 8 | use Utopia\Queue\Consumer; |
8 | 9 |
|
9 | 10 | class Swoole extends Adapter |
10 | 11 | { |
11 | | - protected Pool $pool; |
| 12 | + /** @var Process[] */ |
| 13 | + protected array $workers = []; |
12 | 14 |
|
13 | | - public function __construct(Consumer $consumer, int $workerNum, string $queue, string $namespace = 'utopia-queue') |
14 | | - { |
15 | | - parent::__construct($workerNum, $queue, $namespace); |
| 15 | + /** @var callable[] */ |
| 16 | + protected array $onWorkerStart = []; |
16 | 17 |
|
| 18 | + /** @var callable[] */ |
| 19 | + protected array $onWorkerStop = []; |
| 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); |
17 | 28 | $this->consumer = $consumer; |
18 | | - $this->pool = new Pool($workerNum); |
19 | 29 | } |
20 | 30 |
|
21 | 31 | public function start(): self |
22 | 32 | { |
23 | | - $this->pool->set(['enable_coroutine' => true]); |
24 | | - $this->pool->start(); |
| 33 | + for ($i = 0; $i < $this->workerNum; $i++) { |
| 34 | + $this->spawnWorker($i); |
| 35 | + } |
| 36 | + |
| 37 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
| 38 | + |
| 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()); |
| 43 | + |
| 44 | + while (\count($this->workers) > 0) { |
| 45 | + Coroutine::sleep(1); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
25 | 49 | return $this; |
26 | 50 | } |
27 | 51 |
|
28 | | - public function stop(): self |
| 52 | + protected function spawnWorker(int $workerId): void |
29 | 53 | { |
30 | | - $this->pool->shutdown(); |
31 | | - return $this; |
| 54 | + $process = new Process(function () use ($workerId) { |
| 55 | + Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]); |
| 56 | + |
| 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; |
32 | 72 | } |
33 | 73 |
|
34 | | - public function workerStart(callable $callback): self |
| 74 | + protected function reap(): void |
35 | 75 | { |
36 | | - $this->pool->on('WorkerStart', function (Pool $pool, string $workerId) use ($callback) { |
37 | | - call_user_func($callback, $workerId); |
38 | | - }); |
| 76 | + while (($ret = Process::wait(false)) !== false) { |
| 77 | + unset($this->workers[$ret['pid']]); |
| 78 | + } |
| 79 | + } |
39 | 80 |
|
| 81 | + public function stop(): self |
| 82 | + { |
| 83 | + foreach ($this->workers as $pid => $process) { |
| 84 | + Process::kill($pid, SIGTERM); |
| 85 | + } |
40 | 86 | return $this; |
41 | 87 | } |
42 | 88 |
|
43 | | - public function workerStop(callable $callback): self |
| 89 | + public function workerStart(callable $callback): self |
44 | 90 | { |
45 | | - $this->pool->on('WorkerStart', function (Pool $pool, string $workerId) use ($callback) { |
46 | | - call_user_func($callback, $workerId); |
47 | | - }); |
48 | | - |
| 91 | + $this->onWorkerStart[] = $callback; |
49 | 92 | return $this; |
50 | 93 | } |
51 | 94 |
|
52 | | - public function getNative(): Pool |
| 95 | + public function workerStop(callable $callback): self |
53 | 96 | { |
54 | | - return $this->pool; |
| 97 | + $this->onWorkerStop[] = $callback; |
| 98 | + return $this; |
55 | 99 | } |
56 | 100 | } |
0 commit comments