Browse Source

mxg:添加上传失败重试,重试间隔为5s,重试次数4

mengxiangge 5 years ago
parent
commit
1c312b7ee0

+ 6 - 0
Executer/MBIRevitBase/MBIRevitBase.csproj

@@ -31,6 +31,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Polly, Version=7.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
+      <HintPath>..\..\packages\Polly.7.1.1\lib\netstandard2.0\Polly.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.IO.Compression" />
@@ -52,5 +55,8 @@
     <Compile Include="Tools\HttpUtils.cs" />
     <Compile Include="Tools\ZipUtils.cs" />
   </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 33 - 2
Executer/MBIRevitBase/Services/UploadService.cs

@@ -8,10 +8,12 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Cryptography.X509Certificates;
 using System.Text;
 using System.Threading.Tasks;
 using MBIRevitBase.Config;
 using MBIRevitBase.Tools;
+using Polly;
 
 namespace MBIRevitBase.Services
 {
@@ -20,16 +22,45 @@ namespace MBIRevitBase.Services
     /// </summary>
     public class UploadService
     {
+        public static BResult UploadExportFile(string result)
+        {
+            return DelayRetryPolicy(result);
+        }
         /// <summary>
         /// 上传导出文件
         /// </summary>
         /// <param name="result"></param>
         /// <returns></returns>
-        public static BResult UploadExportFile(string result)
+        public static T UploadExportFileRetry<T>(string result)where T:BResult
         {
             var stream = ZipUtils.ZipString(result, "export.json");
             var url = ApiConfig.AlgorithmUrl() + @"/upload-json-zip/upload";
-            return HttpUtils.PostFormDataFile(url, stream);
+            Console.WriteLine("BeginUploadData");
+            return HttpUtils.PostFormDataFileThrowException(url, stream) as T;
+        }
+
+        static BResult DelayRetryPolicy(string result)
+        {
+            try
+            {
+                var waitAndRetryPolicy = Policy.Handle<Exception>().WaitAndRetry(new[]
+                {
+                    TimeSpan.FromSeconds(5),
+                    TimeSpan.FromSeconds(5),
+                    TimeSpan.FromSeconds(5),
+                    TimeSpan.FromSeconds(5)
+                }, ReportError);
+                return waitAndRetryPolicy.Execute<BResult>(()=> UploadExportFileRetry<BResult>(result));
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine($"Execute failed,Message:{e.Message}");
+                return e.Message;
+            }
+        }
+        static void ReportError(Exception e, TimeSpan timeSpan, int intento, Context context)
+        {
+            Console.WriteLine($"异常{intento:00} <调用秒数:{timeSpan.Seconds} 秒>\t 执行时间:{DateTime.Now}");
         }
     }
 }

+ 43 - 0
Executer/MBIRevitBase/Tools/HttpUtils.cs

@@ -61,6 +61,49 @@ namespace MBIRevitBase.Tools
                 }
             }
             return string.Empty;        
+
+        }
+        /// <summary>
+        /// 上传失败抛出异常,进行重试
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="stream"></param>
+        /// <returns></returns>
+        public static BResult PostFormDataFileThrowException(string url, Stream stream)
+        {
+            using (HttpClient client = CreateClient())
+            {
+                string boundary = string.Format("{0}{1}", WebKitFormBoundary, DateTime.Now.Ticks.ToString("x"));
+                MultipartFormDataContent content = new MultipartFormDataContent(boundary);
+                #region Stream请求
+                var streamContent = new StreamContent(stream);
+                //"file.zip"必须有,它的格式可能影响到内部的一些配置
+                content.Add(streamContent, "file", "file.zip");
+                #endregion
+                var result = client.PostAsync(url, content).Result;
+                try
+                {
+                    if (result.IsSuccessStatusCode)
+                    {
+                        string rslt = result.Content.ReadAsStringAsync().Result;
+                        return new BResult(true, rslt);
+                    }
+                    else
+                    {
+                        throw new Exception(result.ToString());
+                    }
+                }
+                catch
+                {
+                    throw;
+                }
+                finally
+                {
+                    client.Dispose();
+                }
+            }
+            return string.Empty;
+
         }
     }
 }

+ 4 - 0
Executer/MBIRevitBase/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Polly" version="7.1.1" targetFramework="net461" />
+</packages>