Explorar el Código

Defines IProcessorStorageProvider, add ProcessQueueConfiguration (not completed)

Dalibor Votruba hace 2 años
padre
commit
5bd86b3d2e

+ 15 - 0
Workbench/Processor/Dependencies/Value/Extensions/EnumExt.cs

@@ -0,0 +1,15 @@
+namespace Quadarax.Foundation.Core.Business.Processor.Dependencies.Value.Extensions
+{
+    public static class EnumExt
+    {
+        public static IEnumerable<TEnum> All<TEnum>(this TEnum owner) where TEnum : Enum
+        {
+            return Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
+        }
+
+        public static bool In<TEnum>(this TEnum owner, params TEnum[] values) where TEnum : Enum
+        {
+            return values.Contains(owner);
+        }
+    }
+}

+ 53 - 0
Workbench/Processor/Providers/IProcessorStorageProvider.cs

@@ -0,0 +1,53 @@
+using Quadarax.Foundation.Core.Business.Processor.Enums;
+using Quadarax.Foundation.Core.Business.Processor.Task;
+
+namespace Quadarax.Foundation.Core.Business.Processor.Providers
+{
+    public interface IProcessorStorageProvider : IDisposable
+    {
+        /// <summary>
+        /// Open storage connection if needed
+        /// </summary>
+        public void Open();
+        /// <summary>
+        /// Close storage connection if needed
+        /// </summary>
+        public void Close();
+        /// <summary>
+        /// Ensure storage is ready to use. If not exists, create it.
+        /// <description>This operation doesn't use Open or Close operation to access to storage and doesn't affects Open/Close state..</description>
+        /// </summary>
+        /// <returns>Returns true, if storage already exists. Otherwise returns false that storage was created.</returns>
+        public bool EnsureStorage();
+
+        /// <summary>
+        /// Create new task item in storage and save it.
+        /// </summary>
+        /// <param name="item">Item to create.</param>
+        /// <returns>Identifier of new created item.</returns>
+        public ITaskIdentifier Create(ITaskItem item);
+        /// <summary>
+        /// Get task item from storage by identifier.
+        /// </summary>
+        /// <param name="ids">Storage identifiers of Task item</param>
+        /// <returns>Array of Task items</returns>
+        public ITaskItem[] Get(params ITaskIdentifier[] ids);
+        /// <summary>
+        /// Saves task items in storage.
+        /// </summary>
+        /// <param name="items">Task items to save.</param>
+        public void Set(params ITaskItem[] items);
+        /// <summary>
+        /// Deletes task items in storage (or mark it for delete).
+        /// </summary>
+        /// <param name="force">Flag is set, performs physical delete of item. Otherwise mark item to delete.</param>
+        /// <param name="ids">Storage identifiers of Task item</param>
+        public void Delete(bool force, params ITaskIdentifier[] ids);
+        /// <summary>
+        /// Query task items from storage by status.
+        /// </summary>
+        /// <param name="statuses">Allowed statuses</param>
+        /// <returns>Queryable collection of selected Task items.</returns>
+        public IQueryable<ITaskItem> Query(params ProcessStatusEnum[] statuses);
+    }
+}

+ 30 - 6
Workbench/Processor/Queue/ProcessQueue.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Quadarax.Foundation.Core.Business.Processor.Enums;
+using Quadarax.Foundation.Core.Business.Processor.Providers;
 using Quadarax.Foundation.Core.Business.Processor.Task;
 using Quadarax.Foundation.Core.Logging;
 using Quadarax.Foundation.Core.Object;
@@ -12,6 +14,30 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
     public class ProcessQueue : DisposableObject
     {
 
+        #region *** Properties ***
+        public bool IsRunning { get; private set; }
+        public string QueueIdentifier { get; private set; }
+        #endregion
+
+        #region *** Constructors ***
+        public ProcessQueue(string processQueueIdentifierToken, IProcessorStorageProvider storage, ProcessQueueConfiguration configuration, CallbackDelegates delegates = null)
+        {
+            if (string.IsNullOrEmpty(processQueueIdentifierToken)) throw new ArgumentNullException(nameof(processQueueIdentifierToken));
+            IdentifierToken = processQueueIdentifierToken;
+            _delegates = delegates ?? new CallbackDelegates();
+
+            _log = new QLogger().GetLogger(GetType());
+            _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
+            _storageProvider = storage ?? throw new ArgumentNullException(nameof(storage));
+
+            _configuration.Validate();		
+            _processingItems = new ProcessQueueItem[_configuration.WorkersCount];
+        }
+
+
+        #endregion
+
+        #region *** Public Operations ***
         public void Start()
         {
             throw new NotImplementedException();
@@ -35,20 +61,18 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
 
         }
 
-        public bool ContainsPendingTasks()
+        public ITaskIdentifier[] QueryTasks(ProcessStatusEnum[] states, bool onlyValid = true, bool includeDeleted = false)
         {
 
         }
-        public bool ContainsProcessingTasks()
-        {
 
-        }
-        
+        #endregion
 
+        #region *** Private Operations ***
         protected override void OnDisposing()
         {
             throw new NotImplementedException();
         }
-
+        #endregion
     }
 }

+ 38 - 0
Workbench/Processor/Queue/ProcessQueueConfiguration.cs

@@ -0,0 +1,38 @@
+using Quadarax.Foundation.Core.Logging;
+
+namespace Quadarax.Foundation.Core.Business.Processor.Queue;
+
+public class ProcessQueueConfiguration
+{
+
+	#region *** Properties ***
+    public int WorkersCount {get; private set;} //number of max processing workers
+    public TimeSpan WatchdogInterval {get; private set;} //Interval to watchdog monitor and emits new worker
+    public TimeSpan RetentionInterval {get; private set;} //retention - Interval to run retention mechanism. If set to TimeSpan.Zero, retention disabled.
+    public TimeSpan RetentionClearOlderThan {get; private set;} //retention - clear all status Deleted older than value in all queue types
+    public TimeSpan RetentionDeletedOlderThan {get; private set;} //retention - mark all items to status Deleted older than value in Succ, Fail queue
+    #endregion
+
+    #region *** Private Fields ***
+    private ILog _log;
+    #endregion
+
+	public ProcessQueueConfiguration(int workersCount, TimeSpan watchdogInterval)
+	{
+		WorkersCount = workersCount;
+		WatchdogInterval = watchdogInterval;
+		_log = new ConsoleLogger().GetLogger(GetType());
+	}
+		
+
+	public void Validate()
+	{
+		var errors = new List<Exception>();
+		if (WorkersCount==0) errors.Add(new ArgumentOutOfRangeException(nameof(WorkersCount), "WorkersCount must be bigger than zero."));
+		if (WatchdogInterval == TimeSpan.Zero) errors.Add(new ArgumentOutOfRangeException(nameof(WatchdogInterval), "WatchdogInterval must be bigger than TimeSpan.Zero."));
+		
+		if (errors.Count>0) throw new AggregateException("ProcessQueue configuration has some invalid values.", errors);
+
+		_log.Log(LogSeverityEnum.Debug, "Configuration validated.");
+	}
+}