Last active
September 8, 2015 08:28
-
-
Save k1ic/1d932946771548ca478d to your computer and use it in GitHub Desktop.
reTry function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 重试 | |
* @param string $class [必选] | |
* @param string $method [必选] | |
* @param array $param [可选] | |
* @param int $retries[可选] | |
* @return array | |
*/ | |
public static function reTry($class, $method, $param = array(), $retries = 2) | |
{ | |
do | |
{ | |
try | |
{ | |
$result = call_user_func_array(array($class, $method), $param); | |
} | |
catch(Exception $e) | |
{ | |
Tool_FSLog::businessError(sprintf("RETRY|".__FUNCTION__."|".self::$_retryAttempts."|error_code:[%s], error:[%s]", $e->getCode(), $e->getMessage())); | |
if (self::$_retryAttempts == $retries) | |
{ | |
return array(); | |
} | |
$result = false; | |
} | |
if ($result !== false) | |
{ | |
self::$_retryAttempts = 0; | |
return $result; | |
} | |
self::$_retryAttempts += 1; | |
}while(self::$_retryAttempts <= $retries); | |
} | |
========================================================================== | |
/** | |
* 重试方法(注:该方法仅对返回结果为空的接口或方法有效) | |
* @param string $class | |
* @param string $func | |
* @param array $params | |
* @param int $retry | |
* @return array() | |
*/ | |
public static function reTry($class = '', $func = '', $params = array(), $retry = 2) | |
{ | |
$retry = intval($retry) > 0 ? intval($retry) : 2; | |
while($retry-- >0) | |
{ | |
try | |
{ | |
$res = call_user_func_array(array($class, $func), $params); | |
if (!empty($res)) | |
{ | |
break; | |
} | |
} | |
catch(Exception $e) | |
{ | |
$res = array(); | |
continue; | |
} | |
} | |
Tool_Log::error(sprintf("RETRY,class:[%s],func:[%s],params:[%s],result:[%s]", $class, $func, json_encode($params), json_encode($res))); | |
return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment