Browse Source

ServiceBus调整

zhouj1203@hotmail.com 4 năm trước cách đây
mục cha
commit
896ed08488

+ 55 - 42
TEAMModelFunction/MonitorCosmosDB.cs

@@ -64,12 +64,13 @@ namespace TEAMModelFunction
                                 }
                             }
                         }
-                        var message = new ServiceBusMessage(new { id = input[0].Id, name = "Exam", code = code }.ToJsonString());
-                        message.Properties.Add("name", "Exam");
+                       
                         List<ChangeRecord> records = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input[0].Id }, { "PartitionKey", info.progress } });
                         //ChangeRecord record = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input[0].Id, new Azure.Cosmos.PartitionKey($"{info.progress}"));
                         switch (info.progress) {
                             case "pending":
+                                var message = new ServiceBusMessage(new { id = input[0].Id, progress = "going", code = code }.ToJsonString());
+                                message.Properties.Add("name", "Exam");
                                 if (records.Count> 0)
                                 {
                                     await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
@@ -84,7 +85,8 @@ namespace TEAMModelFunction
                                     {
                                         RowKey = input[0].Id,
                                         PartitionKey = "pending",
-                                        sequenceNumber = start
+                                        sequenceNumber = start,
+                                        msgId = message.MessageId
                                     };
                                     await _azureStorage.Save<ChangeRecord>(changeRecord);
                                     //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
@@ -126,15 +128,17 @@ namespace TEAMModelFunction
                                                     result.studentScores.Add(ansPoint);
                                                 }
                                             }
-                                            result.progress = info.progress;
+                                            //result.progress = info.progress;
                                             result.school = info.school;
                                             await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
 
                                         }
                                     }
+                                    var messageEnd = new ServiceBusMessage(new { id = input[0].Id, progress = "finish", code = code }.ToJsonString());
+                                    messageEnd.Properties.Add("name", "Exam");
                                     if (records.Count > 0)
                                     {
-                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                         await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
                                         records[0].sequenceNumber = end;
                                         await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
@@ -142,12 +146,13 @@ namespace TEAMModelFunction
                                     }
                                     else
                                     {
-                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                         ChangeRecord changeRecord = new ChangeRecord
                                         {
                                             RowKey = input[0].Id,
                                             PartitionKey = "going",
-                                            sequenceNumber = end
+                                            sequenceNumber = end,
+                                            msgId = messageEnd.MessageId
                                         };
                                         await _azureStorage.Save<ChangeRecord>(changeRecord);
                                         //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
@@ -158,27 +163,18 @@ namespace TEAMModelFunction
                                 for (int j = 0; j < info.subjects.Count; j++)
                                 {
                                     ExamResult result = new ExamResult();
-                                    result.ttl = -1;
-                                    result.pk = typeof(ExamResult).Name;
-                                    result.code = "ExamResult-" + info.school;
-                                    result.school = info.school;
-                                    result.id = Guid.NewGuid().ToString();
-                                    result.examId = info.id;
-                                    result.subjectId = info.subjects[j].id;
-                                    result.year = info.year;
-                                    result.paper = info.papers[j];
-                                    result.point = info.papers[j].point;
-                                    result.scope = info.scope;
-                                    result.name = info.name;
-                                    //result.time
-
                                     //人数总和
                                     int Count = 0;
                                     int m = 0;
                                     List<ClassRange> classRanges = new List<ClassRange>();
                                     foreach (ExamClassResult classResult in examClassResults)
                                     {
-                                        if (classResult.subjectId.Equals(info.subjects[j].id)) {
+                                        if (classResult.subjectId.Equals(info.subjects[j].id))
+                                        {
+                                            foreach (List<double> scores in classResult.studentScores)
+                                            {
+                                                result.studentScores.Add(scores);
+                                            }
                                             //处理班级信息
                                             ClassRange range = new ClassRange();
                                             range.id = classResult.info.id;
@@ -203,27 +199,36 @@ namespace TEAMModelFunction
                                             foreach (string id in classResult.studentIds)
                                             {
                                                 result.studentIds.Add(id);
-                                            }
-                                            foreach (List<double> scores in classResult.studentScores)
-                                            {
-                                                result.studentScores.Add(scores);
-                                            }
-                                        }                                        
+                                            }                                           
+                                        }
                                     }
                                     result.classes = classRanges;
+                                    result.ttl = -1;
+                                    result.pk = typeof(ExamResult).Name;
+                                    result.code = "ExamResult-" + info.school;
+                                    result.school = info.school;
+                                    result.id = Guid.NewGuid().ToString();
+                                    result.examId = info.id;
+                                    result.subjectId = info.subjects[j].id;
+                                    result.year = info.year;
+                                    result.paper = info.papers[j];
+                                    result.point = info.papers[j].point;
+                                    result.scope = info.scope;
+                                    result.name = info.name;
+                                    //result.time                                    
                                     await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{result.school}"));
                                 }
                                 break;
                         }                       
                         break;
                     case "Vote":
-                        Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input[0].Id, new Azure.Cosmos.PartitionKey($"{code}"));
-                        var messageVote = new ServiceBusMessage(new { id = input[0].Id, name = "Vote", code = code }.ToJsonString());
-                        messageVote.Properties.Add("name", "Vote");
+                        Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input[0].Id, new Azure.Cosmos.PartitionKey($"{code}"));                       
                         List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input[0].Id }, { "PartitionKey", vote.progress } });
                         //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input[0].Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
                         switch (vote.progress) {
                             case "pending":
+                                var messageVote = new ServiceBusMessage(new { id = input[0].Id, progress = "going", code = code }.ToJsonString());
+                                messageVote.Properties.Add("name", "Vote");
                                 if (voteRecords.Count > 0)
                                 {
                                     long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
@@ -239,16 +244,19 @@ namespace TEAMModelFunction
                                     {
                                         RowKey = input[0].Id,
                                         PartitionKey = "pending",
-                                        sequenceNumber = start
+                                        sequenceNumber = start,
+                                        msgId = messageVote.MessageId
                                     };
                                     await _azureStorage.Save<ChangeRecord>(changeRecord);
                                     //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                                 }
                                 break;
                             case "going":
+                                var messageVoteEnd = new ServiceBusMessage(new { id = input[0].Id, progress = "finish", code = code }.ToJsonString());
+                                messageVoteEnd.Properties.Add("name", "Vote");
                                 if (voteRecords.Count > 0)
                                 {
-                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                     await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
                                     voteRecords[0].sequenceNumber = end;
                                     await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
@@ -256,12 +264,13 @@ namespace TEAMModelFunction
                                 }
                                 else
                                 {
-                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                     ChangeRecord changeRecord = new ChangeRecord
                                     {
                                         RowKey = input[0].Id,
                                         PartitionKey = "going",
-                                        sequenceNumber = end
+                                        sequenceNumber = end,
+                                        msgId = messageVoteEnd.MessageId
                                     };
                                     await _azureStorage.Save<ChangeRecord>(changeRecord);
                                     //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
@@ -270,9 +279,7 @@ namespace TEAMModelFunction
                         }
                         break;
                     case "Survey":
-                        Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input[0].Id, new Azure.Cosmos.PartitionKey($"{code}"));
-                        var messageSurvey = new ServiceBusMessage(new { id = input[0].Id, name = "Survey", code = code }.ToJsonString());
-                        messageSurvey.Properties.Add("name", "Survey");
+                        Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input[0].Id, new Azure.Cosmos.PartitionKey($"{code}"));                      
                         //messageSurvey.ScheduledEnqueueTime = DateTimeOffset.FromUnixTimeMilliseconds(stime);
                         //string msgid = messageSurvey.MessageId;
                         List<ChangeRecord> changeRecords =  await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input[0].Id },{"PartitionKey", survey.progress} });
@@ -280,6 +287,8 @@ namespace TEAMModelFunction
                         switch (survey.progress)
                         {
                             case "pending":
+                                var messageSurvey = new ServiceBusMessage(new { id = input[0].Id, progress = "going", code = code }.ToJsonString());
+                                messageSurvey.Properties.Add("name", "Survey");
                                 if (changeRecords.Count > 0)
                                 {
                                     await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
@@ -295,28 +304,32 @@ namespace TEAMModelFunction
                                     {
                                         RowKey = input[0].Id,
                                         PartitionKey = "pending",
-                                        sequenceNumber = start
+                                        sequenceNumber = start,
+                                        msgId = messageSurvey.MessageId
                                     };
                                     await _azureStorage.Save<ChangeRecord>(changeRecord);
                                     //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                                 }
                                 break;
                             case "going":
+                                var messageSurveyEnd = new ServiceBusMessage(new { id = input[0].Id, progress = "finish", code = code }.ToJsonString());
+                                messageSurveyEnd.Properties.Add("name", "Survey");
                                 if (changeRecords.Count > 0)
                                 {
-                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                     await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
                                     changeRecords[0].sequenceNumber = end;
                                     await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
                                 }
                                 else
                                 {
-                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                    long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                                     ChangeRecord changeRecord = new ChangeRecord
                                     {
                                         RowKey = input[0].Id,
                                         PartitionKey = "going",
-                                        sequenceNumber = end
+                                        sequenceNumber = end,
+                                        msgId = messageSurveyEnd.MessageId
                                     };
                                     await _azureStorage.Save<ChangeRecord>(changeRecord);
                                     //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));

+ 35 - 36
TEAMModelFunction/ServiceBusTopic.cs

@@ -27,7 +27,7 @@ namespace TEAMModelFunction
             {
                 var json = JsonDocument.Parse(mySbMsg);                
                 json.RootElement.TryGetProperty("id", out JsonElement id);
-                json.RootElement.TryGetProperty("name", out JsonElement name);
+                json.RootElement.TryGetProperty("progress", out JsonElement progress);
                 json.RootElement.TryGetProperty("code", out JsonElement code);
                 //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
                 var client = _azureCosmos.GetCosmosClient();
@@ -35,19 +35,16 @@ namespace TEAMModelFunction
                 keyValuePairs.TryGetValue("name", out object name);
                 keyValuePairs.TryGetValue("code", out object code);*/
                 //keyValuePairs.TryGetValue("status", out object progress);
-                if (name.ToString().Equals("Exam", StringComparison.OrdinalIgnoreCase))
-                {
                     ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
-                    if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
-                    {
-                        exam.progress = "going";
-                    }
+/*                    if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
+                    {*/
+                    exam.progress = progress.ToString(); ;
+/*                    }
                     else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0)
-                    {
-                        exam.progress = "finish";
-                    }
-                    await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
-                }
+                    {*/
+/*                        exam.progress = "finish";
+                    }*/
+                    await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));             
             } catch (Exception ex) {
                 await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);               
             }
@@ -59,7 +56,7 @@ namespace TEAMModelFunction
             try {
                 var jsonMsg = JsonDocument.Parse(mySbMsg);
                 jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
-                jsonMsg.RootElement.TryGetProperty("name", out JsonElement name);
+                jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
                 jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
                 //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
                 var client = _azureCosmos.GetCosmosClient();
@@ -67,25 +64,26 @@ namespace TEAMModelFunction
                 keyValuePairs.TryGetValue("name", out object name);
                 keyValuePairs.TryGetValue("code", out object code);*/
                 //keyValuePairs.TryGetValue("status", out object progress);
-                if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
-                {
+/*                if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
+                {*/
                     Vote vote;
                     var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     if (sresponse.Status == 200)
                     {
                         using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
                         vote = json.ToObject<Vote>();
-                        if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
-                        {
-                            vote.progress = "going";
-                        }
-                        else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
-                        {
-                            vote.progress = "finish";
-                        }
+                        vote.progress = progress.ToString();
+                    /*if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
+                    {
+                        vote.progress = "going";
+                    }
+                    else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
+                    {
+                        vote.progress = "finish";
+                    }*/
                         await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
                     }
-                }
+/*                }*/
             } catch (Exception ex){
                 await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);               
             }
@@ -97,7 +95,7 @@ namespace TEAMModelFunction
             try {
                 var jsonMsg = JsonDocument.Parse(mySbMsg);
                 jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
-                jsonMsg.RootElement.TryGetProperty("name", out JsonElement name);
+                jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
                 jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
                 //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
                 var client = _azureCosmos.GetCosmosClient();
@@ -105,19 +103,20 @@ namespace TEAMModelFunction
                 keyValuePairs.TryGetValue("name", out object name);
                 keyValuePairs.TryGetValue("code", out object code);*/
                 //keyValuePairs.TryGetValue("status", out object progress);
-                if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
-                {
+                /*if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
+                {*/
                     Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
-                    if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
-                    {
-                        survey.progress = "going";
-                    }
-                    else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
-                    {
-                        survey.progress = "finish";
-                    }
-                    await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
+                /*if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
+                {
+                    survey.progress = "going";
                 }
+                else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
+                {
+                    survey.progress = "finish";
+                }*/
+                    survey.progress = progress.ToString();
+                    await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
+                //}
             }
             catch (Exception ex){
                 await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);                

+ 1 - 0
TEAMModelFunction/model/ChangeRecord.cs

@@ -11,5 +11,6 @@ namespace TEAMModelFunction.model
     public class ChangeRecord : TableEntity
     { 
         public long sequenceNumber { get; set; }
+        public string msgId { get; set; }
     }
 }

+ 2 - 2
TEAMModelFunction/model/ExamClassResult.cs

@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
@@ -28,7 +28,7 @@ namespace TEAMModelFunction
         public string subjectId { get; set; }
         public int year { get; set; }
         public ClassInfo info { get; set; }
-        public string progress { get; set; }
+        public bool progress { get; set; }
         //public PaperSimple paper { get; set; }
         //public List<double> point { get; set; }
         public List<string> studentIds { get; set; }