Exemple #1
0
 public function AfterHandle(IRequest $request)
 {
     $this->_endTime = microtime(true);
     if ($this->_endTime - $this->_startTime > 1) {
         Log::Debug("Handler " . get_class($this) . " used " . ($this->_endTime - $this->_startTime . "s."));
     }
 }
Exemple #2
0
 public function Bootstrap()
 {
     //echo '=============================================================' . PHP_EOL;
     $envAry = [];
     $colorPrefix = "";
     //"\033[31m";
     $colorSufix = "";
     //"\033[0m";
     if (php_sapi_name() !== 'cli') {
         throw new BootException('App should run in CLI mode.' . PHP_EOL);
     }
     $envAry['Running mode'] = 'PHP-CLI';
     // 检测PHP环境
     if (version_compare(PHP_VERSION, '5.5.9', '<')) {
         throw new BootException('Require PHP version >= 5.5.9 !' . PHP_EOL);
     }
     $envAry['PHP version'] = $colorPrefix . PHP_VERSION . $colorSufix;
     // 检测Swoole环境
     if (version_compare(\swoole_version(), '1.8.1', '<')) {
         throw new BootException('Require swoole version >= 1.8.1' . PHP_EOL);
     }
     $envAry['Swoole version'] = $colorPrefix . \swoole_version() . $colorSufix;
     // 检测服务名称
     if (!\defined('DI_SERVER_NAME')) {
         throw new BootException('DI_SERVER_NAME should set' . PHP_EOL);
     }
     $envAry['Server name'] = $colorPrefix . DI_SERVER_NAME . $colorSufix;
     //echo '=============================================================' . PHP_EOL;
     Log::Info(['Environment' => $envAry]);
 }
Exemple #3
0
 public function Bootstrap()
 {
     $this->setConfig();
     //	Log::Info("=============================================================");
     Log::Info(['Swoole Settings' => Server::GetSetting()]);
     //	Log::Info("=============================================================");
     //echo $this->formatSettings();
 }
 /**
  * @param \DIServer\Interfaces\IRequest $request 当前请求
  * @param \Closure                      $next    下一个中间件的方法引用
  */
 public function Handle(IRequest $request, \Closure $next)
 {
     // TODO: Implement Handle() method.
     if (rand(0, 100) > 80) {
         call_user_func($next, $request);
     } else {
         Log::Debug("Auth failed.");
     }
 }
Exemple #5
0
 public function Bootstrap()
 {
     $processes = Application::AutoBuildCollection('Process.php', \swoole_process::class);
     foreach ($processes as $processClass => $process) {
         if (Server::AddProcess($process)) {
             Log::Notice("User process {processClass} has added.", ['processClass' => get_class($process)]);
         }
     }
 }
Exemple #6
0
 /**
  * @param \DIServer\Interfaces\IRequest $request 当前请求
  * @param \Closure                      $next    下一个中间件的方法引用
  */
 public function Handle(IRequest $request, \Closure $next)
 {
     Log::Debug('Session Middleware Start');
     //\DIServer\Services\Session::Start($request->GetFD());
     $response = $next($request);
     Log::Debug('Session Middleware Close');
     //\DIServer\Services\Session::Close($request->GetFD());
     return $response;
 }
Exemple #7
0
 public function GC()
 {
     //尝试进行回收
     $files = IO::AllFile($this->path);
     foreach ($files as $file) {
         Log::Debug("{$file} =" . date('[Y-m-d H:i:s]', fileatime($file)));
         if (time() - fileatime($file) > 1440) {
             unlink($file);
         }
     }
 }
Exemple #8
0
 public function Bootstrap()
 {
     /** @var IListener $defaultListener */
     $defaultListener = $this->initListener()->current();
     //拿出第一个监听作为构造函数的参数
     $initParams = ['serv_host' => $defaultListener->GetHost(), 'serv_port' => $defaultListener->GetPort(), 'serv_mode' => SWOOLE_PROCESS, 'sock_type' => $defaultListener->GetType()];
     Application::RegisterClass(\swoole_server::class, $initParams);
     $this->swoole = Application::GetInstance(\swoole_server::class);
     //记录一下
     $this->_listenerConfigs[get_class($defaultListener)] = ['Host' => $defaultListener->GetHost(), 'Port' => $defaultListener->GetPort(), 'Type' => $this->_getTypeName($defaultListener->GetType())];
     //注册剩余的Listener
     $this->initListener()->send('');
     $this->initProxy();
     //构造时已经自动完成了回调注册
     Log::Notice(['Listeners' => $this->_listenerConfigs]);
 }
Exemple #9
0
 /**
  * @param \DIServer\Interfaces\IHandler $handler
  *
  * @return \Closure
  */
 private function _createPipeClosure(\DIServer\Interfaces\IHandler $handler)
 {
     $pipeline = new Pipeline();
     $middlewareClasses = $handler->GetMiddlewares();
     $middlewareHandlers = [];
     foreach ($middlewareClasses as $middlewareClass) {
         $refClass = new \ReflectionClass($middlewareClass);
         if (!$refClass->isSubclassOf(IMiddleware::class)) {
             Log::Warning("Try to load {$middlewareClass} in " . get_class($handler) . " but is not instance of IMiddleware");
             continue;
         }
         $middlewareHandlers[] = Container::BuildWithClass($middlewareClass);
     }
     return $pipeline->Through($middlewareHandlers)->Prepared(function ($request) use($handler) {
         //最后一层封装为Handler的默认Handle方法
         return Container::CallMethod($handler, 'Handle', ['request' => $request]);
     });
 }
Exemple #10
0
 /**
  * 触发事件
  *
  * @param string $tag    标签名
  * @param mixed  $params 以引用方式传递于此标签行为的参数数组
  */
 public function Listen($tag, &$params = [])
 {
     if (isset($this->events[$tag])) {
         $startTime = microtime(true);
         foreach ($this->events[$tag] as $event) {
             $res = call_user_func_array($event, $params);
             if ($res === false) {
                 break;
                 //中断执行
             }
         }
         $endTime = microtime(true);
         $costTime = $startTime - $endTime;
         if ($costTime >= 1000) {
             Log::Warning("Call {$tag} event cost {$costTime}ms.");
         }
     }
 }
Exemple #11
0
 /**
  * Handle dynamic, static calls to the object.
  *
  * @param  string $method
  * @param  array  $args
  *
  * @return mixed
  */
 public static function __callStatic($method, $args)
 {
     $instance = static::getFacadeRoot();
     if (!$instance) {
         Log::Critical("A facade call to {$method} has not set instance.");
     }
     switch (count($args)) {
         case 0:
             return $instance->{$method}();
         case 1:
             return $instance->{$method}($args[0]);
         case 2:
             return $instance->{$method}($args[0], $args[1]);
         case 3:
             return $instance->{$method}($args[0], $args[1], $args[2]);
         case 4:
             return $instance->{$method}($args[0], $args[1], $args[2], $args[3]);
         default:
             return call_user_func_array([$instance, $method], $args);
     }
 }
Exemple #12
0
 /**
  * 当工作进程收到由sendMessage发送的管道消息时会触发onPipeMessage
  *
  * @param \swoole_server $server
  * @param int            $from_worker_id
  * @param string         $message
  */
 public function OnTaskWorkerPipeMessage(\swoole_server $server, $from_worker_id, $message)
 {
     Log::Debug("Receive message from {$from_worker_id} in {$server->worker_id}.");
 }
Exemple #13
0
 public function AutoBuildCollection($registryFile, $iface = '')
 {
     $files = $this->GetConventionPaths("/Registry/{$registryFile}");
     if ($this->isAbstract($iface)) {
         $check = function (string $class) use($iface) {
             if (class_exists($class)) {
                 $refClass = new \ReflectionClass($class);
                 if ($refClass->isSubclassOf($iface)) {
                     return true;
                 } else {
                     Log::Warning("Try to auto-build {$class}, but class isn't instance of {$iface}.");
                 }
             } else {
                 Log::Warning("Try to auto-build {$class}, but class not exist.");
             }
             return false;
         };
     } else {
         $check = function (string $class) {
             if (class_exists($class)) {
                 return true;
             } else {
                 Log::Warning("Try to auto-build {$class}, but class not exist.");
             }
             //没有传入iface就不检查默认true
             return false;
         };
     }
     $newClasses = [];
     foreach ($files as $file) {
         if (file_exists($file)) {
             $tempClass = (include $file);
             Ary::MergeRecursive($newClasses, $tempClass);
         }
     }
     $newInstances = [];
     foreach ($newClasses as $key => $newClass) {
         if (is_array($newClass)) {
             foreach ($newClass as $tempClass) {
                 if ($check($newClass)) {
                     $newInstances[$key][] = $this->BuildWithClass($newClass);
                 }
             }
         } else {
             if ($check($newClass)) {
                 $newInstances[$key] = $this->BuildWithClass($newClass);
             }
         }
     }
     return $newInstances;
 }
Exemple #14
0
 public function OnTask(\swoole_server $server, $task_id, $from_id, $param)
 {
     try {
         Event::Listen('OnTaskReceived', [&$server, &$task_id, &$from_id, &$param]);
         //尽量避免TaskWorker直接挂掉。
         $this->taskerServer->OnTask($server, $task_id, $from_id, $param);
         Event::Listen('OnTaskFinished', [&$server, &$task_id, &$from_id, &$param]);
     } catch (\Exception $taskException) {
         Log::Error("Catch exception while tasking: {$taskException->getMessage()}");
     }
 }
Exemple #15
0
 public function OnShutdown(\swoole_server $server)
 {
     Log::Instance()->Notice("On Master Shutdown");
 }
Exemple #16
0
 /**
  * 当工作进程收到由sendMessage发送的管道消息时会触发onPipeMessage
  *
  * @param \swoole_server $server
  * @param int            $from_worker_id
  * @param string         $message
  */
 public function OnPipeMessage(\swoole_server $server, $from_worker_id, $message)
 {
     Event::Listen('OnPipeMessage', [&$server, &$from_worker_id, &$message]);
     Log::Debug("Receive message from {$from_worker_id} in {$server->worker_id}.");
     /** @var \swoole_table $table */
     //$table = unserialize($message);
 }
 public function OnManagerStop(\swoole_server $server)
 {
     Log::Notice("On Manager Stop");
 }
Exemple #18
0
 public function DumpLog()
 {
     Log::Debug($this->_log);
 }
Exemple #19
0
 /**
  * 当工作进程收到由sendMessage发送的管道消息时会触发onPipeMessage
  *
  * @param \swoole_server $server
  * @param int            $from_worker_id
  * @param string         $message
  */
 public function OnWorkerPipeMessage(\swoole_server $server, $from_worker_id, $message)
 {
     Log::Debug("Receive message from {$from_worker_id} in {$server->worker_id}.");
     /** @var \swoole_table $table */
     //$table = unserialize($message);
 }