CrazyIter_Bin 10 mēneši atpakaļ
vecāks
revīzija
565cec8079

+ 32 - 0
TEAMModelOS.Function/Function.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Threading.Tasks;
+using Azure.Messaging.ServiceBus;
+using Microsoft.Azure.Functions.Worker;
+using Microsoft.Extensions.Logging;
+
+namespace TEAMModelOS.Function
+{
+    public class Function
+    {
+        private readonly ILogger<Function> _logger;
+
+        public Function(ILogger<Function> logger)
+        {
+            _logger = logger;
+        }
+
+        [Function(nameof(Function))]
+        public async Task Run(
+            [ServiceBusTrigger("genpdf", Connection = "Endpoint=sb://coreservicebuscn.servicebus.chinacloudapi.cn/;SharedAccessKeyName=TEAMModelOS;SharedAccessKey=xO8HcvXXuuEkuFI0KlV5uXs8o6vyuVqTR+ASbPGMhHo=")]
+            ServiceBusReceivedMessage message,
+            ServiceBusMessageActions messageActions)
+        {
+            _logger.LogInformation("Message ID: {id}", message.MessageId);
+            _logger.LogInformation("Message Body: {body}", message.Body);
+            _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
+
+            // Complete the message
+            await messageActions.CompleteMessageAsync(message);
+        }
+    }
+}

+ 28 - 2
TEAMModelOS.Function/IESServiceBusTrigger.cs

@@ -27,7 +27,6 @@ using TEAMModelOS.SDK.Services;
 using Azure.Storage.Blobs.Models;
 using static TEAMModelOS.SDK.Models.Service.LessonService;
 using System.Text;
-
 namespace TEAMModelOS.Function
 {
     public class IESServiceBusTrigger
@@ -65,7 +64,34 @@ namespace TEAMModelOS.Function
             _httpTrigger = httpTrigger;
             _backgroundWorkerQueue = backgroundWorkerQueue;
         }
-
+        [Function("GenPDF")]
+        public async Task Run(
+         [ServiceBusTrigger("genpdf", Connection = "Azure:ServiceBus:ConnectionString")]
+            ServiceBusReceivedMessage message,
+         ServiceBusMessageActions messageActions)
+        {
+            _logger.LogInformation("Message ID: {id}", message.MessageId);
+            _logger.LogInformation("Message Body: {body}", message.Body);
+            _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
+            var json = JsonDocument.Parse(message.Body);
+            try
+            {
+              
+                // Complete the message
+            }
+            catch (CosmosException e)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.StatusCode}\n{json.RootElement.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()\n{ex.Message}\n{ex.StackTrace}\n\n{json.RootElement.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+            }
+            finally
+            {  // Complete the message
+                await messageActions.CompleteMessageAsync(message);
+            }
+        }
 
         /// <summary>
         /// UseDevelopmentStorage=true

+ 120 - 0
TEAMModelOS.SDK/Models/Service/GenPDFService.cs

@@ -0,0 +1,120 @@
+using StackExchange.Redis;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+
+namespace TEAMModelOS.SDK.Models.Service
+{
+    public class GenPDFService
+    {
+        public async Task<string> AddGenPdfQueue(AzureServiceBusFactory azureServiceBus , AzureRedisFactory azureRedis, GenPDFData data) 
+        {
+            long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+            List<PDFGenParam> genParams = new List<PDFGenParam>();
+            List<PDFGenParam> dbgenParams = new List<PDFGenParam>();
+            HashEntry[] datas = await azureRedis.GetRedisClient(8).HashGetAllAsync($"PDFGen:{data.sessionId}");
+            if (datas!= null  && datas.Length > 0)
+            { 
+                foreach (var item in datas)
+                {
+                    dbgenParams.Add(item.Value.ToString().ToObject<PDFGenParam>());
+                }
+            }
+            int countProcess= 0;
+            foreach (var item in data.datas)
+            {
+                var dbData = dbgenParams.Find(x => x.id.Equals(item.id));
+                if (dbData!=null)
+                {
+                    
+                    if (dbData.status==0|| dbData.status==1)
+                    {
+                        //不变的
+                        countProcess+=1;
+                    }
+                    else
+                    {
+                        //需要变更的
+                        dbData.status = 0;
+                        dbData.cost=0;
+                        dbData.join= now;
+                        dbData.wait=0;
+                        genParams.Add(dbData);
+                    }
+                }
+                else {
+                    genParams.Add(new PDFGenParam { id = item.id, status=0, cost=0, wait=0, join=now });
+                }
+            }
+            //过期时间 当前个数+ reddis的个数
+            foreach (var item in genParams) 
+            {
+                countProcess+=1;
+                await azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{data.sessionId}", item.id, item.ToJsonString());
+            }
+            long expire = (data.delay+data.timeout) * countProcess ;
+            var tiemSpan = TimeSpan.FromMilliseconds(expire);
+            await azureRedis.GetRedisClient(8).KeyExpireAsync($"PDFGen:{data.sessionId}", tiemSpan);
+            return null; 
+        }
+
+    }
+
+    public class GenPDFData
+    {
+        public string pageUrl { get; set; }
+        public long timeout { get; set; } = 30000;
+        public long delay { get; set; } = 2000;
+        public string orientation { get; set; }
+        public string pageSize { get; set; }
+        public string pageWidth { get; set; }
+        public string pageHeight { get; set; }
+        /// <summary>
+        /// 生成会话id, 活动id
+        /// </summary>
+        public string sessionId { get; set; }
+
+        public List<PDFData> datas { get; set; } = new List<PDFData>();
+    }
+    public record PDFData{
+        /// <summary>
+        /// 学生id
+        /// </summary>
+        public string id { get; set; }
+        /// <summary>
+        /// 数据链接
+        /// </summary>
+        public string url { get; set; }
+       
+    }
+    /// <summary>
+    /// redis 存储数据,超时时间设置为status=0 的count   timeout* count+ delay*count
+    /// </summary>
+    public record PDFGenParam 
+    {
+        /// <summary>
+        /// 学生id
+        /// </summary>
+        public string id { get; set; }
+        /// <summary>
+        /// 0 未执行,1 执行中,2 执行成功,3 执行失败,4超时,5 取消
+        /// </summary>
+        public int status { get; set; } = 0;
+        /// <summary>
+        /// 执行生成 毫秒
+        /// </summary>
+        public long cost { get; set;}
+        /// <summary>
+        /// 等候时间
+        /// </summary>
+        public long wait { get; set;}
+        /// <summary>
+        /// 加入时间
+        /// </summary>
+        public long join     { get; set; }
+    }
+}

+ 2 - 2
TEAMModelOS.sln

@@ -17,11 +17,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contest.Server", "TEAMModel
 EndProject
 Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "contest.client", "TEAMModelContest\contest.client\contest.client.esproj", "{84B6D998-91A7-420D-A409-4C7442B91055}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TEAMModelOS.Function", "TEAMModelOS.Function\TEAMModelOS.Function.csproj", "{1C7F42FE-4644-47C6-92BD-594BE22AB0C9}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TEAMModelOS.Function", "TEAMModelOS.Function\TEAMModelOS.Function.csproj", "{1C7F42FE-4644-47C6-92BD-594BE22AB0C9}"
 EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TEAMModelOS.Lib", "TEAMModelOS.Lib", "{9B74B53F-20E8-46CC-903B-62AEB1583DD7}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TEAMModelOS.HTEXLib", "TEAMModelOS.HTEXLib\TEAMModelOS.HTEXLib.csproj", "{2F580DE8-95D1-40E0-BA23-489A5685950F}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TEAMModelOS.HTEXLib", "TEAMModelOS.HTEXLib\TEAMModelOS.HTEXLib.csproj", "{2F580DE8-95D1-40E0-BA23-489A5685950F}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 42 - 0
TEAMModelOS/Controllers/System/GenPDFController.cs

@@ -0,0 +1,42 @@
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Options;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK;
+using TEAMModelOS.Models;
+using System.Net.Http;
+using System.Collections.Generic;
+
+namespace TEAMModelOS.Controllers
+{
+    [ProducesResponseType(StatusCodes.Status200OK)]
+    [ProducesResponseType(StatusCodes.Status400BadRequest)]
+
+    [Route("gen-pdf")]
+    [ApiController]
+    public class GenPDFController : ControllerBase
+    {
+        private readonly DingDing _dingDing;
+        private readonly IHttpClientFactory _httpClient;
+        private readonly IConfiguration _configuration;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly AzureRedisFactory _azureRedis;
+        private readonly IPSearcher _ipSearcher;
+        private readonly Option _option;
+        private readonly Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator;
+        public GenPDFController(AzureRedisFactory azureRedis, Region2LongitudeLatitudeTranslator longitudeLatitudeTranslator, IHttpClientFactory httpClient, IConfiguration configuration, AzureStorageFactory azureStorage, IPSearcher searcher, DingDing dingDing, IOptionsSnapshot<Option> option)
+        {
+            _httpClient = httpClient;
+            _configuration = configuration;
+            _azureStorage = azureStorage;
+            _ipSearcher = searcher;
+            _dingDing = dingDing;
+            _option = option.Value;
+            _azureRedis=azureRedis;
+            _longitudeLatitudeTranslator = longitudeLatitudeTranslator;
+        }
+
+    }
+
+}