DiskManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.SCCore;
  5. using Aitex.Core.Util;
  6. using System;
  7. using System.Diagnostics;
  8. namespace MECF.Framework.Common.DataCenter
  9. {
  10. public class DiskManager
  11. {
  12. PeriodicJob _threadMonitorDiskSpace;
  13. Stopwatch _sw = new Stopwatch();
  14. public DiskManager()
  15. {
  16. if (SC.ContainsItem("System.IsEnableMonitorDiskSpaceFunc"))
  17. {
  18. IsEnableMonitorDiskSpaceFunc = SC.GetValue<bool>("System.IsEnableMonitorDiskSpaceFunc");
  19. }
  20. }
  21. public bool IsEnableMonitorDiskSpaceFunc { get; set; }
  22. public void Run()
  23. {
  24. //30分钟运行一次,删除一个月之前的 log文件
  25. _threadMonitorDiskSpace = new PeriodicJob(1000 * 60 * 30, MonitorDiskSpace, "MonitorDiskSpace Thread", false);
  26. _sw.Start();
  27. //防止UI没启动,就进行EV.Post
  28. //Task.Delay(10000).ContinueWith((a) => _threadMonitorDiskSpace.Start()) ;
  29. }
  30. bool MonitorDiskSpace()
  31. {
  32. if (_sw.IsRunning && _sw.ElapsedMilliseconds < 10000)
  33. return true;
  34. _sw.Stop();
  35. try
  36. {
  37. long freeSpace = new long();
  38. long totalSpace = new long();
  39. string hardDiskName = PathManager.GetAppDir().Substring(0, 3);
  40. System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
  41. foreach (System.IO.DriveInfo drive in drives)
  42. {
  43. if (drive.Name == hardDiskName)
  44. {
  45. freeSpace = drive.TotalFreeSpace;
  46. totalSpace = drive.TotalSize;
  47. }
  48. }
  49. if (freeSpace < totalSpace * 0.05)
  50. {
  51. EV.PostAlarmLog("System", $"{hardDiskName} Hard disk Free Space is less than 5% ,need release");
  52. }
  53. else if (freeSpace < totalSpace * 0.1)
  54. {
  55. EV.PostWarningLog("System", $"{hardDiskName} Hard disk Free Space is less than 10%,need release");
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. LOG.Write(ex);
  61. }
  62. return true;
  63. }
  64. public void Stop()
  65. {
  66. _threadMonitorDiskSpace.Stop();
  67. }
  68. ~DiskManager()
  69. {
  70. Stop();
  71. }
  72. }
  73. }