Skip to content

Commit 5ee732a

Browse files
committed
加入重试机制
1 parent 35ef86c commit 5ee732a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

CnBlogPublishTool/ImageUploader.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using CnBlogPublishTool.Util;
44
using MetaWeblogClient;
5+
using Polly;
56

67
namespace CnBlogPublishTool
78
{
@@ -16,9 +17,26 @@ public static void Init(BlogConnectionInfo info)
1617

1718
public static string Upload(string filePath)
1819
{
19-
FileInfo fileinfo = new FileInfo(filePath);
20-
var mediaObjectInfo = BlogClient.NewMediaObject(fileinfo.Name, MimeMapping.GetMimeMapping(filePath), File.ReadAllBytes(filePath));
21-
return mediaObjectInfo.URL;
20+
var policy = Policy.Handle<Exception>().Retry(3,(exception, retryCount) =>
21+
{
22+
Console.WriteLine("上传失败,正在重试 {0},异常:{1}", retryCount,exception.Message);
23+
});
24+
try
25+
{
26+
var url = policy.Execute<string>(() =>
27+
{
28+
FileInfo fileinfo = new FileInfo(filePath);
29+
var mediaObjectInfo = BlogClient.NewMediaObject(fileinfo.Name, MimeMapping.GetMimeMapping(filePath), File.ReadAllBytes(filePath));
30+
return mediaObjectInfo.URL;
31+
});
32+
33+
return url;
34+
}
35+
catch (Exception e)
36+
{
37+
Console.WriteLine("上传失败,异常:{0}",e.Message);
38+
throw;
39+
}
2240
}
2341
}
2442
}

0 commit comments

Comments
 (0)