RetentionWorker.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using BO.ProcessServer.Business.Workers.Contexts;
  4. using Quadarax.Foundation.Core.Logging;
  5. using Quadarax.Foundation.Core.Thread;
  6. namespace BO.ProcessServer.Business.Workers
  7. {
  8. public class RetentionWorker : LoopWorker
  9. {
  10. #region *** Constants ***
  11. private const string CS_WK_NAME = "Retention";
  12. #endregion
  13. #region *** Private fields ***
  14. #endregion
  15. #region *** Properties ***
  16. #endregion
  17. #region *** Constructors ***
  18. public RetentionWorker(IWorkerContext context) : base(CS_WK_NAME, context)
  19. {
  20. IterationsDelayBetween = ((CfgCtx)context).Configuration.System.Retention.Interval;
  21. }
  22. public RetentionWorker(string workerName, IWorkerContext context) : base(workerName, context)
  23. {
  24. }
  25. public RetentionWorker(string workerName) : base(workerName)
  26. {
  27. }
  28. public RetentionWorker(string workerName, IWorkerContext context, ILogger logger = null) : base(workerName, context, logger)
  29. {
  30. }
  31. #endregion
  32. #region *** Iteration ***
  33. protected override async Task DoIteration(IWorkerContext context)
  34. {
  35. var ctx = (CfgCtx)context;
  36. if (!ctx.Configuration.System.Retention.Enabled)
  37. return;
  38. Log(LogSeverityEnum.Debug,"Retention iteration started...");
  39. var maxFiles = ctx.Configuration.System.Retention.Items;
  40. var maxSize = ctx.Configuration.System.Retention.Size;
  41. var delDoneFiles = 0;
  42. var delFailFiles = 0;
  43. var delDoneSize = 0;
  44. var delFailSize = 0;
  45. if (maxFiles > 0)
  46. {
  47. // retention by files count
  48. delDoneFiles = DeleteRetentionByCount(ctx,Pool.PoolTypeEnum.Done, maxFiles);
  49. delFailFiles = DeleteRetentionByCount(ctx,Pool.PoolTypeEnum.Fail, maxFiles);
  50. }
  51. if (maxSize > 0)
  52. {
  53. // retention by size
  54. delDoneSize = DeleteRetentionBySize(ctx, Pool.PoolTypeEnum.Done, maxSize);
  55. delFailSize = DeleteRetentionBySize(ctx, Pool.PoolTypeEnum.Fail, maxSize);
  56. }
  57. Log(LogSeverityEnum.Debug,"Retention iteration end...");
  58. Log(LogSeverityEnum.Info,$"Retention deleted {delDoneFiles+delDoneSize + delFailFiles + delFailSize} files -> Pool.Done: Ret.Files={delDoneFiles}, Ret.Size={delDoneSize}; Pool.Fail: Ret.Files={delFailFiles}, Ret.Size={delFailSize}");
  59. }
  60. #endregion
  61. #region *** Overrides ***
  62. #endregion
  63. #region *** Private Operations ***
  64. private int DeleteRetentionByCount(CfgCtx ctx,Pool.PoolTypeEnum repo, int maxFiles)
  65. {
  66. var total = 0;
  67. while (ctx.Pool.GetDocumentsCount(repo) > maxFiles)
  68. {
  69. var docIds = ctx.Pool.GetDocumentIdsFromOldest(repo).ToArray();
  70. if (docIds.Length > 0)
  71. total += ctx.Pool.DeleteDocumentFiles(repo, docIds.First()).Count();
  72. }
  73. return total;
  74. }
  75. private int DeleteRetentionBySize(CfgCtx ctx,Pool.PoolTypeEnum repo, long maxSize)
  76. {
  77. var total = 0;
  78. while (ctx.Pool.GetDocumentsSize(repo) > maxSize)
  79. {
  80. var docIds = ctx.Pool.GetDocumentIdsFromOldest(repo).ToArray();
  81. if (docIds.Length > 0)
  82. total += ctx.Pool.DeleteDocumentFiles(repo, docIds.First()).Count();
  83. }
  84. return total;
  85. }
  86. #endregion
  87. }
  88. }