예제 #1
0
 /**
  * 通过CURL执行
  *
  * @param array $hosts
  * @param string $url
  * @param array $param_arr
  * @return array
  */
 protected static function exec_by_curl($hosts, $url, array $param_arr = null)
 {
     $mh = curl_multi_init();
     # 监听列表
     $listener_list = array();
     $vars = http_build_query($param_arr);
     # 创建列队
     foreach ($hosts as $h) {
         # 排除重复HOST
         if (isset($listener_list[$h])) {
             continue;
         }
         list($host, $port) = explode(':', $h, 2);
         if (!$port) {
             # 默认端口
             $port = $_SERVER["SERVER_PORT"];
         }
         # 一个mictime
         $mictime = microtime(1);
         # 生成一个随机字符串
         $rstr = Text::random();
         # 生成一个HASH
         $hash = self::get_hash($vars, $rstr, $mictime);
         # 创建一个curl对象
         $current = HttpCall::_create_curl($host, $port, $url, 10, $hash, $vars, $mictime, $rstr);
         # 列队数控制
         curl_multi_add_handle($mh, $current);
         $listener_list[$h] = $current;
     }
     unset($current);
     $running = null;
     $result = array();
     # 已完成数
     $done_num = 0;
     # 待处理数
     $list_num = count($listener_list);
     do {
         while (($execrun = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM) {
         }
         if ($execrun != CURLM_OK) {
             break;
         }
         while (true == ($done = curl_multi_info_read($mh))) {
             foreach ($listener_list as $done_host => $listener) {
                 if ($listener === $done['handle']) {
                     # 获取内容
                     $result[$done_host] = curl_multi_getcontent($done['handle']);
                     $code = curl_getinfo($done['handle'], CURLINFO_HTTP_CODE);
                     if ($code != 200) {
                         Core::debug()->error('system exec:' . $done_host . ' ERROR,CODE:' . $code);
                         //                             $result[$done_host] = false;
                     } else {
                         # 返回内容
                         Core::debug()->info('system exec:' . $done_host . ' OK.');
                     }
                     curl_close($done['handle']);
                     curl_multi_remove_handle($mh, $done['handle']);
                     unset($listener_list[$done_host], $listener);
                     $done_num++;
                     $time = microtime(1);
                     break;
                 }
             }
         }
         if ($done_num >= $list_num) {
             break;
         }
         if (!$running) {
             break;
         }
     } while (true);
     # 关闭列队
     curl_multi_close($mh);
     return $result;
 }
예제 #2
0
 /**
  * 调用HttpServer执行
  *
  * @param string $storage
  * @param string $uri
  * @param mixed $arg1
  * @param mixed $arg2
  * @return boolean mixed
  */
 protected static function call_http_host($storage, $uri, $arg1 = null, $arg2 = null)
 {
     $param_arr = func_get_args();
     array_shift($param_arr);
     // 把 $storage 移除
     $sync_mode = File::sync_mode();
     if ($sync_mode == 'rsync') {
         // rsync 模式,调用主服执行
         $action = 'master_exec';
     } else {
         // 全部同步执行
         $action = 'sync_exec';
     }
     $rs = call_user_func_array(array(HttpCall::factory($storage), $action), $param_arr);
     if (IS_DEBUG) {
         Core::debug()->log($rs);
     }
     if (is_array($rs)) {
         $i = 0;
         foreach ($rs as $item) {
             $i++;
             if ($item !== 'success') {
                 if (IS_DEBUG) {
                     Core::debug()->error($i . '/' . count($rs), 'call_http_host rs status');
                 }
                 return false;
             }
         }
     } else {
         if ($rs === 'success') {
             return true;
         } else {
             Core::debug()->error('call_http_host error.');
             return false;
         }
     }
     return $rs;
 }
예제 #3
0
 /**
  * 调用HttpServer执行
  *
  * @param string $storage
  * @param string $uri
  * @param mixed $arg1
  * @param mixed $arg2
  * @return boolean mixed
  */
 protected static function call_http_host($storage, $uri, $arg1 = null, $arg2 = null)
 {
     $param_arr = \func_get_args();
     \array_shift($param_arr);
     // 把 $storage 移除
     $sync_mode = static::sync_mode();
     if ($sync_mode == 'rsync') {
         // rsync 模式,调用主服执行
         $action = 'master_exec';
     } else {
         // 全部同步执行
         $action = 'sync_exec';
     }
     $rs = \call_user_func_array(array(\HttpCall::factory($storage), $action), $param_arr);
     if (\IS_DEBUG) {
         \Core::debug()->log($rs);
     }
     if (\is_array($rs)) {
         foreach ($rs as $item) {
             if ($item !== 'success') {
                 return false;
             }
         }
     } else {
         return $rs === 'success' ? true : false;
     }
     return $rs;
 }