Parcourir la source

fix event bug + refactor docker compose file + add child process manager

stanley-king il y a 4 ans
Parent
commit
a872dc4066

+ 0 - 2
conf/php/docker-php-spwan-start

@@ -3,8 +3,6 @@ set -e
 spawn-fcgi -a 0.0.0.0 -p 9100 -F 1 -f "php /var/www/html/fcgi_run.php"
 
 time=$(date "+%Y%m%d")
-#$logfile="/var/www/html/data/log/${time}-mobile.log"
-#echo $logfile
 
 if [ ! -f "/var/www/html/data/log/${time}-mobile.log" ]; then
     touch "/var/www/html/data/log/${time}-mobile.log"

+ 3 - 5
conf/php/docker-php-start

@@ -3,11 +3,9 @@ set -e
 spawn-fcgi -a 0.0.0.0 -p 9100 -F 10 -f "php /var/www/html/fcgi_run.php"
 
 time=$(date "+%Y%m%d")
-#$logfile="/var/www/html/data/log/${time}.log"
-#echo $logfile
 
-if [ ! -f "/var/www/html/data/log/${time}.log" ]; then
-    touch "/var/www/html/data/log/${time}.log"
+if [ ! -f "/var/www/html/data/log/${time}-mobile.log" ]; then
+    touch "/var/www/html/data/log/${time}-mobile.log"
 fi
 
-tail -f "/var/www/html/data/log/${time}.log"
+tail -f "/var/www/html/data/log/${time}-mobile.log"

+ 7 - 7
conf/queue/docker-queue-start

@@ -3,10 +3,10 @@ set -e
 
 php /var/www/html/queue/index.php queue index
 
-time=$(date "+%Y%m%d")
-
-if [ ! -f "/var/www/html/data/log/${time}-queue.log" ]; then
-    touch "/var/www/html/data/log/${time}-queue.log"
-fi
-
-tail -f "/var/www/html/data/log/${time}-queue.log"
+#time=$(date "+%Y%m%d")
+#
+#if [ ! -f "/var/www/html/data/log/${time}-queue.log" ]; then
+#    touch "/var/www/html/data/log/${time}-queue.log"
+#fi
+#
+#tail -f "/var/www/html/data/log/${time}-queue.log"

+ 1 - 1
data/config/dev/srv.ini.php

@@ -3,7 +3,7 @@ defined('InShopNC') or exit('Access Invalid!');
 
 define('LOCAL_DOMAIN','192.168.1.200');
 
-$config['searcher']['host'] = 'SearchSrv';
+$config['searcher']['host'] = 'searcher';
 $config['searcher']['port'] = 2000;
 
 $config['relation']['host'] = LOCAL_DOMAIN;

+ 25 - 35
docker-compose-dev.yml

@@ -5,10 +5,9 @@ services:
     image: nginx:alpine
     ports:
       - "80:80"
-      - "443:443"
     volumes:
-      - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD:/var/www/html
+      - $PWD/conf/etc/localtime:/etc/localtime:ro
       - /Volumes/Transcend/upload:/var/www/html/data/upload
       - $PWD/conf/nginx/cert:/etc/nginx/cert
       - $PWD/conf/nginx/nginx-debug-http.conf:/etc/nginx/nginx.conf:ro
@@ -31,83 +30,61 @@ services:
 
   mobilesrv:
     image: php-zts-debug:7.3.18
-    ports:
-      - "9100:9100"
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD:/var/www/html
       - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
       - /Volumes/Transcend/upload:/var/www/html/data/upload
-      - $PWD/conf/php/docker-php-spwan-start:/usr/local/bin/docker-php-spwan-start
+      - $PWD/conf/php/docker-php-spwan-start:/usr/local/bin/docker-spwan-start
     links:
       - redisrv
     container_name: "panda-mobile"
-    command: ['docker-php-spwan-start']
+    command: ['docker-spwan-start']
     depends_on:
       - "redisrv"
-      - "SearchSrv"
+      - "searcher"
 
   fpmsrv:
       image: php-fpm:alpine
-      ports:
-        - "9000:9000"
       volumes:
         - $PWD/conf/etc/localtime:/etc/localtime:ro
         - $PWD:/var/www/html
         - /Volumes/Transcend/upload:/var/www/html/data/upload
         - $PWD/conf/php-fpm/php-debug.ini:/usr/local/etc/php/php.ini
         - $PWD/conf/php-fpm/php-fpm.conf:/usr/local/etc/php-fpm.conf
-        - $PWD/conf/php-fpm/docker-php-fpm-start:/usr/local/bin/docker-php-fpm-start
       container_name: "panda-web"
-      command: [docker-php-fpm-start]
+      command: [php-fpm]
       depends_on:
         - "redisrv"
-        - "SearchSrv"
+        - "searcher"
       #docker run -it -v $PWD:/var/www/html -v $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini -p 9000:9000
       #--name websrv  php-fpm:alpine
 
-  phpcli:
-    image: php-zts-debug:7.3.18
-    volumes:
-      - $PWD/conf/etc/localtime:/etc/localtime:ro
-      - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
-      - $PWD:/var/www/html
-      - /Volumes/Transcend/upload:/var/www/html/data/upload
-    links:
-      - redisrv
-    container_name: "panda-php"
-    depends_on:
-      - "redisrv"
-      - "fpmsrv"
-      - "SearchSrv"
-
-  QueueSrv:
+  queuesrv:
     image: php-zts-debug:7.3.18
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD:/var/www/html
       - /Volumes/Transcend/upload:/var/www/html/data/upload
       - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
-      - $PWD/conf/queue/docker-queue-start:/usr/local/bin/docker-queue-start
     links:
       - redisrv
     container_name: "panda-queue"
-    command: [docker-queue-start]
+    command: [php,"/var/www/html/queue/index.php", "queue", "index"]
     depends_on:
       - "redisrv"
 
-  SearchSrv:
+  searcher:
     image: php-zts-debug:7.3.18
     volumes:
       - $PWD/conf/etc/localtime:/etc/localtime:ro
       - $PWD:/var/www/html
       - /Volumes/Transcend/upload:/var/www/html/data/upload
       - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
-      - $PWD/conf/searcher/docker-search-start:/usr/local/bin/docker-start
     links:
       - redisrv
     container_name: "panda-searcher"
-    command: [docker-start]
+    command: [php, "/var/www/html/searcher.php"]
     depends_on:
       - "redisrv"
 
@@ -119,10 +96,23 @@ services:
         - /Volumes/Transcend/upload:/var/www/html/data/upload
         - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
         - $PWD/conf/crontab/debug-root:/var/spool/cron/crontabs/root
-        - $PWD/conf/crontab/docker-start:/usr/local/bin/docker-start
       links:
         - redisrv
       container_name: "panda-crontab"
-      command: [docker-start]
+      command: [crond,"-f"]
       depends_on:
         - "redisrv"
+  phpcli:
+    image: php-zts-debug:7.3.18
+    volumes:
+      - $PWD/conf/etc/localtime:/etc/localtime:ro
+      - $PWD/conf/php/php-debug.ini:/usr/local/etc/php/php.ini
+      - $PWD:/var/www/html
+      - /Volumes/Transcend/upload:/var/www/html/data/upload
+    links:
+      - redisrv
+    container_name: "panda-php"
+    depends_on:
+      - "redisrv"
+      - "fpmsrv"
+      - "searcher"

+ 6 - 10
docker-compose-test.yml

@@ -33,11 +33,11 @@ services:
       - $PWD:/var/www/html
       - $PWD/conf/php/php.ini:/usr/local/etc/php/php.ini
       - /mnt/upload:/var/www/html/data/upload
-      - $PWD/conf/php/docker-php-start:/usr/local/bin/docker-php-start
+      - $PWD/conf/php/docker-php-start:/usr/local/bin/docker-spwan-start
     links:
       - tredisrv
     container_name: "xyztest-mobile"
-    command: ['docker-php-start']
+    command: ['docker-spwan-start']
     depends_on:
       - "tredisrv"
     extra_hosts:
@@ -52,9 +52,8 @@ services:
       - /mnt/upload:/var/www/html/data/upload
       - $PWD/conf/php-fpm/php.ini:/usr/local/etc/php/php.ini
       - $PWD/conf/php-fpm/php-fpm.conf:/usr/local/etc/php-fpm.conf
-      - $PWD/conf/php-fpm/docker-php-fpm-start:/usr/local/bin/docker-php-fpm-start
     container_name: "xyztest-web"
-    command: [docker-php-fpm-start]
+    command: [php-fpm]
     depends_on:
       - "tredisrv"
     extra_hosts:
@@ -68,11 +67,10 @@ services:
       - $PWD:/var/www/html
       - /mnt/upload:/var/www/html/data/upload
       - $PWD/conf/php/php.ini:/usr/local/etc/php/php.ini
-      - $PWD/conf/queue/docker-queue-start:/usr/local/bin/docker-queue-start
     links:
       - tredisrv
     container_name: "xyztest-queue"
-    command: [docker-queue-start]
+    command: [php,"/var/www/html/queue/index.php", "queue", "index"]
     depends_on:
       - "tredisrv"
     extra_hosts:
@@ -86,11 +84,10 @@ services:
       - $PWD:/var/www/html
       - /mnt/upload:/var/www/html/data/upload
       - $PWD/conf/php/php.ini:/usr/local/etc/php/php.ini
-      - $PWD/conf/searcher/docker-search-start:/usr/local/bin/docker-start
     links:
       - tredisrv
     container_name: "xyztest-searcher"
-    command: [docker-start]
+    command: [php, "/var/www/html/searcher.php"]
     depends_on:
       - "tredisrv"
     extra_hosts:
@@ -105,11 +102,10 @@ services:
       - /mnt/upload:/var/www/html/data/upload
       - $PWD/conf/php/php.ini:/usr/local/etc/php/php.ini
       - $PWD/conf/crontab/root:/var/spool/cron/crontabs/root
-      - $PWD/conf/crontab/docker-start:/usr/local/bin/docker-start
     links:
       - tredisrv
     container_name: "xyztest-crontab"
-    command: [docker-start]
+    command: [crond,"-f"]
     depends_on:
       - "tredisrv"
     extra_hosts:

+ 10 - 6
helper/event/EventLooper.php

@@ -61,8 +61,9 @@ class SocketReader
         }
         if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
             $this->mBufferEvent->disable(Event::READ);
-            $this->mBufferEvent->setCallbacks(NULL, NULL, NULL, NULL); //这行加上了,才能让对象释放。
+            $this->mBufferEvent->close();
             $this->mBufferEvent->free();
+            $this->mBufferEvent = null;
             $this->mParent->remove($this->mFd);
             $this->mParent = null;
             $this->mProcessor = null;
@@ -145,12 +146,15 @@ class EventLooper
 
     public function run_loop()
     {
-        $this->add_signal(SIGINT);
-        $this->add_signal(SIGQUIT);
-        $this->add_signal(SIGTERM);
+        $scope = new scope_trace(__METHOD__);
+
+//        $this->add_signal(SIGINT);
+//        $this->add_signal(SIGQUIT);
+//        $this->add_signal(SIGTERM);
 
         Log::record('EventLooper start run loop....',Log::DEBUG);
-        $this->mEvBase->loop();
+//        $this->mEvBase->loop();
+        $this->mEvBase->dispatch();
     }
 
     public function add_listen($stream)
@@ -177,7 +181,7 @@ class EventLooper
     {
         $scope = new scope_trace(__METHOD__);
         Log::record("ev_signal sig={$sig},flag={$flag}", Log::DEBUG);
-        $this->mEvBase->exit();
+        $this->mEvBase->exit(NULL);
     }
 
     public function onAccept($listener, $fd, $address, $ctx)

+ 82 - 10
helper/event/util.php

@@ -62,13 +62,15 @@ class util
         else
         {
             $ret = pcntl_waitpid($pid,$status,WNOHANG);
-            Log::record("pcntl_waitpid pid = {$pid}",Log::DEBUG);
+            Log::record("pcntl_waitpid pid = {$pid},ret={$ret}",Log::DEBUG);
 
             if($ret == 0) {
                 Log::record("fork_child: successful ret == 0 PID: {$pid}",Log::DEBUG);
+                return $pid;
             }
             elseif($ret == -1) {
                 Log::record("fork_child: ret == -1  PID: {$pid}",Log::DEBUG);
+
             }
             else {
                 Log::record("fork_child: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
@@ -76,14 +78,8 @@ class util
         }
     }
 
-    static function fork_listen($host, $port, $callback, $count)
+    static function fork_listen($fd, $callback, $count)
     {
-        $fd = util::listen($host,$port);
-        if($fd === false) {
-            Log::record("cannot open listen socket = {$host}:{$port}",Log::DEBUG);
-            return false;
-        }
-
         $params = [$fd];
         $count = intval($count);
         if($count <= 0) {
@@ -91,11 +87,62 @@ class util
         }
         else
         {
+            $childs = [];
             while ($count--) {
-                util::fork_child($callback,$params);
+                $childs[] = util::fork_child($callback,$params);
+            }
+        }
+
+        return $childs;
+    }
+
+    static function fork_listenex($fd, $callback, $count)
+    {
+        self::fork_listen($fd,$callback,$count);
+        while ($count) {
+            Log::record("waitting child quit......",Log::DEBUG);
+            $ret = pcntl_wait($status);
+            Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
+            if($ret > 0)
+            {
+                if(pcntl_wifexited($status)) {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
+                } else {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
+                }
+                self::fork_listen($fd,$callback,1);
+            }
+            else {
+                //子进程已经退出干净了.
+                break;
+            }
+        }
+    }
+
+    static function join()
+    {
+        do {
+            Log::record("waitting child quit......",Log::DEBUG);
+            $ret = pcntl_wait($status);
+            Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
+            if($ret > 0)
+            {
+                if(pcntl_wifexited($status)) {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
+                } else {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
+                }
+            }
+            else {
+                //子进程已经退出干净了.
+                break;
             }
-            socket_close($fd);
         }
+        while(true);
     }
 
     static function fork_worker($callback, $count)
@@ -111,4 +158,29 @@ class util
             }
         }
     }
+
+    static function fork_workerex($callback, $count)
+    {
+        self::fork_worker($callback,$count);
+        while ($count) {
+            Log::record("waitting child quit......",Log::DEBUG);
+            $ret = pcntl_wait($status);
+            Log::record("pcntl_waitpid ret={$ret}",Log::DEBUG);
+            if($ret > 0)
+            {
+                if(pcntl_wifexited($status)) {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child normal quit ret={$ret} status = {$status}",Log::DEBUG);
+                } else {
+                    $ret = pcntl_wexitstatus($status);
+                    Log::record("child abnormal quit ret={$ret} status = {$status}",Log::DEBUG);
+                }
+                self::fork_worker($callback,1);
+            }
+            else {
+                //子进程已经退出干净了.
+                break;
+            }
+        }
+    }
 }

+ 5 - 1
queue/index.php

@@ -7,6 +7,7 @@ defined('BASE_PATH') or define('BASE_PATH',BASE_ROOT_PATH . '/queue');
 
 if (!@include(dirname(dirname(__FILE__)) . '/global.php')) exit('global.php isn\'t exists!');
 if (!@include(BASE_CORE_PATH . '/lrlz.php')) exit('lrlz.php isn\'t exists!');
+
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/event_looper.php');
 
@@ -19,4 +20,7 @@ if (!@include(BASE_PATH . '/control/control.php')) exit('control.php isn\'t exis
 function work_proc() {
     Base::run();
 }
-event\util::fork_worker('work_proc',0);
+
+//work_proc();
+//for demaon 进程
+event\util::fork_workerex('work_proc',1);

+ 55 - 17
searcher.php

@@ -1,5 +1,5 @@
 <?php
-declare(strict_types=1);
+declare(strict_types=0);
 
 define('APP_ID','search');
 define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
@@ -7,6 +7,7 @@ define('BASE_PATH', BASE_ROOT_PATH . '/helper');
 
 require_once(BASE_ROOT_PATH . '/global.php');
 require_once(BASE_ROOT_PATH . '/fooder.php');
+
 require_once(BASE_HELPER_PATH . '/event_looper.php');
 require_once(BASE_HELPER_PATH . '/search/processor.php');
 require_once(BASE_HELPER_PATH . '/search/util.php');
@@ -22,38 +23,75 @@ require_once(BASE_HELPER_PATH . '/message/msgstates.php');
 require_once(BASE_HELPER_PATH . '/message/msgutil.php');
 require_once(BASE_HELPER_PATH . '/message/subscriber.php');
 
-function all_channels()
+
+function handle_error($level, $message, $file, $line)
 {
+    if($level == E_NOTICE) return;
+    $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
+    $backtrace = debug_backtrace();
+    foreach ($backtrace as $item) {
+        $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
+    }
+
+    Log::record($trace,Log::ERR);
+}
+
+set_error_handler('handle_error');
+
+function all_channels() {
     return ['searcher'];
 }
 
 $gMessageStates = null;
 function searchwork($sockfd)
 {
-    global $gMessageStates;
-    $gMessageStates = new MsgStates();
+    $pid = posix_getpid();
+    Log::record("seracher worker child process {$pid} is starting....",Log::DEBUG);
 
-    Base::run_util();
+    try
+    {
+        global $gMessageStates;
+        $gMessageStates = new MsgStates();
 
-    StatesHelper::init();
-    $listener = new message\subscriber($gMessageStates);
-    $listener->start();
+        Base::run_util();
 
-    search\area_library::instance();
+        StatesHelper::init();
+        $listener = new message\subscriber($gMessageStates);
+        $listener->start();
 
-    if(StatesHelper::fetch_state('init')) {
-        Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
-        search\searcher::instance()->init();
+        search\area_library::instance();
+
+        if(StatesHelper::fetch_state('init')) {
+            Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
+            search\searcher::instance()->init();
+        }
+
+        process_looper::instance()->init(new search\processor());
+        process_looper::instance()->add_listen($sockfd);
+        process_looper::instance()->run_loop();
+    }
+    catch (Exception $ex)
+    {
+        Log::record("Exception {$ex->getMessage()}",Log::ERR);
     }
 
-    process_looper::instance()->init(new search\processor());
-    process_looper::instance()->add_listen($sockfd);
-    process_looper::instance()->run_loop();
+    Log::record("child {$pid} searchwork quit",Log::DEBUG);
+    exit(0);
 }
 
 global $config;
 $host = '0.0.0.0';
 $port = $config['searcher']['port'];
 
-Log::record("Search server is starting....",Log::DEBUG);
-event\util::fork_listen($host,$port,'searchwork',1);
+$listen_fd = event\util::listen($host,$port);
+if($listen_fd === false) {
+    Log::record("cannot open listen socket = {$host}:{$port}",Log::DEBUG);
+    return false;
+}
+
+$count = 0;
+event\util::fork_listenex($listen_fd,'searchwork',$count);
+socket_close($listen_fd);
+
+Log::record("searchwork all child process quit.");
+

+ 1 - 1
test/TestAddData.php

@@ -523,5 +523,5 @@ class TestAddData extends TestCase
 
 //docker-compose -f ./docker-compose-dev.yml run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testCancelOrder)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
 //docker-compose -f ./docker-compose-dev.yml run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200821_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
-//docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200916_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
+//docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200917_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test