|
@@ -0,0 +1,107 @@
|
|
|
+using Microsoft.Azure.Cosmos;
|
|
|
+using Azure.Messaging.ServiceBus;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
+using System.Text.Json;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using TEAMModelOS.Function;
|
|
|
+namespace TEAMModelOS.CosmosDBTriggers
|
|
|
+{
|
|
|
+ public class TriggerJointExam
|
|
|
+ {
|
|
|
+ public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
|
|
|
+ CosmosClient client, JsonElement input, TriggerData data, IHttpClientFactory _httpClient, IConfiguration _configuration, HttpTrigger _httpTrigger, AzureRedisFactory _azureRedis)
|
|
|
+ {
|
|
|
+ JointExam info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<JointExam>(data.id, new PartitionKey($"{data.code}"));
|
|
|
+ if (info != null)
|
|
|
+ {
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
|
|
|
+ string PartitionKey = string.Format("{0}{1}{2}{3}{4}", info.code, "-", info.creatorId, "-", info.progress); //JointExam-{info.creatorId}-{info.progress}
|
|
|
+ List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", data.id }, { "PartitionKey", PartitionKey } });
|
|
|
+ switch (info.progress)
|
|
|
+ {
|
|
|
+ case "pending":
|
|
|
+ var message = new ServiceBusMessage(new { id = data.id, progress = "going", code = data.code }.ToJsonString());
|
|
|
+ message.ApplicationProperties.Add("name", "JointExam");
|
|
|
+ if (records.Count > 0)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(data.startTime));
|
|
|
+ records[0].sequenceNumber = start;
|
|
|
+ await table.SaveOrUpdate<ChangeRecord>(records[0]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(data.startTime));
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
+ {
|
|
|
+ RowKey = data.id,
|
|
|
+ PartitionKey = PartitionKey,
|
|
|
+ sequenceNumber = start,
|
|
|
+ msgId = message.MessageId
|
|
|
+ };
|
|
|
+ await table.Save<ChangeRecord>(changeRecord);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "going":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //新增serviceBus 消息释放后处理table
|
|
|
+ string pkey = string.Format("{0}{1}{2}{3}{4}", info.code, "-", info.creatorId, "-", "pending");
|
|
|
+ await table.DeleteSingle<ChangeRecord>(pkey, data.id);
|
|
|
+ // 发送信息通知
|
|
|
+ var messageEnd = new ServiceBusMessage(new { id = data.id, progress = "finish", code = data.code }.ToJsonString());
|
|
|
+ messageEnd.ApplicationProperties.Add("name", "JointExam");
|
|
|
+ if (records.Count > 0)
|
|
|
+ {
|
|
|
+ long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(data.endTime));
|
|
|
+ await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
|
|
|
+ records[0].sequenceNumber = end;
|
|
|
+ await table.SaveOrUpdate<ChangeRecord>(records[0]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(data.endTime));
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
+ {
|
|
|
+ RowKey = data.id,
|
|
|
+ PartitionKey = PartitionKey,
|
|
|
+ sequenceNumber = end,
|
|
|
+ msgId = messageEnd.MessageId
|
|
|
+ };
|
|
|
+ await table.Save<ChangeRecord>(changeRecord);
|
|
|
+ }
|
|
|
+ //生成所有報名教師個人評量
|
|
|
+ await JointService.GenerateExamFromJointExamAsync(client, _azureStorage, _serviceBus, _coreAPIHttpService, _azureRedis, _configuration, _dingDing, info);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-ChangeRecord{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "finish":
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //新增serviceBus 消息释放后处理table
|
|
|
+ string pk = string.Format("{0}{1}{2}{3}{4}", info.code, "-", info.creatorId, "-", "going");
|
|
|
+ await table.DeleteSingle<ChangeRecord>(pk, data.id);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测finish状态异常{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|