예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function stop()
 {
     $pid = posix_getpid();
     if ($this->parentPid != $pid) {
         //kill the thread
         posix_kill($pid, SIGTERM);
         //TODO
     } elseif ($this->parentPid == $pid && null !== $this->server) {
         $this->server->shutdown();
         $this->server = null;
     }
 }
예제 #2
0
 public function actionRun($hostinfo = '0.0.0.0:9580')
 {
     $this->parseHostPort($hostinfo);
     $http = new \swoole_http_server($this->host, $this->port);
     //初始化进程数 ,关联getTaskId函数的使用
     $task_num = 0;
     foreach (self::$q_config as $key => $val) {
         self::$event_base[$key] = $task_num;
         self::$cnt[$key] = 0;
         $task_num += $val;
     }
     //初始化配置
     $this->config['task_worker_num'] = $task_num;
     $http->set($this->config);
     //接受http请求参数
     $http->on('request', function ($request, $response) use($http) {
         if (!isset($request->get['opt'])) {
             $response->end('HTTP_OPT_ERROR:missing opt');
             return;
         }
         $opt = $request->get['opt'];
         if ($opt == 'put') {
             //获取data数据
             if (isset($request->get['data'])) {
                 $data = $request->get['data'];
             } elseif (isset($request->post['data'])) {
                 $data = $request->post['data'];
             } else {
                 $response->end('HTTP_PARAM_ERROR:missing data');
                 return;
             }
             $data = json_decode($data, true);
             if (json_last_error() != JSON_ERROR_NONE) {
                 $response->end(json_last_error_msg());
                 return;
             }
             if (!isset($data['class']) || !isset(self::$q_config[$data['class']])) {
                 $type = 'Common';
             } else {
                 $type = $data['class'];
             }
             $taskId = $this->getTaskId($type);
             if (!isset($data['task'])) {
                 $response->end('HTTP_PARAM_ERROR:missing task');
                 return;
             }
             //分配给task的worker
             $http->task($data, $taskId);
             $response->end("HTTP_PUT_OK");
         } elseif ($opt == 'reload') {
             $http->reload();
             $response->end('reset now!');
             return;
         } elseif ($opt == 'shutdown') {
             $http->shutdown();
             $response->end('shutdown now!');
             return;
         } else {
             $response->end("HTTP_OPT_ERROR:nothing to do");
             return;
         }
     });
     //任务调度
     $http->on('Task', function ($http, $taskId, $fromId, $data) {
         $rs = $this->doJob($data);
         return $rs;
     });
     //任务完成的响应事件
     $http->on('Finish', function ($http, $taskId, $data) {
         echo "finish task,the result:" . $data . "\n";
     });
     $http->start();
 }