33namespace Utopia \Queue \Adapter ;
44
55use Swoole \Constant ;
6+ use Swoole \Process ;
67use Swoole \Process \Pool ;
7- use Utopia \CLI \ Console ;
8+ use Utopia \Console ;
89use Utopia \Queue \Adapter ;
910use Utopia \Queue \Consumer ;
1011
1112class Swoole extends Adapter
1213{
1314 protected Pool $ pool ;
1415
15- /** @var callable */
16- private $ onStop ;
17-
18- public function __construct (Consumer $ consumer , int $ workerNum , string $ queue , string $ namespace = 'utopia-queue ' )
19- {
16+ public function __construct (
17+ Consumer $ consumer ,
18+ int $ workerNum ,
19+ string $ queue ,
20+ string $ namespace = "utopia-queue " ,
21+ ) {
2022 parent ::__construct ($ workerNum , $ queue , $ namespace );
2123
2224 $ this ->consumer = $ consumer ;
@@ -25,36 +27,29 @@ public function __construct(Consumer $consumer, int $workerNum, string $queue, s
2527
2628 public function start (): self
2729 {
28- $ this ->pool ->set (['enable_coroutine ' => true ]);
29-
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-
37- pcntl_signal (SIGINT , function () {
38- Console::info ("[Swoole] Received SIGINT, initiating graceful shutdown... " );
39- $ this ->stop ();
40- });
30+ $ this ->pool ->set (["enable_coroutine " => true ]);
31+
32+ // Register signal handlers for master process
33+ Process::signal (SIGTERM , function () {
34+ Console::info (
35+ "[Swoole] Master received SIGTERM, shutting down pool... " ,
36+ );
37+ $ this ->stop ();
38+ });
4139
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- }
40+ Process::signal (SIGINT , function () {
41+ Console::info (
42+ "[Swoole] Master received SIGINT, shutting down pool... " ,
43+ );
44+ $ this ->stop ();
45+ });
4746
4847 $ this ->pool ->start ();
4948 return $ this ;
5049 }
5150
5251 public function stop (): self
5352 {
54- if ($ this ->onStop ) {
55- call_user_func ($ this ->onStop );
56- }
57-
5853 Console::info ("[Swoole] Shutting down process pool... " );
5954 $ this ->pool ->shutdown ();
6055 Console::success ("[Swoole] Process pool stopped. " );
@@ -63,33 +58,38 @@ public function stop(): self
6358
6459 public function workerStart (callable $ callback ): self
6560 {
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 );
61+ $ this ->pool ->on (Constant::EVENT_WORKER_START , function (
62+ Pool $ pool ,
63+ string $ workerId ,
64+ ) use ($ callback ) {
65+ // Register signal handlers in worker to gracefully stop consume loop
66+ Process::signal (SIGTERM , function () use ($ workerId ) {
67+ Console::info (
68+ "[Swoole] Worker {$ workerId } received SIGTERM, stopping consumer... " ,
69+ );
70+ $ this ->consumer ->close ();
71+ });
72+
73+ Process::signal (SIGINT , function () use ($ workerId ) {
74+ Console::info (
75+ "[Swoole] Worker {$ workerId } received SIGINT, stopping consumer... " ,
76+ );
77+ $ this ->consumer ->close ();
78+ });
79+
80+ \call_user_func ($ callback , $ workerId );
8381 });
8482
8583 return $ this ;
8684 }
8785
8886 public function workerStop (callable $ callback ): self
8987 {
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 );
88+ $ this ->pool ->on (Constant::EVENT_WORKER_STOP , function (
89+ Pool $ pool ,
90+ string $ workerId ,
91+ ) use ($ callback ) {
92+ \call_user_func ($ callback , $ workerId );
9393 });
9494
9595 return $ this ;
0 commit comments