Sfoglia il codice sorgente

remove some warnings

Dalibor Votruba 2 anni fa
parent
commit
840c006582

+ 2 - 2
Workbench/Processor/Dependencies/Data/TypedArgument.cs

@@ -5,12 +5,12 @@ namespace Quadarax.Foundation.Core.Object;
 public class TypedArgument
 {
     public string Name {get; private set;}
-    public string Value {get; private set;}  //TODO: must be nullable
+    public string? Value {get; private set;}  //TODO: must be nullable
     public Type ValueType {get; private set;}
        
     public bool IsOutput {get; private set;}
        
-    public TypedArgument(string name, string valueAsString, Type valueType, bool isOutput = false)
+    public TypedArgument(string name, string? valueAsString, Type? valueType, bool isOutput = false)
     {
         Name = name ?? throw new ArgumentNullException(nameof(name));
         ValueType = valueType ?? throw new ArgumentNullException(nameof(valueType));

+ 2 - 2
Workbench/Processor/Dependencies/Exceptions/ApiException.cs

@@ -4,11 +4,11 @@
     {
         public int StatusCode { get; }
 
-        public string Response { get; }
+        public string? Response { get; }
 
         public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }
 
-        public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
+        public ApiException(string message, int statusCode, string? response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
             : base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
         {
             StatusCode = statusCode;

+ 2 - 2
Workbench/Processor/Dependencies/Exceptions/TryCatchExt.cs

@@ -4,7 +4,7 @@ namespace Quadarax.Foundation.Core.Exceptions
 {
     public static class TryCatchExt
     {
-        public static Exception TryCatch<TResult>(this object owner, Func<TResult> fnc)
+        public static Exception? TryCatch<TResult>(this object owner, Func<TResult>? fnc)
         {
             if (owner == null)
                 throw new ArgumentNullException(nameof(owner));
@@ -21,7 +21,7 @@ namespace Quadarax.Foundation.Core.Exceptions
 
             return null;
         }
-        public static Exception TryCatch(this object owner, Action fnc)
+        public static Exception? TryCatch(this object owner, Action? fnc)
         {
             if (owner == null)
                 throw new ArgumentNullException(nameof(owner));

+ 2 - 2
Workbench/Processor/Dependencies/Logging/ConsoleLog.cs

@@ -28,12 +28,12 @@
             Console.WriteLine(final);
         }
 
-        public void Log(LogSeverityEnum severity, string message, Exception exception)
+        public void Log(LogSeverityEnum severity, string message, Exception? exception)
         {
             Log(severity, $"{message}\n{exception}");
         }
 
-        public void Log(LogSeverityEnum severity, int code, string message, Exception exception)
+        public void Log(LogSeverityEnum severity, int code, string message, Exception? exception)
         {
             Log(severity, string.Format("{2} - {0}\n{1}", message,exception, code));
         }

+ 1 - 2
Workbench/Processor/Dependencies/Logging/ConsoleLogger.cs

@@ -1,5 +1,4 @@
-using ProcessingQueue.Externals;
-
+
 namespace Quadarax.Foundation.Core.Logging
 {
     public class ConsoleLogger : ILogger

+ 2 - 2
Workbench/Processor/Dependencies/Logging/ILog.cs

@@ -6,7 +6,7 @@ namespace Quadarax.Foundation.Core.Logging
     {
         void Log(LogSeverityEnum severity, int code, string message);
         void Log(LogSeverityEnum severity, string message);
-        void Log(LogSeverityEnum severity, string message, Exception exception);
-        void Log(LogSeverityEnum severity, int code, string message, Exception exception);
+        void Log(LogSeverityEnum severity, string message, Exception? exception);
+        void Log(LogSeverityEnum severity, int code, string message, Exception? exception);
     }
 }

+ 1 - 1
Workbench/Processor/Dependencies/Logging/ILogHandler.cs

@@ -4,6 +4,6 @@ namespace Quadarax.Foundation.Core.Logging
 {
     public interface ILogHandler
     {
-        void Log(LogSeverityEnum type, string message, Exception e = null);
+        void Log(LogSeverityEnum type, string message, Exception? e = null);
     }
 }

+ 1 - 1
Workbench/Processor/Dependencies/Object/Extensions/ObjectExt.cs

@@ -8,7 +8,7 @@ namespace Quadarax.Foundation.Core.Object.Extensions
 {
     public static class ObjectExt
     {
-        public static IDictionary<string, object> GetAllPropertyValues(this object obj,string indentPrefix = null)
+        public static IDictionary<string, object> GetAllPropertyValues(this object? obj,string? indentPrefix = null)
         {
             var props = new Dictionary<string, object>();
             if (obj == null)

+ 2 - 2
Workbench/Processor/Dependencies/Object/Singleton.cs

@@ -11,7 +11,7 @@
         /// <typeparam name="TInstance"></typeparam>
         public class Singleton<TInstance> where TInstance : new()
         {
-            private static TInstance _instance;
+            private static TInstance? _instance;
             private static readonly object _syncRoot = new object();
 
             /// <summary>
@@ -48,7 +48,7 @@
             where TInstance : TInterface, new()
             where TInterface : class
         {
-            private static volatile TInterface _instance;
+            private static volatile TInterface? _instance;
             private static readonly object _syncRoot = new object();
 
             public static TInterface Instance

+ 1 - 1
Workbench/Processor/Dependencies/Object/WeakReference.cs

@@ -22,7 +22,7 @@ namespace Quadarax.Foundation.Core.Object
         /// <summary>
         /// Strongly typed version of <see cref="WeakReference.Target"/>.
         /// </summary>
-        public new TReference Target
+        public new TReference? Target
         {
             get { return base.Target as TReference; }
             set { base.Target = value; }

+ 3 - 3
Workbench/Processor/Dependencies/Reflection/QualifiedName.cs

@@ -1,6 +1,4 @@
-using System;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using Quadarax.Foundation.Core.Reflection.Extensions;
 
 namespace Quadarax.Foundation.Core.Reflection
@@ -39,6 +37,8 @@ namespace Quadarax.Foundation.Core.Reflection
                 try
                 {
                     var oType = Type.GetType(aParts[0].Trim(), true);
+                    if (oType == null)
+                        throw new Exception("Type is null");
                     SetupProperties(oType.Assembly.GetName());
                     bCodebase = true;
                 }

+ 9 - 6
Workbench/Processor/Dependencies/Reflection/TypeUtils.cs

@@ -22,16 +22,19 @@ namespace Quadarax.Foundation.Core.Reflection
         }
         #endregion
         #region *** Public operations & overrides ***
-        public static Type GetRemoteType(string sReflectionQualifiedName)
+        public static Type? GetRemoteType(string sReflectionQualifiedName)
         {
-            if (m_oRemTypeCache.ContainsKey(sReflectionQualifiedName))
-                return m_oRemTypeCache[sReflectionQualifiedName];
+            if (m_oRemTypeCache.TryGetValue(sReflectionQualifiedName, out var type))
+                lock (@lock)
+                {
+                    return type;
+                }
 
             var oName = new QualifiedName(sReflectionQualifiedName);
-            Type oType;
-            if (oName.Assembly == null)
+            Type? oType;
+            if (oName?.Assembly == null)
             {
-                oType = Type.GetType(oName.Name, true);
+                oType = Type.GetType(oName?.Name, true);
             }
             else
             {

+ 1 - 7
Workbench/Processor/Dependencies/Value/Extensions/TimeSpanExt.cs

@@ -1,7 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Quadarax.Foundation.Core.Value.Formatters;
+using Quadarax.Foundation.Core.Value.Formatters;
 
 namespace Quadarax.Foundation.Core.Value.Extensions
 {
@@ -9,9 +6,6 @@ namespace Quadarax.Foundation.Core.Value.Extensions
     {
         public static string ToReadableString(this TimeSpan owner)
         {
-            if (owner == null)
-                throw new ArgumentNullException(nameof(owner));
-
             // formats and its cutoffs based on TotalSeconds
             var cutoff = new SortedList<long, string> { 
                 {59, "{3:S}" }, 

+ 26 - 0
Workbench/Processor/Dependencies/Value/Formatters/HmsFormatter.cs

@@ -0,0 +1,26 @@
+namespace Quadarax.Foundation.Core.Value.Formatters
+{
+    /// <summary>
+    /// Formatter for forms of seconds/hours/day
+    /// </summary>
+    public class HmsFormatter : ICustomFormatter, IFormatProvider 
+    {
+        static Dictionary<string, string> _timeformats = new()
+        {
+            {"S", "{0:P:Seconds:Second}"},
+            {"M", "{0:P:Minutes:Minute}"},
+            {"H","{0:P:Hours:Hour}"},
+            {"D", "{0:P:Days:Day}"}
+        };
+
+        public string Format(string? format, object? arg, IFormatProvider? formatProvider)
+        {
+            return string.Format(new PluralFormatter(),format == null ? string.Empty : _timeformats[format], arg);
+        }
+
+        public object? GetFormat(Type? formatType)
+        {
+            return formatType == typeof(ICustomFormatter)?this:null;
+        }
+    }
+}

+ 34 - 0
Workbench/Processor/Dependencies/Value/Formatters/PluralFormatter.cs

@@ -0,0 +1,34 @@
+using System;
+using static System.String;
+
+namespace Quadarax.Foundation.Core.Value.Formatters
+{
+    /// <summary>
+    /// Formats a numeric value based on a format P:Plural:Singular
+    /// </summary>
+    public class PluralFormatter : ICustomFormatter, IFormatProvider
+    {
+
+        public string Format(string? format, object? arg, IFormatProvider? formatProvider)
+        {
+            if (arg !=null)
+            {
+                var parts = format == null ? Array.Empty<string>() : format.Split(':'); // ["P", "Plural", "Singular"]
+
+                if (parts[0] == "P") // correct format?
+                {
+                    // which index position to use
+                    int partIndex = (arg.ToString() == "1")?2:1;
+                    // pick string (safe guard for array bounds) and format
+                    return $"{arg} {(parts.Length > partIndex ? parts[partIndex] : "")}";               
+                }
+            }
+            return String.Format(format ?? string.Empty, arg);
+        }
+
+        public object? GetFormat(Type? formatType)
+        {
+            return formatType == typeof(ICustomFormatter)?this:null;
+        }   
+    }
+}

+ 1 - 1
Workbench/Processor/Providers/ProcessorStorageProvider.cs

@@ -114,7 +114,7 @@ public abstract class ProcessorStorageProvider : DisposableObject, IProcessorSto
     public abstract IQueryable<ITaskItemData> Query(params ProcessStatusEnum[] statuses);
 
     #region *** Private Operations & Virtuals ***
-    protected virtual void Log(LogSeverityEnum type, string message, Exception e = null)
+    protected virtual void Log(LogSeverityEnum type, string message, Exception? e = null)
     {
         _log.Log(type, message, e);
     }

+ 18 - 6
Workbench/Processor/Queue/CallbackDelegates.cs

@@ -7,17 +7,17 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
     public delegate void ItemAddedCallback(ITaskItem item);
     public delegate void ProcessorStartedCallback(ITaskItem item);
     public delegate void ProcessorStoppedCallback(ITaskItem item);
-    public delegate void ProcessorErrorCallback(string message, Exception e);
+    public delegate void ProcessorErrorCallback(string message, Exception? e);
     #endregion
 
     public class CallbackDelegates
     {
         #region *** Public properties ***
-        public ItemProcessedCallback OnItemProcessed { get; set; }
-        public ItemAddedCallback OnItemAdded { get; set; }
-        public ProcessorStartedCallback OnProcessorStarted { get; set; }
-        public ProcessorStoppedCallback OnProcessorStopped { get; set; }
-        public ProcessorErrorCallback OnProcessorError { get; set; }
+        public ItemProcessedCallback? OnItemProcessed { get; set; }
+        public ItemAddedCallback? OnItemAdded { get; set; }
+        public ProcessorStartedCallback? OnProcessorStarted { get; set; }
+        public ProcessorStoppedCallback? OnProcessorStopped { get; set; }
+        public ProcessorErrorCallback? OnProcessorError { get; set; }
         #endregion
 
         #region *** Constructors ***
@@ -25,5 +25,17 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
         {
         }
         #endregion
+
+        #region *** Public operations ***
+
+        public void Reset()
+        {
+            OnItemProcessed = null;
+            OnItemAdded = null;
+            OnProcessorStarted = null;
+            OnProcessorStopped = null;
+            OnProcessorError = null;
+        }
+        #endregion
     }
 }

+ 23 - 3
Workbench/Processor/Queue/ProcessQueue.cs

@@ -117,16 +117,33 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
             return (ITaskItem[])result;
         }
 
-        public ITaskIdentifier[] QueryTasks(ProcessStatusEnum[] states, bool onlyValid = true, bool includeDeleted = false)
+        public ITaskIdentifier[] QueryTasks(ProcessStatusEnum[] states,int maxItems, bool onlyTimeValid = true, bool includeDeleted = false)
         {
+            if (states == null || !states.Any()) throw new ArgumentNullException(nameof(states));
 
+            var now = DateTime.Now;
+            if (!states.Contains(ProcessStatusEnum.Deleted) && includeDeleted)
+                states = states.Concat(new[] { ProcessStatusEnum.Deleted }).ToArray();
+
+            var items = _storageProvider.Query(states);
+            if (onlyTimeValid)
+                items = items.Where(x=> x.ProcessingValidFrom <= now && x.ProcessingValidTo.GetValueOrDefault(DateTime.MaxValue) >= now);
+            var result = items.OrderBy(x => x.ProcessingValidFrom)
+                .ThenBy(x => x.Priority)
+                .Take(maxItems)
+                .Select(x=>(ITaskIdentifier)x)
+                .ToArray();
+
+            Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] Query tasks {result.Length} of {maxItems}@{DateTime.Now.Subtract(now).ToReadableString()}");
+
+            return result;
         }
 
         #endregion
 
         #region *** Private Operations ***
 
-        protected void Log(LogSeverityEnum type, string message, Exception e = null)
+        protected void Log(LogSeverityEnum type, string message, Exception? e = null)
         {
             if (type == LogSeverityEnum.Error || type == LogSeverityEnum.Fatal)
             {
@@ -137,7 +154,10 @@ namespace Quadarax.Foundation.Core.Business.Processor.Queue
 
         protected override void OnDisposing()
         {
-            throw new NotImplementedException();
+            if (IsRunning)
+                Stop();
+            _delegates?.Reset();
+            Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] disposed.");
         }
         #endregion
     }

+ 2 - 2
Workbench/Processor/Task/AffinityToken.cs

@@ -45,7 +45,7 @@ public class AffinityToken : IAffinityToken
 		WorkerOrdinal = int.Parse(parts[1]);
 		
 	}
-	public void Set(string queueIdentifier, int workerOrdinal)
+	public void Set(string? queueIdentifier, int workerOrdinal)
 	{
 		if (string.IsNullOrEmpty(queueIdentifier)) throw new ArgumentNullException(nameof(queueIdentifier));
 		QueueIdentifier = queueIdentifier;
@@ -62,7 +62,7 @@ public class AffinityToken : IAffinityToken
 		return sb.ToString();
 	}
 	
-	public bool Equals (AffinityToken other)
+	public bool Equals (AffinityToken? other)
 	{
 		if (other == null) return false;
 		return string.Equals(ToString(), other.ToString());