Skip to content

Commit

Permalink
1.当发生异常:cURL error 35: OpenSSL SSL_connect: SSL_ERROR_SYSCALL in conn…
Browse files Browse the repository at this point in the history
…ection to api.github.com:443 的时候,能够捕获并进行重试。

2.支持记录错误日志,如果开启的话。
  • Loading branch information
niqingyang committed Jun 28, 2020
1 parent 939db5b commit 9967a98
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
75 changes: 72 additions & 3 deletions src/GithubApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class GithubApi
*/
const API_NETWORK_ERROR = - 2;

/**
* 请求错误
*/
const API_REQUEST_ERROR = - 3;

/**
* 数据完整性错误
*/
Expand Down Expand Up @@ -56,6 +61,13 @@ class GithubApi
*/
private static $client;

/**
* 日志文件
*
* @var string|boolean false-禁止记录日志
*/
private static $log_file = false;

/**
* 初始化
*
Expand All @@ -75,6 +87,11 @@ public static function init ($config = [])
{
static::setAccessToken($config['access_token']);
}

if(static::$log_file !== false)
{
static::$log_file = dirname(__DIR__) . '/info.log';
}
}

/**
Expand Down Expand Up @@ -146,9 +163,11 @@ public static function getSha ($owner, $repo, $path)
* 上传的文件路径
* @param string $insertOnly
* 同名文件是否覆盖
* @param integer $retry
* 部分失败的原因会进行尝试重新上传,最多不超过重试次数
* @return array
*/
public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '', $insertOnly = false, $sha = null)
public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '', $insertOnly = false, $sha = null, $retry = 3)
{
if(! file_exists($srcPath))
{
Expand Down Expand Up @@ -208,8 +227,8 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
{
return array(
'code' => $e->getCode(),
'message' => '资源冲突',
'data' => []
'data' => [],
'message' => '资源冲突'
);
}
// 更新资源却没有提供 sha 签名值
Expand Down Expand Up @@ -237,6 +256,20 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
);
}
}
catch(\Exception $e)
{
// 部分失败的原因会导致重试,不超过3次
if($retry > 0 && strpos($e->getMessage(), 'cURL error 35: OpenSSL SSL_connect: SSL_ERROR_SYSCALL') !== false)
{
return static::upload($owner, $repo, $srcPath, $dstPath, $message, $insertOnly, $sha, $retry - 1);
}

return array(
'code' => $e->getCode() == 0 ? 0 : static::API_REQUEST_ERROR,
'data' => [],
'message' => $e->getMessage()
);
}
}

/**
Expand Down Expand Up @@ -380,4 +413,40 @@ private static function normalizerPath ($path, $isfolder = False)

return $path;
}

/**
* 记录错误信息
*
* @param string $message
* @return boolean
*/
public static function error ($message)
{
if(empty(static::$log_file))
{
return false;
}

$date = date('Y-m-d H:i:s');

@file_put_contents(static::$log_file, "[ERROR][{$date}] " . $message . "\n", FILE_APPEND);
}

/**
* 记录日志信息
*
* @param string $message
* @return boolean
*/
public static function info ($message)
{
if(empty(static::$log_file))
{
return false;
}

$date = date('Y-m-d H:i:s');

@file_put_contents(static::$log_file, "[INFO][{$date}] " . $message . "\n", FILE_APPEND);
}
}
20 changes: 19 additions & 1 deletion wordpress-gos.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ function github_file_upload ($object, $file, $opt = array())
if(@file_exists($file))
{
$ret = GithubApi::upload($github_owner, $github_repo, $file, $object);

if($ret['code'] != 0)
{
GithubApi::error(json_encode($ret, JSON_UNESCAPED_UNICODE));
}
else
{
GithubApi::info(json_encode($ret, JSON_UNESCAPED_UNICODE));
}

return $ret;
}
else
{
Expand Down Expand Up @@ -182,7 +193,14 @@ function github_upload_attachments ($metadata)
);

// 执行上传操作
github_file_upload('/' . $object, $file, $opt);
$ret = github_file_upload('/' . $object, $file, $opt);

if($ret !== false && is_array($ret) && $ret['code'] != 0)
{
return [
'error' => $ret['message']
];
}

// 获取URL参数
if(@file_exists($file))
Expand Down

0 comments on commit 9967a98

Please sign in to comment.