Пример #1
0
 /**
  * 开始会话
  */
 public function open()
 {
     if (!$this->_opened) {
         $this->sessionStart();
         if ($this->_sessionId == '') {
             $this->_opened = false;
             Sky::log('Failed to start session.', Logger::LEVEL_WARNING, __METHOD__);
         } else {
             $this->_opened = true;
         }
     }
 }
Пример #2
0
 /**
  * 开始会话
  */
 public function open()
 {
     // this is available in PHP 5.4.0+
     // 		if (function_exists('session_status')) {
     // 			if (session_status() == PHP_SESSION_ACTIVE) {
     // 				$this->_opened = true;
     // 				return;
     // 			}
     // 		}
     if (!$this->_opened) {
         if ($this->getUseCustomStorage()) {
             @session_set_save_handler(array($this, 'openSession'), array($this, 'closeSession'), array($this, 'readSession'), array($this, 'writeSession'), array($this, 'destroySession'), array($this, 'gcSession'));
         }
         @session_start();
         if (session_id() == '') {
             $this->_opened = false;
             $error = error_get_last();
             $message = isset($error['message']) ? $error['message'] : 'Failed to start session.';
             Sky::log($message, Logger::LEVEL_WARNING, __METHOD__);
         } else {
             $this->_opened = true;
         }
     }
 }
Пример #3
0
 /**
  *
  * @param mixed $obj any type of variable to profile memory
  * @param string $msg message to be logged
  * @param string  $category category of the message (e.g. 'system.web'). It is case-insensitive.
  */
 public static function logMemory($obj, $msg = '', $category = 'application')
 {
     if (is_string($obj)) {
         $memory = \Sky\Sky::getLogger()->getMemoryUsage();
         $msg = $obj;
     } else {
         $memory = strlen(serialize($obj));
     }
     $type = gettype($obj);
     \Sky\Sky::log("[{$memory}][{$type}] {$msg}", 'memory', $category);
 }
Пример #4
0
 /**
  * 处理队列中的消息
  * 该方法会先从队列中取出消息,然后放到{@link $_msgArray}中。
  * 当满足读取消息超时或消息间隔超时或收到{@link $maxExecCount}
  * 个消息后,会调用{@link processMsg()}方法。
  * @return boolean
  */
 public function consume()
 {
     echo "begin consume message...\n";
     $pid = getmypid();
     while (!$this->_isFinished) {
         try {
             $this->_beginTime = time();
             $this->_rabbit->queue->consume(array($this, 'myCallback'), 'update' . $pid);
         } catch (\Exception $e) {
             if (!$this->_rabbit->isConnected()) {
                 echo "connection timeout\n";
                 echo $e->getMessage();
                 return false;
             } else {
                 echo "it has been too long since previous message.begin process..\n";
             }
         }
         try {
             $this->cancelQueue('update' . $pid);
             $ret = $this->processMsg($this->_msgArray);
             echo "process " . count($this->_msgArray) . " msg over.\n";
             if (SKY_DEBUG && $this->_counter > 0) {
                 $avgTime = sprintf('%01.2f', $this->_totalTime / $this->_counter);
                 // 					$avgTime=$this->_totalTime/$this->_counter;
                 Sky::log("msg:{$this->_counter} avg queue time:{$avgTime} ms");
             }
             if ($ret === FALSE) {
                 $this->_msgArray = array();
                 return;
             }
         } catch (\Exception $e) {
             echo $e->getMessage();
             $this->_msgArray = array();
             return false;
         }
         $this->_msgArray = array();
         $this->_counter = 0;
         $this->_totalTime = 0;
     }
 }
Пример #5
0
 /**
  * 处理PHP异常错误,如警告,通知。
  *
  * 这个方法实现了一个PHP的error handler。 
  * 它需要常量SKY_ENABLE_ERROR_HANDLER被定义为true。
  *
  * 应用程序将被此方法终止。
  *
  * @param integer $code 发起错误的等级
  * @param string $message 错误消息
  * @param string $file 发起错误的文件
  * @param integer $line 发起错误的行号
  */
 public function handleError($code, $message, $file, $line)
 {
     if ($code & error_reporting()) {
         // disable error capturing to avoid recursive errors
         restore_error_handler();
         restore_exception_handler();
         $log = $message . ' (' . $file . ':' . $line . ")\nStack trace:\n";
         $trace = debug_backtrace();
         // skip the first 3 stacks as they do not tell the error position
         if (count($trace) > 3) {
             $trace = array_slice($trace, 3);
         }
         foreach ($trace as $i => $t) {
             if (!isset($t['file'])) {
                 $t['file'] = 'unknown';
             }
             if (!isset($t['line'])) {
                 $t['line'] = 0;
             }
             if (!isset($t['function'])) {
                 $t['function'] = 'unknown';
             }
             $log .= "#{$i} {$t['file']}({$t['line']}): ";
             if (isset($t['object']) && is_object($t['object'])) {
                 $log .= get_class($t['object']) . '->';
             }
             $log .= "{$t['function']}()\n";
         }
         if (isset($_SERVER['REQUEST_URI'])) {
             $log .= 'REQUEST_URI=' . $_SERVER['REQUEST_URI'];
         }
         $log .= "\n---";
         \Sky\Sky::log($log, \Sky\logging\Logger::LEVEL_ERROR, 'php');
         if (!class_exists('\\Sky\\base\\ErrorHandler', false)) {
             require_once __DIR__ . '/ErrorHandler.php';
         }
         if (($handler = $this->getErrorHandler()) !== null) {
             $handler->handleError($code, $message, $file, $line);
         } else {
             $this->displayError($code, $message, $file, $line);
         }
         $this->end(1);
     }
 }