|
@@ -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)
|
|
|
{
|