服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-05-31 d4c326deaa51e7d4897a84afc339684012b8cfbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Rhea.Common;
using Rhea.Common.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tiger.IBusiness;
 
namespace Tiger.Business
{
    /// <summary>
    /// 事务监控线程
    /// </summary>
    public class TransactionMonitor : ITigerMonitor
    {
        #region Variables
        private WhileThread TransactionMonitorThread;
        #endregion
 
        #region Propertys
        public string Id { get; set; } = Guid.NewGuid().ToString("N");
        public string Tag { get; set; } = "WMSTransactionMonitor";
        public string Name { get; set; } = "WMSTransactionMonitor";
        public bool IsRunning { get; set; }
        #endregion
 
        #region Functions
        public void Start()
        {
            try
            {
                TransactionMonitorThread = new(TransactionMonitoring);
                TransactionMonitorThread.Start();
                Logger.Default.Info("Start Transaction Monitoring Thread");
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, "Start Transaction Monitoring Thread Exception");
            }
        }
        public void Stop()
        {
            try
            {
                TransactionMonitorThread?.Stop();
                Logger.Console.Info("Stop Transaction Monitoring Thread");
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, "Stop Transaction Monitoring Thread Exception");
            }
        }
        private void TransactionMonitoring()
        {
            //Logger.Console.Info("Exec Transaction Monitoring Action");
            //定时删除超时的Transaction
            try
            {
                var timeOutTrans = BizContext.TransactionDic.Where(q => (DateTime.Now - q.Value.LastActionTime).TotalHours > ApiConfig.Transaction_TimeoutHours || q.Value.IsFinished).Select(q => q.Key);
                if (timeOutTrans.Count() > 0)
                {
                    foreach (var key in timeOutTrans)
                    {
                        try
                        {
                            BizContext.TransactionDic[key].Close();
                        }
                        catch (System.Exception ex)
                        {
                            Logger.Console.Fatal(ex, $"删除超时的Transaction[{key}]({BizContext.TransactionDic[key].GetType().FullName})异常");
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.Console.Fatal(ex, "事务监控线程异常");
            }
 
            //休眠10分钟
            Thread.Sleep(10 * 60 * 1000);
        }
        #endregion
    }
}