| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using System.Linq;
- using System.Threading.Tasks;
- using BO.ProcessServer.Business.Workers.Contexts;
- using Quadarax.Foundation.Core.Logging;
- using Quadarax.Foundation.Core.Thread;
- namespace BO.ProcessServer.Business.Workers
- {
- public class RetentionWorker : LoopWorker
- {
- #region *** Constants ***
- private const string CS_WK_NAME = "Retention";
- #endregion
- #region *** Private fields ***
- #endregion
- #region *** Properties ***
- #endregion
- #region *** Constructors ***
- public RetentionWorker(IWorkerContext context) : base(CS_WK_NAME, context)
- {
- IterationsDelayBetween = ((CfgCtx)context).Configuration.System.Retention.Interval;
- }
- public RetentionWorker(string workerName, IWorkerContext context) : base(workerName, context)
- {
- }
- public RetentionWorker(string workerName) : base(workerName)
- {
- }
- public RetentionWorker(string workerName, IWorkerContext context, ILogger logger = null) : base(workerName, context, logger)
- {
- }
- #endregion
- #region *** Iteration ***
- protected override async Task DoIteration(IWorkerContext context)
- {
- var ctx = (CfgCtx)context;
- if (!ctx.Configuration.System.Retention.Enabled)
- return;
- Log(LogSeverityEnum.Debug,"Retention iteration started...");
- var maxFiles = ctx.Configuration.System.Retention.Items;
- var maxSize = ctx.Configuration.System.Retention.Size;
- var delDoneFiles = 0;
- var delFailFiles = 0;
- var delDoneSize = 0;
- var delFailSize = 0;
- if (maxFiles > 0)
- {
- // retention by files count
- delDoneFiles = DeleteRetentionByCount(ctx,Pool.PoolTypeEnum.Done, maxFiles);
- delFailFiles = DeleteRetentionByCount(ctx,Pool.PoolTypeEnum.Fail, maxFiles);
- }
- if (maxSize > 0)
- {
- // retention by size
- delDoneSize = DeleteRetentionBySize(ctx, Pool.PoolTypeEnum.Done, maxSize);
- delFailSize = DeleteRetentionBySize(ctx, Pool.PoolTypeEnum.Fail, maxSize);
- }
- Log(LogSeverityEnum.Debug,"Retention iteration end...");
- 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}");
- }
- #endregion
- #region *** Overrides ***
- #endregion
- #region *** Private Operations ***
- private int DeleteRetentionByCount(CfgCtx ctx,Pool.PoolTypeEnum repo, int maxFiles)
- {
- var total = 0;
- while (ctx.Pool.GetDocumentsCount(repo) > maxFiles)
- {
- var docIds = ctx.Pool.GetDocumentIdsFromOldest(repo).ToArray();
- if (docIds.Length > 0)
- total += ctx.Pool.DeleteDocumentFiles(repo, docIds.First()).Count();
- }
- return total;
- }
- private int DeleteRetentionBySize(CfgCtx ctx,Pool.PoolTypeEnum repo, long maxSize)
- {
- var total = 0;
- while (ctx.Pool.GetDocumentsSize(repo) > maxSize)
- {
- var docIds = ctx.Pool.GetDocumentIdsFromOldest(repo).ToArray();
- if (docIds.Length > 0)
- total += ctx.Pool.DeleteDocumentFiles(repo, docIds.First()).Count();
- }
- return total;
- }
-
- #endregion
-
- }
- }
|