Example #1
0
File: mc.php Project: hitzheng/ares
 /**
  * Memcached函数调用,方法列表及参数参见memcached扩展
  * @param  [type] $func [description]
  * @param  [type] $args [description]
  * @return [type]       [description]
  */
 public function __call($func, $args)
 {
     $res = call_user_func_array(array($this->mc, $func), $args);
     if (false === $res) {
         $info = array('func' => $func, 'args' => $args, 'code' => $this->mc->getResultCode(), 'msg' => $this->mc->getResultMessage());
         Tool_Log::error('mc __call failed - ' . Tool_Json::encode($info));
     }
     return $res;
 }
Example #2
0
 /**
  * 全局异常处理
  * 异常有两个来源
  * 1. 通过set_exception_handler设置的全局未捕获异常
  * 2. 通过Core_App::dispatch()捕获的异常
  * @param  [type] $e [description]
  * @return [type]    [description]
  */
 public function handleException($e)
 {
     $controller = Core_App::getInstance()->controllerObj;
     $isBaseExp = $e instanceof Exception_Base;
     $info = array('exp' => get_class($e), 'code' => $e->getCode(), 'msg' => $e->getMessage(), 'expParams' => $isBaseExp ? $e->getParams() : null, 'line' => sprintf('%s:%d', $e->getFile(), $e->getLine()), 'uri' => Comm_Context::getServer('REQUEST_URI'), 'elapse' => sprintf('%.3f', microtime(true) - SYSTEM_START_TIME), 'reqParams' => empty($controller) ? null : $controller->getParams());
     Tool_Log::error('request exception, ' . Tool_Json::encode($info));
     if ($isBaseExp) {
         // 异常自身处理过程
         $e->handle();
     } else {
         // 默认异常处理过程
         $a = new Exception_Base();
         $a->__real__ = $e;
         $a->handle();
     }
 }
Example #3
0
 private function _logMessage($level, $message, $type)
 {
     if ($level < $this->logLevel) {
         return;
     }
     // 日志文件
     $file = "{$this->logDir}/";
     if ($this->logName) {
         $file .= "{$this->logName}-";
     }
     $file .= date('Ymd') . '.log';
     if ($level >= LOG_LEVEL_WARN) {
         $file .= '.wf';
     }
     // 日志格式
     if (is_array($message) || is_object($message)) {
         $message = Tool_Json::encode($message);
     }
     $log = array($this->_levelToStr($level), date('Y-m-d H:i:s'), $this->logId, $type, $message);
     $message = implode('|', $log);
     @file_put_contents($file, $message . PHP_EOL, FILE_APPEND);
 }
Example #4
0
 private function _logmsg($func, $args, $code, $msg)
 {
     $info = array('func' => $func, 'args' => $args, 'code' => $code, 'msg' => $msg);
     Tool_Log::error('redis failed - ' . Tool_Json::encode($info));
 }
Example #5
0
 public function handle()
 {
     Tool_Json::api($this->code, $this->message);
 }
Example #6
0
 private function _childLoop()
 {
     $index = $this->child['index'];
     Tool_Log::info("daemon:{$this->name} child[{$index}] start");
     ini_set('memory_limit', $this->memLimit);
     Tool_Proc::setTitle("daemon:{$this->name} child[{$index}]");
     // 运行daemon初始化
     $this->init();
     while (true) {
         try {
             // 检查子进程状态
             if (!$this->_checkChild()) {
                 break;
             }
             // 处理任务
             if ($this->handle()) {
                 $this->child['total_count']++;
                 continue;
             }
         } catch (Exception $e) {
             $info = array('daemon' => $this->name, 'exp' => get_class($e), 'code' => $e->getCode(), 'msg' => $e->getMessage(), 'expParams' => $e instanceof Exception_Base ? $e->getParams() : null, 'line' => sprintf('%s:%d', $e->getFile(), $e->getLine()));
             $this->child['error_count']++;
             Tool_Log::error('daemon exception, ' . Tool_Json::encode($info));
         }
         usleep($this->idleSleep * 1000 * 1000);
     }
     Tool_Log::info("daemon {$this->name} child[{$index}] stop");
     fclose($this->child['socket']);
     exit;
 }
Example #7
0
 private function _loadParams()
 {
     if (is_string($this->params)) {
         // 已编码数据
         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->params);
         return;
     }
     switch ($this->method) {
         case 'GET':
             // GET方法的参数补到url后面
             $url = $this->url . (strpos($this->url, '?') ? '&' : '?') . http_build_query($this->params);
             curl_setopt($this->ch, CURLOPT_URL, $url);
             break;
         case 'POST':
             // POST方法时检查并处理上传文件
             if ($this->_checkUpload()) {
                 break;
             }
         default:
             if ('json' == $this->encode) {
                 $params = Tool_Json::encode($this->params);
                 $this->addHeader('Content-Type', 'application/json');
             } else {
                 // Content-Type: application/x-www-form-urlencoded
                 $params = http_build_query($this->params);
             }
             curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params);
             break;
     }
 }
Example #8
0
 private function _logmsg($func, $args)
 {
     $info = array('func' => $func, 'args' => $args, 'code' => $this->mc->getResultCode(), 'msg' => $this->mc->getResultMessage());
     Tool_Log::error('memcached failed - ' . Tool_Json::encode($info));
 }
Example #9
0
 private function _doStop()
 {
     if (true !== $this->controllerObj->getLogRequest()) {
         return;
     }
     $info = array('elapse' => sprintf('%.3f', microtime(true) - SYSTEM_START_TIME), 'uri' => Comm_Context::getServer('REQUEST_URI'), 'params' => $this->controllerObj->getParams());
     Tool_Log::info('request ok, ' . Tool_Json::encode($info));
 }
Example #10
0
 /**
  * Redis函数调用,调用方法及参数见phpredis
  * !!注意!! 在多server下,若不期望使用一致hash或随机
  * 选择reids实例,则不能使用本类进行操作
  * @param  [type] $func [description]
  * @param  [type] $args [description]
  * @return [type]       [description]
  */
 public function __call($func, $args)
 {
     $key = null;
     if (isset($args[0]) && is_string($args[0])) {
         $key = $args[0];
     }
     $redis = $this->getServer($key);
     try {
         return call_user_func_array(array($redis, $func), $args);
     } catch (RedisException $e) {
         $err = array('func' => $func, 'args' => $args, 'code' => $e->getCode(), 'msg' => $e->getMessage());
         Tool_Log::error('redis error - ' . Tool_Json::encode($err));
         return false;
     }
 }