Browse Source

checkErrors when process exit

walkor 10 năm trước cách đây
mục cha
commit
ddd527ee3d
2 tập tin đã thay đổi với 86 bổ sung4 xóa
  1. 21 3
      GatewayWorker/Gateway.php
  2. 65 1
      Workerman/Worker.php

+ 21 - 3
GatewayWorker/Gateway.php

@@ -109,6 +109,12 @@ class Gateway extends Worker
     protected $_onWorkerStop = null;
     
     /**
+     * 进程启动时间
+     * @var int
+     */
+    protected $_startTime = 0;
+    
+    /**
      * 构造函数
      * @param string $socket_name
      * @param array $context_option
@@ -143,6 +149,10 @@ class Gateway extends Worker
         // 保存用户的回调,当对应的事件发生时触发
         $this->_onWorkerStop = $this->onWorkerStop;
         $this->onWorkerStop = array($this, 'onWorkerStop');
+        
+        // 记录进程启动的时间
+        $this->_startTime = time();
+        // 运行父方法
         parent::run();
     }
     
@@ -223,8 +233,13 @@ class Gateway extends Worker
         // 没有可用的worker
         else
         {
-            $msg = "SendBufferToWorker fail. The connections between Gateway and BusinessWorker are not ready";
-            $this->log($msg);
+            // gateway启动后1-2秒内SendBufferToWorker fail是正常现象,因为与worker的连接还没建立起来,所以不记录日志,只是关闭连接
+            $time_diff = 2;
+            if(time() - $this->_startTime >= $time_diff)
+            {
+                $msg = "SendBufferToWorker fail. The connections between Gateway and BusinessWorker are not ready";
+                $this->log($msg);
+            }
             $connection->close();
             return false;
         }
@@ -548,7 +563,10 @@ class Gateway extends Worker
                 continue;
             }
             $connection->pingNotResponseCount++;
-            $connection->send($this->pingData);
+            if($this->pingData)
+            {
+                $connection->send($this->pingData);
+            }
         }
     }
     

+ 65 - 1
Workerman/Worker.php

@@ -21,7 +21,7 @@ class Worker
      * 版本号
      * @var string
      */
-    const VERSION = '3.0.8';
+    const VERSION = '3.0.9';
     
     /**
      * 状态 启动中
@@ -348,6 +348,10 @@ class Worker
         self::$_statisticsFile = sys_get_temp_dir().'/workerman.status';
         // 尝试设置进程名称(需要php>=5.5或者安装了proctitle扩展)
         self::setProcessTitle('WorkerMan: master process  start_file=' . self::$_startFile);
+        
+        // 注册进程退出回调,用来检查是否有错误
+        register_shutdown_function(array('\\Workerman\\Worker', 'checkErrors'));
+        
         // 初始化定时器
         Timer::init();
     }
@@ -1013,6 +1017,66 @@ class Worker
     }
     
     /**
+     * 检查错误
+     * @return void
+     */
+    public static function checkErrors()
+    {
+        if(self::STATUS_SHUTDOWN != self::$_status)
+        {
+            $error_msg = "WORKER EXIT UNEXPECTED ";
+            if($errors = error_get_last())
+            {
+                $error_msg .= $this->getErrorType($errors['type']) . " {$errors['message']} in {$errors['file']} on line {$errors['line']}";
+            }
+            $this->log($error_msg);
+        }
+    }
+    
+    /**
+     * 获取错误类型对应的意义
+     * @param integer $type
+     * @return string
+     */
+    protected static function getErrorType($type)
+    {
+        switch($type)
+        {
+            case E_ERROR: // 1 //
+                return 'E_ERROR';
+            case E_WARNING: // 2 //
+                return 'E_WARNING';
+            case E_PARSE: // 4 //
+                return 'E_PARSE';
+            case E_NOTICE: // 8 //
+                return 'E_NOTICE';
+            case E_CORE_ERROR: // 16 //
+                return 'E_CORE_ERROR';
+            case E_CORE_WARNING: // 32 //
+                return 'E_CORE_WARNING';
+            case E_COMPILE_ERROR: // 64 //
+                return 'E_COMPILE_ERROR';
+            case E_CORE_WARNING: // 128 //
+                return 'E_COMPILE_WARNING';
+            case E_USER_ERROR: // 256 //
+                return 'E_USER_ERROR';
+            case E_USER_WARNING: // 512 //
+                return 'E_USER_WARNING';
+            case E_USER_NOTICE: // 1024 //
+                return 'E_USER_NOTICE';
+            case E_STRICT: // 2048 //
+                return 'E_STRICT';
+            case E_RECOVERABLE_ERROR: // 4096 //
+                return 'E_RECOVERABLE_ERROR';
+            case E_DEPRECATED: // 8192 //
+                return 'E_DEPRECATED';
+            case E_USER_DEPRECATED: // 16384 //
+                return 'E_USER_DEPRECATED';
+        }
+        return "";
+    }
+    
+    /**
      * 记录日志
      * @param string $msg
      * @return void