Browse Source

增加消息跟踪

develop
H Vs 1 year ago
parent
commit
0e37ba123a
10 changed files with 117 additions and 56 deletions
  1. +0
    -7
      HealthMonitor.Common/Class1.cs
  2. +4
    -0
      HealthMonitor.Common/HealthMonitor.Common.csproj
  3. +19
    -0
      HealthMonitor.Common/MessageTypeUtils.cs
  4. +0
    -7
      HealthMonitor.Model/Class1.cs
  5. +15
    -0
      HealthMonitor.Model/Enum/MessageType.cs
  6. +13
    -0
      HealthMonitor.Model/Enum/TopicType.cs
  7. +2
    -2
      HealthMonitor.Service/Resolver/BloodpressResolver.cs
  8. +15
    -3
      HealthMonitor.Service/Sub/MsgQueueManager.cs
  9. +29
    -25
      HealthMonitor.Service/Sub/TDengineDataSubcribe.cs
  10. +20
    -12
      HealthMonitor.WebApi/PackageProcess.cs

+ 0
- 7
HealthMonitor.Common/Class1.cs View File

@@ -1,7 +0,0 @@
namespace HealthMonitor.Common
{
public class Class1
{

}
}

+ 4
- 0
HealthMonitor.Common/HealthMonitor.Common.csproj View File

@@ -11,4 +11,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HealthMonitor.Model\HealthMonitor.Model.csproj" />
</ItemGroup>

</Project>

+ 19
- 0
HealthMonitor.Common/MessageTypeUtils.cs View File

@@ -0,0 +1,19 @@
using HealthMonitor.Model.Enum;

namespace HealthMonitor.Common
{
public class MessageTypeUtils
{
public static string TryToDescription(int type)
{
//if (type >= (int)TopicType.Property)
//{
// return ((MessageType)type).ToDescription();
//}
//else return "Unknown";

return "Unknown";
}
}
}

+ 0
- 7
HealthMonitor.Model/Class1.cs View File

@@ -1,7 +0,0 @@
namespace HealthMonitor.Model
{
public class Class1
{

}
}

+ 15
- 0
HealthMonitor.Model/Enum/MessageType.cs View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HealthMonitor.Model.Enum
{
public enum MessageType
{
[Description(nameof(BloodPressInfo))]
BloodPressInfo,
}
}

+ 13
- 0
HealthMonitor.Model/Enum/TopicType.cs View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HealthMonitor.Model.Enum
{
public enum TopicType
{
Property = 0x200,
}
}

+ 2
- 2
HealthMonitor.Service/Resolver/BloodpressResolver.cs View File

@@ -76,14 +76,14 @@ namespace HealthMonitor.Service.Resolver
//验证这个信息是否存在
if (person == null || person?.Person.BornDate == null)
{
_logger.LogWarning("验证这个信息是否存在");
_logger.LogWarning("验证个人信息,找不到个人信息,跳过此消息");
return;
}
// 验证年龄是否在范围 (2 - 120)
var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!);
if (age < 1 || age > 120)
{
_logger.LogWarning("验证年龄是否在范围 (2 - 120)");
_logger.LogWarning("验证年龄,不在范围 (2 - 120)岁,跳过此消息");
return;
}
#endregion


+ 15
- 3
HealthMonitor.Service/Sub/MsgQueueManager.cs View File

@@ -20,9 +20,21 @@ namespace HealthMonitor.Service.Sub
_resolver = resolver;
}

public IResolver? GetMsgResolver()
{
TryDequeue(out var msg);
//public IResolver? GetMsgResolver()
//{
// TryDequeue(out var msg);
// if (msg == null)
// {
// return null;
// }
// _resolver.SetResolveInfo(msg);

// return _resolver;
//}

public IResolver? GetMsgResolver(PackageMsgModel msg)
{
//TryDequeue(out var msg);
if (msg == null)
{
return null;


+ 29
- 25
HealthMonitor.Service/Sub/TDengineDataSubcribe.cs View File

@@ -38,7 +38,7 @@ namespace HealthMonitor.Service.Sub

public TDengineDataSubcribe(

TDengineService serviceDengine,
TDengineService serviceTDengine,
PersonCacheManager personCacheMgr,
BloodPressReferenceValueCacheManager bpRefValCacheManager,
IResolverFactory resolverFactory,
@@ -47,7 +47,7 @@ namespace HealthMonitor.Service.Sub
ILogger<TDengineDataSubcribe> logger
)
{
_serviceTDengine = serviceDengine;
_serviceTDengine = serviceTDengine;
_personCacheMgr = personCacheMgr;
_bpRefValCacheManager = bpRefValCacheManager;
_logger = logger;
@@ -80,28 +80,30 @@ namespace HealthMonitor.Service.Sub

public void DoTDengineConnect()
{
string host = _configTDengineService.Host;
short port = 6030;
string username = _configTDengineService.UserName;
string password = _configTDengineService.Password;
string dbname = _configTDengineService.DB;
//#if DEBUG
// //string configDir = "C:/TDengine/cfg";
// //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir);
// TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing");
//#endif
var conn = TDengine.Connect(host, username, password, dbname, port);

if (conn == IntPtr.Zero)
{
_logger.LogError("reason:{TDengine.Error(conn)}", TDengine.Error(conn));
}
else
{
_logger.LogInformation($"连接 TDengine 成功....");
}
// string host = _configTDengineService.Host;
// short port = 6030;
// string username = _configTDengineService.UserName;
// string password = _configTDengineService.Password;
// string dbname = _configTDengineService.DB;
////#if DEBUG
//// //string configDir = "C:/TDengine/cfg";
//// //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir);
//// TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing");
////#endif
// var conn = TDengine.Connect(host, username, password, dbname, port);

// if (conn == IntPtr.Zero)
// {
// _logger.LogError("reason:{TDengine.Error(conn)}", TDengine.Error(conn));

// }
// else
// {
// _logger.LogInformation($"连接 TDengine 成功....");

// }

var conn = _serviceTDengine.Connection();
DoReceive(conn);
}

@@ -137,6 +139,7 @@ namespace HealthMonitor.Service.Sub

if (TDengine.ErrorNo(res) != 0)
{
_logger.LogError($"create topic failed, reason:{TDengine.Error(res)}");
throw new Exception($"create topic failed, reason:{TDengine.Error(res)}");
}

@@ -187,7 +190,8 @@ namespace HealthMonitor.Service.Sub
}
consumer.Commit(consumeRes);
_logger.LogInformation("监听中....");
//_logger.LogInformation("监听中....");
Console.WriteLine("监听中....");
}

// close consumer after use.Otherwise will lead memory leak.


+ 20
- 12
HealthMonitor.WebApi/PackageProcess.cs View File

@@ -1,4 +1,5 @@
using HealthMonitor.Service.Resolver.Interface;
using HealthMonitor.Common;
using HealthMonitor.Service.Resolver.Interface;
using HealthMonitor.Service.Sub;
using HealthMonitor.Service.Sub.Interface;
using System.Collections.Concurrent;
@@ -20,22 +21,29 @@ namespace HealthMonitor.WebApi

public async Task<bool> ResolveAsync()
{
var resolver = _msgQueueManager.GetMsgResolver();

try
_msgQueueManager.TryDequeue(out var msg);
if (msg == null) return false;
var resolver = _msgQueueManager.GetMsgResolver(msg!);
using (_logger.BeginScope(new Dictionary<string, object> { ["RequestId"] = msg!.MessageId }))
using (new CustomizeStopWatch(msg!.Topic, _logger))
{
if (resolver != null)
try
{
await resolver.ExecuteMessageAsync().ConfigureAwait(false);

if (resolver != null)
{
await resolver.ExecuteMessageAsync().ConfigureAwait(false);
}

}
catch (Exception ex)
{
_logger.LogError($"未处理异常 message: {ex.Message}\n {ex.StackTrace}");
}

}
catch (Exception ex)
{
_logger.LogError($"未处理异常 message: {ex.Message}\n {ex.StackTrace}");
}

return true;

}


Loading…
Cancel
Save