Пример #1
0
 /**
  * 控制器参数设置的魔术方法
  * @param [type] $name  [description]
  * @param [type] $value [description]
  */
 public function __set($name, $value)
 {
     if (!isset($this->paramRule[$name])) {
         Tool_Log::error("unsupported controller param - {$name}");
         return;
     }
     do {
         $opt = $this->paramRule[$name];
         if (null === $value) {
             if (isset($opt['default'])) {
                 $value = $opt['default'];
                 break;
             }
             $msg = empty($opt['nulmsg']) ? "缺少参数:{$name}" : $opt['nulmsg'];
             $e = new Exception_Api($msg, CODE_PARAM_ERROR);
             $e->name = $name;
             throw $e;
         }
         if (!empty($opt['rule']) && !Comm_Checker::run($value, $opt['rule'])) {
             if (isset($opt['default'])) {
                 $value = $opt['default'];
                 break;
             }
             $msg = empty($opt['errmsg']) ? "参数错误:{$name}" : $opt['errmsg'];
             $e = new Exception_Api($msg, CODE_PARAM_ERROR);
             $e->name = $name;
             throw $e;
         }
     } while (false);
     $this->paramData[$name] = $value;
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
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();
     }
 }
Пример #4
0
 public function delete()
 {
     $options = $this->_parseOptions();
     $data = array();
     $sql = "DELETE FROM {$options['table']} ";
     if (!empty($options['where'])) {
         $sql .= " WHERE {$options['where'][0]}";
         array_shift($options['where']);
         $data = array_merge($data, $options['where']);
     } else {
         // 没有删除条件,不做任何操作
         Tool_Log::error('model no delete condition!');
         return false;
     }
     empty($options['order']) || ($sql .= " ORDER BY {$options['order']}");
     empty($options['limit']) || ($sql .= " LIMIT {$options['limit']}");
     $res = $this->db->exec($sql, $data);
     $this->delCache($options['cache']);
     return $res;
 }
Пример #5
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));
 }
Пример #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;
 }
Пример #7
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));
 }
Пример #8
0
 private function _connect($name)
 {
     $tmp = explode(':', $name, 2);
     $host = empty($tmp[0]) ? '127.0.0.1' : $tmp[0];
     $port = empty($tmp[1]) ? 6379 : $tmp[1];
     $redis = new Redis();
     $res = $redis->connect($host, $port, self::REDIS_TIMEOUT);
     // 如果扩展支持,使用PHP自动序列化
     if (defined('Redis::OPT_SERIALIZER')) {
         $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
     }
     if (!$res) {
         Tool_Log::error("redis connect failed - {$host}:{$port}");
         return false;
     }
     $this->servers[$name] = $redis;
     return true;
 }