|
@@ -2055,7 +2055,6 @@ namespace TEAMModelOS.Function
|
|
var json = JsonDocument.Parse(message.Body);
|
|
var json = JsonDocument.Parse(message.Body);
|
|
try
|
|
try
|
|
{
|
|
{
|
|
-
|
|
|
|
json.RootElement.TryGetProperty("id", out JsonElement id);
|
|
json.RootElement.TryGetProperty("id", out JsonElement id);
|
|
json.RootElement.TryGetProperty("progress", out JsonElement progress);
|
|
json.RootElement.TryGetProperty("progress", out JsonElement progress);
|
|
json.RootElement.TryGetProperty("code", out JsonElement code);
|
|
json.RootElement.TryGetProperty("code", out JsonElement code);
|
|
@@ -2078,6 +2077,134 @@ namespace TEAMModelOS.Function
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 統測活動行程進行狀況更新
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="message"></param>
|
|
|
|
+ /// <param name="messageActions"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ [Function("JointEventSchedule")]
|
|
|
|
+ public async Task JointEventFunc(
|
|
|
|
+ [ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "jointevent-schedule", 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 jsonMsg = JsonDocument.Parse(message.Body).RootElement;
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ jsonMsg.TryGetProperty("jointEventId", out JsonElement jointEventId);
|
|
|
|
+ jsonMsg.TryGetProperty("jointScheduleId", out JsonElement jointScheduleId);
|
|
|
|
+ jsonMsg.TryGetProperty("progress", out JsonElement progress);
|
|
|
|
+
|
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
|
|
|
|
+ string PartitionKey = string.Format("{0}{1}{2}{3}{4}{5}{6}", "JointEvent", "-", $"{jointEventId}", "-", "schedule", "-", $"{progress}"); //主key: JointEvent-{jointEventId}-schedule-{progress} RowKey: {jointScheduleId}
|
|
|
|
+ List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", $"{jointScheduleId}" }, { "PartitionKey", PartitionKey } });
|
|
|
|
+
|
|
|
|
+ bool updFlg = false;
|
|
|
|
+ string code = "JointEvent";
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
+
|
|
|
|
+ JointEvent jointEvent = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<JointEvent>(jointEventId.ToString(), new PartitionKey($"{code}"));
|
|
|
|
+ if (jointEvent != null)
|
|
|
|
+ {
|
|
|
|
+ JointEvent.JointEventSchedule jointEventSchedule = jointEvent.schedule.Where(s => s.id.Equals($"{jointScheduleId}")).FirstOrDefault();
|
|
|
|
+ if (jointEventSchedule != null)
|
|
|
|
+ {
|
|
|
|
+ //資料處理
|
|
|
|
+ if (!jointEventSchedule.progress.Equals($"{progress}"))
|
|
|
|
+ {
|
|
|
|
+ jointEventSchedule.progress = $"{progress}";
|
|
|
|
+ updFlg = true;
|
|
|
|
+ }
|
|
|
|
+ if (updFlg)
|
|
|
|
+ {
|
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(jointEvent, jointEvent.id);
|
|
|
|
+ }
|
|
|
|
+ //訊息處理
|
|
|
|
+ switch (jointEventSchedule.progress)
|
|
|
|
+ {
|
|
|
|
+ case "pending":
|
|
|
|
+ var msg = new ServiceBusMessage(new { jointEventId = $"{jointEventId}", jointScheduleId = $"{jointScheduleId}", progress = "going" }.ToJsonString());
|
|
|
|
+ msg.ApplicationProperties.Add("name", "JointEventSchedule");
|
|
|
|
+ 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"), msg, DateTimeOffset.FromUnixTimeMilliseconds(jointEventSchedule.startTime));
|
|
|
|
+ records[0].sequenceNumber = start;
|
|
|
|
+ await table.SaveOrUpdate<ChangeRecord>(records[0]);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), msg, DateTimeOffset.FromUnixTimeMilliseconds(jointEventSchedule.startTime));
|
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
|
+ {
|
|
|
|
+ RowKey = jointEventSchedule.id,
|
|
|
|
+ PartitionKey = PartitionKey,
|
|
|
|
+ sequenceNumber = start,
|
|
|
|
+ msgId = message.MessageId
|
|
|
|
+ };
|
|
|
|
+ await table.Save<ChangeRecord>(changeRecord);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case "going":
|
|
|
|
+ //刪除pending訊息
|
|
|
|
+ string pkey = string.Format("{0}{1}{2}{3}{4}{5}{6}", "JointEvent", "-", $"{jointEventId}", "-", "schedule", "-", "pending");
|
|
|
|
+ await table.DeleteSingle<ChangeRecord>(pkey, jointEventSchedule.id);
|
|
|
|
+ //發送finish訊息
|
|
|
|
+ var messageEnd = new ServiceBusMessage(new { jointEventId = $"{jointEventId}", jointScheduleId = $"{jointScheduleId}", progress = "finish" }.ToJsonString());
|
|
|
|
+ messageEnd.ApplicationProperties.Add("name", "JointEventSchedule");
|
|
|
|
+ if (records.Count > 0)
|
|
|
|
+ {
|
|
|
|
+ long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(jointEventSchedule.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(jointEventSchedule.endTime));
|
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
|
+ {
|
|
|
|
+ RowKey = jointEventSchedule.id,
|
|
|
|
+ PartitionKey = PartitionKey,
|
|
|
|
+ sequenceNumber = end,
|
|
|
|
+ msgId = messageEnd.MessageId
|
|
|
|
+ };
|
|
|
|
+ await table.Save<ChangeRecord>(changeRecord);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case "finish":
|
|
|
|
+ string pk = string.Format("{0}{1}{2}{3}{4}{5}{6}", "JointEvent", "-", $"{jointEventId}", "-", "schedule", "-", "going");
|
|
|
|
+ await table.DeleteSingle<ChangeRecord>(pk, jointEventSchedule.id);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (CosmosException e)
|
|
|
|
+ {
|
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,JointEventScheduleBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.StatusCode}\n{jsonMsg.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,JointEventScheduleBus()\n{ex.Message}\n{ex.StackTrace}\n\n{jsonMsg.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ { // Complete the message
|
|
|
|
+ await messageActions.CompleteMessageAsync(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
private async Task RefreshBlob(string name, string u)
|
|
private async Task RefreshBlob(string name, string u)
|
|
{
|
|
{
|
|
long statr = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
long statr = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|