public function handleExecOutRet($stream) { while (($line = fgets($stream, 4096)) !== false) { try { echo $line . PHP_EOL; } catch (\Exception $e) { Logger::logWarn("throw error message: [" . $e->getMessage() . "] error code : [" . $e->getCode() . "]"); } } }
public function httpTaskProcess($r = null) { $query_string = isset($r->server['query_string']) ? trim($r->server['query_string']) : null; if ($query_string) { $args = array(); parse_str($query_string, $args); try { if (isset($args['ip'])) { return IP::find($args['ip']); } } catch (\Exception $e) { Logger::logWarn("throw error message: [" . $e->getMessage() . "] error code : [" . $e->getCode() . "]"); throw new \Exception($e->getMessage()); } } else { throw new \Exception('args is invaild'); } }
public function consumeStart($obj, $cb) { $topic = $this->_getTopic($this->_topicname); /* RD_KAFKA_OFFSET_BEGINNING, RD_KAFKA_OFFSET_END, RD_KAFKA_OFFSET_STORED */ try { $topic->consumeStart($this->_partition, $this->_offset); $counter = 60; while (true && $counter > 0) { $msg = $topic->consume($this->_partition, 1000); if ($msg == null || isset($msg) && $msg->err) { Logger::logWarn('sleep 1s msg error: '); $counter--; sleep(1); continue; } else { if (method_exists($obj, $cb)) { call_user_func_array(array($obj, $cb), array($msg)); } else { throw new \Exception('no callback in obj'); } } } } catch (\RdKafka\Exception $e) { throw new \Exception('kafka error: ' . $e->getMessage()); } }
/** * @param $request * @param $response * @throws Exception */ public function onRequest($request, $response) { /* 下面操作性能出现了比较大的下降 */ try { $msg = ""; $code = 0; $query = array(); if (!isset($request->server['path_info'])) { throw new \Exception('path_info of request->server is invaild'); } //获取class名称和自身路径 $c_arr = $this->getCname($request->server['path_info']); if (!isset($request->server['query_string'])) { $query = array(); } else { parse_str($request->server['query_string'], $query); } if ($this->_getRegisterType($c_arr['cname']) == 'http') { $c = $this->_disPatch($c_arr); $ret = $c->httpTaskProcess($request); if ($this->_swoole_cfg['swoole']['gzip'] === true) { $response->gzip(1); $response->header('Content-Encoding', 'gzip'); } /* default */ $response->header('Content-Type', 'application/json'); $response->header('Content-Type', 'text/html; charset=utf-8'); /* 这里msg的类型发生了变化, 不好的编程风格 */ is_string($ret) ? $msg .= $ret : ($msg = $ret); $code = 1; } elseif ($this->_getRegisterType($c_arr['cname']) == 'process') { $this->_serv->task(array('c_arr' => $c_arr, 'args' => array('query' => $query))); $msg .= " pid: [" . posix_getpid() . "] finished"; $code = 1; } else { $msg = $c_arr['cname'] . " not be register"; $code = 0; } $response->end(json_encode(array('code' => $code, 'msg' => $msg), JSON_UNESCAPED_UNICODE)); } catch (\Exception $e) { $msg = "throw error message: [" . $e->getMessage() . "] error code : [" . $e->getCode() . "]\n"; Logger::logWarn($msg . "" . $e->getTraceAsString()); $response->end(json_encode(array('code' => 0, 'msg' => $msg))); } }
/** * 数据库调试 记录当前SQL * @access protected * @param boolean $start 调试开始标记 true 开始 false 结束 */ protected function debug($start) { if ($this->config['debug']) { // 开启数据库调试模式 if ($start) { G('queryStartTime'); } else { $this->modelSql[$this->model] = $this->queryStr; //$this->model = '_think_'; // 记录操作结束时间 G('queryEndTime'); \lib\log\Logger::logInfo($this->queryStr . ' [ RunTime:' . G('queryStartTime', 'queryEndTime') . 's ]', '', 'SQL'); } } }
/** * 添加和获取页面Trace记录 * @param string $value 变量 * @param string $label 标签 * @param string $level 日志级别 * @param boolean $record 是否记录日志 * @return void|array */ function trace($value = '[think]', $label = '', $level = 'DEBUG', $record = false) { // return Think\Think::trace($value, $label, $level, $record); return \lib\log\Logger::logDebug($value); }