Skip to content

Commit 9967a98

Browse files
committed
1.当发生异常:cURL error 35: OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to api.github.com:443 的时候,能够捕获并进行重试。
2.支持记录错误日志,如果开启的话。
1 parent 939db5b commit 9967a98

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/GithubApi.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class GithubApi
2727
*/
2828
const API_NETWORK_ERROR = - 2;
2929

30+
/**
31+
* 请求错误
32+
*/
33+
const API_REQUEST_ERROR = - 3;
34+
3035
/**
3136
* 数据完整性错误
3237
*/
@@ -56,6 +61,13 @@ class GithubApi
5661
*/
5762
private static $client;
5863

64+
/**
65+
* 日志文件
66+
*
67+
* @var string|boolean false-禁止记录日志
68+
*/
69+
private static $log_file = false;
70+
5971
/**
6072
* 初始化
6173
*
@@ -75,6 +87,11 @@ public static function init ($config = [])
7587
{
7688
static::setAccessToken($config['access_token']);
7789
}
90+
91+
if(static::$log_file !== false)
92+
{
93+
static::$log_file = dirname(__DIR__) . '/info.log';
94+
}
7895
}
7996

8097
/**
@@ -146,9 +163,11 @@ public static function getSha ($owner, $repo, $path)
146163
* 上传的文件路径
147164
* @param string $insertOnly
148165
* 同名文件是否覆盖
166+
* @param integer $retry
167+
* 部分失败的原因会进行尝试重新上传,最多不超过重试次数
149168
* @return array
150169
*/
151-
public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '', $insertOnly = false, $sha = null)
170+
public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '', $insertOnly = false, $sha = null, $retry = 3)
152171
{
153172
if(! file_exists($srcPath))
154173
{
@@ -208,8 +227,8 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
208227
{
209228
return array(
210229
'code' => $e->getCode(),
211-
'message' => '资源冲突',
212-
'data' => []
230+
'data' => [],
231+
'message' => '资源冲突'
213232
);
214233
}
215234
// 更新资源却没有提供 sha 签名值
@@ -237,6 +256,20 @@ public static function upload ($owner, $repo, $srcPath, $dstPath, $message = '',
237256
);
238257
}
239258
}
259+
catch(\Exception $e)
260+
{
261+
// 部分失败的原因会导致重试,不超过3次
262+
if($retry > 0 && strpos($e->getMessage(), 'cURL error 35: OpenSSL SSL_connect: SSL_ERROR_SYSCALL') !== false)
263+
{
264+
return static::upload($owner, $repo, $srcPath, $dstPath, $message, $insertOnly, $sha, $retry - 1);
265+
}
266+
267+
return array(
268+
'code' => $e->getCode() == 0 ? 0 : static::API_REQUEST_ERROR,
269+
'data' => [],
270+
'message' => $e->getMessage()
271+
);
272+
}
240273
}
241274

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

381414
return $path;
382415
}
416+
417+
/**
418+
* 记录错误信息
419+
*
420+
* @param string $message
421+
* @return boolean
422+
*/
423+
public static function error ($message)
424+
{
425+
if(empty(static::$log_file))
426+
{
427+
return false;
428+
}
429+
430+
$date = date('Y-m-d H:i:s');
431+
432+
@file_put_contents(static::$log_file, "[ERROR][{$date}] " . $message . "\n", FILE_APPEND);
433+
}
434+
435+
/**
436+
* 记录日志信息
437+
*
438+
* @param string $message
439+
* @return boolean
440+
*/
441+
public static function info ($message)
442+
{
443+
if(empty(static::$log_file))
444+
{
445+
return false;
446+
}
447+
448+
$date = date('Y-m-d H:i:s');
449+
450+
@file_put_contents(static::$log_file, "[INFO][{$date}] " . $message . "\n", FILE_APPEND);
451+
}
383452
}

wordpress-gos.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ function github_file_upload ($object, $file, $opt = array())
105105
if(@file_exists($file))
106106
{
107107
$ret = GithubApi::upload($github_owner, $github_repo, $file, $object);
108+
109+
if($ret['code'] != 0)
110+
{
111+
GithubApi::error(json_encode($ret, JSON_UNESCAPED_UNICODE));
112+
}
113+
else
114+
{
115+
GithubApi::info(json_encode($ret, JSON_UNESCAPED_UNICODE));
116+
}
117+
118+
return $ret;
108119
}
109120
else
110121
{
@@ -182,7 +193,14 @@ function github_upload_attachments ($metadata)
182193
);
183194

184195
// 执行上传操作
185-
github_file_upload('/' . $object, $file, $opt);
196+
$ret = github_file_upload('/' . $object, $file, $opt);
197+
198+
if($ret !== false && is_array($ret) && $ret['code'] != 0)
199+
{
200+
return [
201+
'error' => $ret['message']
202+
];
203+
}
186204

187205
// 获取URL参数
188206
if(@file_exists($file))

0 commit comments

Comments
 (0)