CrazyIter_Bin 2 gadi atpakaļ
vecāks
revīzija
dc53d1369c

+ 26 - 33
TEAMModelOS.FunctionV4/ServiceBus/ActiveTaskTopic.cs

@@ -40,8 +40,7 @@ using System.Net.Http;
 using System.Web;
 using System.Net;
 using System.Net.Http.Json;
-using Microsoft.Azure.ServiceBus;
-using System.Collections.Concurrent;
+using System.Threading;
 
 namespace TEAMModelOS.FunctionV4.ServiceBus
 {
@@ -735,16 +734,10 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
             }
         }
         [Function("GenPdf")]
-        public async Task GenPdfFunc([ServiceBusTrigger("%Azure:ServiceBus:GenPdfQueue%", Connection = "Azure:ServiceBus:ConnectionString",IsBatched =false)] string msg  ,
-             Int32 deliveryCount,
-              DateTime enqueuedTimeUtc, string messageId, ILogger log
-             )
+        public async Task GenPdfFunc([ServiceBusTrigger("%Azure:ServiceBus:GenPdfQueue%", Connection = "Azure:ServiceBus:ConnectionString",IsBatched =false)] string msg  )
         {
             //https://github.com/aafgani/AzFuncWithServiceBus/blob/a0da42f59b5fc45655b73b85bae932e84520db70/ServiceBusTriggerFunction/host.json
             //  messageHandlerOptions 设置
-            string constring = $"{Environment.GetEnvironmentVariable("Azure:ServiceBus:ConnectionString")}";
-            string queue = $"{Environment.GetEnvironmentVariable("Azure:ServiceBus:GenPdfQueue")}";
-            var client = new QueueClient(constring, queue, ReceiveMode.PeekLock);
             JsonElement element = msg.ToObject<JsonElement>();
             // https://dotblogs.com.tw/yc421206/2013/04/25/102300
             // ConcurrentQueue  http://t.zoukankan.com/hohoa-p-12622459.html
@@ -754,23 +747,14 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                     //处理教师线下研修报告的生成。
                     await GenOfflineRecordPdf(element, msg);
                     break;
-                case bool when element.TryGetProperty("bizType", out JsonElement _bizType) && $"{_bizType}".Equals("ArtStudentPdf"):
-                    //处理学生艺术评测报告的生成。
-                    long stime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
-                    await _dingDing.SendBotMsg($"{stime}-艺术评测报告生成中...", GroupNames.成都开发測試群組);
-                    await GenArtStudentPdf(element, msg);
-                    long etime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
-                    await _dingDing.SendBotMsg($"{stime}-{etime}艺术评测报告生成完成!", GroupNames.成都开发測試群組);
-                    break;
+                //case bool when element.TryGetProperty("bizType", out JsonElement _bizType) && $"{_bizType}".Equals("ArtStudentPdf"):
+                //    await GenArtStudentPdf(element, msg);
+                   // break;
             }
-            //await client.CompleteAsync(message.SystemProperties.LockToken);
-            await  client.CloseAsync();
         }
         private async Task GenArtStudentPdf(JsonElement json, string msg) {
             try
             {
-                //string data = await new StreamReader(req.Body).ReadToEndAsync();
-                //var json = JsonDocument.Parse(data).RootElement;
                 json.TryGetProperty("studentPdfs", out JsonElement _studentPdfs);
                 json.TryGetProperty("artResults", out JsonElement _artResults);
                 json.TryGetProperty("schoolCode", out JsonElement _schoolCode);
@@ -790,14 +774,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                     var s = _azureStorage.GetBlobSAS($"{_schoolCode}", x.blob, BlobSasPermissions.Read);
                     s = $"{HttpUtility.UrlEncode($"{s}", Encoding.UTF8)}";
                     string url = $"{atrUrl}?url={s}&pdfpath={x.artId}/report/{x.studentId}";
-                    urls.Add(url);
-                    //var a = list.Find(l => l.Contains(x.studentId));
-                    //if (a != null) {
-
-                    //    a = $"{HttpUtility.UrlEncode($"{a}?{sas}", Encoding.UTF8)}";
-                    //    string url = $"{atrUrl}?url={a}&pdfpath={x.artId}/report/{x.studentId}";
-                    //    urls.Add(url);
-                    //}
+                    urls.Add(url); 
                 });
                 string env = "release";
                 if (Environment.GetEnvironmentVariable("Option:Location").Contains("Test", StringComparison.CurrentCultureIgnoreCase) ||
@@ -807,6 +784,25 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                 }
                 int psize = 20;
                 List<string> resUrls = new List<string>();
+                SpinWait spinWait = new SpinWait(); // 构造SpinWait实例
+               
+                while (true) {
+                    bool lockKey = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"ArtStudentPdf:Lock");
+                    if (!lockKey) {
+                        //锁不存在,先取20个人的数据执行PDF生成,剩下的放在。
+                        var takes= urls.Take(20);
+                        urls.RemoveAll(z=>takes.Contains(z));
+                        await _azureRedis.GetRedisClient(8).HashSetAsync($"ArtStudentPdf:Lock",new RedisValue($"{_schoolCode}"),new RedisValue (urls.ToJsonString()));
+                        await _azureRedis.GetRedisClient(8).KeyExpireAsync($"ArtStudentPdf:Lock", DateTime.UtcNow.AddHours(15));
+                    }
+                    else { 
+                        //锁存在
+                        //锁续期。
+
+                    }
+                    //空转一次
+                    spinWait.SpinOnce();
+                }
                 if (urls.Count <= psize)
                 {
                     var screenshot = new ScreenshotDto
@@ -821,11 +817,9 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                         env = env
                     };
                     var st = screenshot.ToJsonString();
-                    await _dingDing.SendBotMsg($"艺术评测报告生成中:\n{st}", GroupNames.成都开发測試群組);
                     var httpResponse = await _httpClient.PostAsJsonAsync("http://cdhabook.teammodel.cn:8805/screen/screenshot-pdf",
                        screenshot
                     );
-
                     if (httpResponse.StatusCode == HttpStatusCode.OK)
                     {
                         JsonElement json_res = await httpResponse.Content.ReadFromJsonAsync<JsonElement>();
@@ -851,7 +845,6 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                             env = env
                         };
                         var st = screenshot.ToJsonString();
-                        //  await _dingDing.SendBotMsg($"艺术评测报告生成中:\n{st}", GroupNames.成都开发測試群組);
                         var httpResponse = await _httpClient.PostAsJsonAsync("http://cdhabook.teammodel.cn:8805/screen/screenshot-pdf",
                            screenshot
                         );
@@ -863,7 +856,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                         }
                     }
                 }
-               // await response.WriteAsJsonAsync(new { data = new { count = studentPdfs.Count, resUrls } });
+               
             }
             catch (Exception ex)
             {

+ 0 - 1
TEAMModelOS.FunctionV4/TEAMModelOS.FunctionV4.csproj

@@ -55,7 +55,6 @@
 		<!--<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="3.0.9" />-->
 		<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="5.0.0" />
 		<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
-		<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
 		<PackageReference Include="SPS.SBCompressor" Version="0.6.0" />
 		<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
 	</ItemGroup>

+ 4 - 2
TEAMModelOS.FunctionV4/TimeTrigger/IESTimerTrigger.cs

@@ -167,9 +167,11 @@ namespace TEAMModelOS.FunctionV4.TimeTrigger
         /// <param name="myTimer"></param>
         /// <param name="log"></param>
         /// <returns></returns>
-        [Function("CleanUnusedLessonRecord")]
+        //[Function("CleanUnusedLessonRecord")]
         //5分钟一次
-        public async Task CleanUnusedLessonRecord([TimerTrigger("0 */10 15-23 * * *")] TimerInfo myTimer, ILogger log) {
+        public async Task CleanUnusedLessonRecord(
+            //[TimerTrigger("0 */10 15-23 * * *")] TimerInfo myTimer, ILogger log
+            ) {
             string location = Environment.GetEnvironmentVariable("Option:Location");
             try {
                 if (location.Equals("China") || location.Equals("Global"))

+ 0 - 10
TEAMModelOS.FunctionV4/host.json

@@ -1,15 +1,5 @@
 {
   "version": "2.0",
-  "extensions": {
-    "serviceBus": {
-      //"prefetchCount": 100,
-      "messageHandlerOptions": {
-        "autoComplete": false,
-        "maxAutoRenewDuration": "00:55:00",
-        "maxConcurrentCalls": 5
-      }
-    }
-  },
   "cosmosDB": {
     "connectionMode": "Direct",
     "protocol": "Tcp"

+ 13 - 1
TEAMModelOS.SDK/Models/Service/ArtService.cs

@@ -79,9 +79,21 @@ namespace TEAMModelOS.SDK.Models.Service
             return nodes;
         }
 
-        public async static Task<List<ArtStudentPdf>> GenArtPDF(List<StudentArtResult> artResults,string _artId,string _schoolId,string head_lang ,AzureCosmosFactory _azureCosmos, 
+        public async static Task<List<ArtStudentPdf>> GenArtPDF(List<string> studentIds, string _artId,string _schoolId,string head_lang ,AzureCosmosFactory _azureCosmos, 
             IWebHostEnvironment _environment, CoreAPIHttpService _coreAPIHttpService, DingDing _dingDing, AzureServiceBusFactory _serviceBus, IConfiguration _configuration) {
             var client = _azureCosmos.GetCosmosClient();
+
+            string query = $" select value c from c where c.school  = '{_schoolId}' ";
+            if (studentIds.Any())
+            {
+                query = $" select value c from c where c.school  = '{_schoolId}' and c.studentId in ({string.Join(",", studentIds.Select(z => $"'{z}'"))})";
+            }
+            List<StudentArtResult> artResults = new List<StudentArtResult>();
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<StudentArtResult>
+              (queryText: query, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"ArtResult-{_artId}") }))
+            {
+                artResults.Add(item);
+            }
             School school = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>($"{_schoolId}", new PartitionKey("Base"));
             string path = Path.Combine(_environment.ContentRootPath, $"Lang/{head_lang}.json");
             string comment1 = "";

+ 8 - 3
TEAMModelOS/Controllers/School/ArtReviewController.cs

@@ -108,6 +108,11 @@ namespace TEAMModelOS.Controllers
             {
                 studentIds = _studentIds.ToObject<List<string>>();
             }
+
+
+            var messageBlobPDF = new ServiceBusMessage(new { studentIds = studentIds, artId= $"{_artId}", head_lang, schoolCode = $"{_schoolId}", bizType = "ArtStudentPdf" }.ToJsonString());
+            var GenPdfQueue = _configuration.GetValue<string>("Azure:ServiceBus:GenPdfQueue");
+            await _serviceBus.GetServiceBusClient().SendMessageAsync(GenPdfQueue, messageBlobPDF);
             var client = _azureCosmos.GetCosmosClient();
 
             string query = $" select value c from c where c.school  = '{_schoolId}' ";
@@ -121,8 +126,8 @@ namespace TEAMModelOS.Controllers
             {
                 artResults.Add(item);
             }
-            List<ArtStudentPdf> studentPdfs = await ArtService.GenArtPDF(artResults, $"{_artId}", $"{_schoolId}", head_lang, _azureCosmos, _environment, _coreAPIHttpService, _dingDing, _serviceBus, _configuration);
-            return Ok(new { count = studentPdfs.Count(), studentIds = studentPdfs.Select(z => z.studentId) });
+            //List<ArtStudentPdf> studentPdfs = await ArtService.GenArtPDF(artResults, $"{_artId}", $"{_schoolId}", head_lang, _azureCosmos, _environment, _coreAPIHttpService, _dingDing, _serviceBus, _configuration);
+            return Ok(new { /*count = studentPdfs.Count(), studentIds = studentPdfs.Select(z => z.studentId)*/ });
         }
         /// <summary>
         /// 
@@ -402,7 +407,7 @@ namespace TEAMModelOS.Controllers
                                     await client.GetContainer(Constant.TEAMModelOS, Constant.Student).ReplaceItemAsync(rs, rs.id, new PartitionKey(rs.code));
                                 }
                                 if (artResults.Any()) {
-                                    List<ArtStudentPdf> studentPdfs = await ArtService.GenArtPDF(artResults, $"{_artId}", $"{school}", head_lang, _azureCosmos, _environment, _coreAPIHttpService, _dingDing, _serviceBus, _configuration);
+                                    List<ArtStudentPdf> studentPdfs = await ArtService.GenArtPDF(artResults.Select(z=>z.studentId).ToList(), $"{_artId}", $"{school}", head_lang, _azureCosmos, _environment, _coreAPIHttpService, _dingDing, _serviceBus, _configuration);
                                 }
                                 return Ok(new { results = studentArtResults, status = 1 });
                             }