Procházet zdrojové kódy

finished ITaskItem, add Dependencies

Dalibor Votruba před 2 roky
rodič
revize
326ee943c2

+ 23 - 0
Workbench/Processor/Dependencies/Exceptions/ApiException.cs

@@ -0,0 +1,23 @@
+namespace Quadarax.Foundation.Core.Exceptions
+{
+    public partial class ApiException : System.Exception
+    {
+        public int StatusCode { 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)
+            : base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
+        {
+            StatusCode = statusCode;
+            Response = response; 
+            Headers = headers;
+        }
+
+        public override string ToString()
+        {
+            return $"HTTP Response: \n\n{Response}\n\n{base.ToString()}";
+        }
+    }}

+ 18 - 0
Workbench/Processor/Dependencies/Exceptions/CodeException.cs

@@ -0,0 +1,18 @@
+using System;
+
+namespace Quadarax.Foundation.Core.Exceptions
+{
+    [Serializable]
+    public class CodeException : Exception
+    {
+        public CodeException(int code, string message, Exception innerException) : base(message, innerException)
+        {
+            this.SetCode(code);
+        }
+
+        public CodeException(int code, string message) : base(message)
+        {
+            this.SetCode(code);
+        }
+    }
+}

+ 20 - 0
Workbench/Processor/Dependencies/Exceptions/ExceptionExc.cs

@@ -0,0 +1,20 @@
+using System;
+
+namespace Quadarax.Foundation.Core.Exceptions
+{
+    public static class ExceptionExc
+    {
+        public static int GetCode(this Exception e)
+        {
+            if (!e.Data.Contains("Code"))
+                return -1;
+
+            return int.Parse(e.Data["Code"]?.ToString() ?? "-1");
+        }
+
+        public static void SetCode(this Exception e, int code)
+        {
+            e.Data.Add("Code", code);
+        }
+    }
+}

+ 42 - 0
Workbench/Processor/Dependencies/Exceptions/TryCatchExt.cs

@@ -0,0 +1,42 @@
+using System;
+
+namespace Quadarax.Foundation.Core.Exceptions
+{
+    public static class TryCatchExt
+    {
+        public static Exception TryCatch<TResult>(this object owner, Func<TResult> fnc)
+        {
+            if (owner == null)
+                throw new ArgumentNullException(nameof(owner));
+            if (fnc == null)
+                return null;
+            try
+            {
+                fnc();
+            }
+            catch (Exception e)
+            {
+                return e;
+            }
+
+            return null;
+        }
+        public static Exception TryCatch(this object owner, Action fnc)
+        {
+            if (owner == null)
+                throw new ArgumentNullException(nameof(owner));
+            if (fnc == null)
+                return null;
+            try
+            {
+                fnc();
+            }
+            catch (Exception e)
+            {
+                return e;
+            }
+
+            return null;
+        }
+    }
+}

+ 39 - 0
Workbench/Processor/Exceptons/InvalidTaskStateException.cs

@@ -0,0 +1,39 @@
+using Quadarax.Foundation.Core.Business.Processor.Enums;
+using Quadarax.Foundation.Core.Business.Processor.Task;
+// ReSharper disable InconsistentNaming
+
+
+namespace Quadarax.Foundation.Core.Business.Processor.Exceptons
+{
+    public sealed class InvalidTaskStateException : Exception
+    {
+        #region *** Constants ***
+        private const string CS_EXCC_ENT_TYPE = "EntityType";
+        private const string CS_EXCC_ENT_ID = "EntityIdentifier";
+        private const string CS_EXCC_ENT_CURRENT_STA = "CurrentStatus";
+        private const string CS_EXCC_ENT_NEW_STA = "NewStatus";
+        private const string CS_EXCC_ENT_EXCEPTED_STA = "ExpectedStatus";
+        #endregion
+
+        #region *** Constructors ***
+        public InvalidTaskStateException(ITaskItem task, ProcessStatusEnum newStatus, params ProcessStatusEnum[] expectedStatuses) : base(GetMessage(task, newStatus, expectedStatuses))
+        {
+            Data.Add(CS_EXCC_ENT_TYPE, task.GetType());
+            Data.Add(CS_EXCC_ENT_ID, task.Id);
+            Data.Add(CS_EXCC_ENT_CURRENT_STA, task.Status);
+            Data.Add(CS_EXCC_ENT_NEW_STA, newStatus);
+            Data.Add(CS_EXCC_ENT_EXCEPTED_STA, expectedStatuses);
+            
+        }
+
+        #endregion
+
+        #region *** Private Operations ***
+        private static string GetMessage(ITaskItem task, ProcessStatusEnum newStatus,
+            params ProcessStatusEnum[] expectedStatuses)
+        {
+            return $"Invalid task state exception. Task: {task.GetType()}[{task.Id}] is in state {task.Status} and cannot be changed to {newStatus}. Expected states: {string.Join(", ", expectedStatuses)}";
+        }
+        #endregion
+    }
+}

+ 24 - 3
Workbench/Processor/Task/ITaskItem.cs

@@ -1,4 +1,5 @@
 using Quadarax.Foundation.Core.Business.Processor.Enums;
+using Quadarax.Foundation.Core.Business.Processor.Exceptons;
 
 namespace Quadarax.Foundation.Core.Business.Processor.Task;
 
@@ -53,16 +54,36 @@ public interface ITaskItem : ITaskIdentifier, ITimeTracked
 
 	#region *** Operations ***
 	/// <summary>
-	/// Schedules task to future and sets <see cref="Status"/> to <see cref="ProcessStatusEnum.Pending"/> (affects <see cref="ProcessingValidFrom"/> and <see cref="ProcessingValidTo"/>).
+	/// Re-Schedules inactive task to future and sets <see cref="Status"/> to <see cref="ProcessStatusEnum.Pending"/> (affects <see cref="ProcessingValidFrom"/> and <see cref="ProcessingValidTo"/>).
 	/// <list type="bullet">
 	///	<item><description>Allowed state: <see cref="ProcessStatusEnum.Pending"/>, <see cref="ProcessStatusEnum.Paused"/></description></item>
     ///	<item><description>Set state to: <see cref="ProcessStatusEnum.Pending"/></description></item>
     /// </list>
+    /// <exception cref="InvalidTaskStateException">When task state is invalid for operation.</exception>
 	/// </summary>
 	/// <param name="postponeInterval">Interval to shift to future.</param>
 	/// <param name="reason">Reason message to postpone.</param>
     void Postpone(TimeSpan postponeInterval, string reason);
-	void Stop(string reason);
-	void Reset(string reason);
+	/// <summary>
+	/// Stops running task and sets <see cref="Status"/> to <see cref="ProcessStatusEnum.Paused"/> or
+	/// <see cref="ProcessStatusEnum.ProcessedSucc"/> or <see cref="ProcessStatusEnum.ProcessedFail"/> (target status is depends on input arguments)..
+    /// <list type="bullet">
+    ///	<item><description>Allowed state: <see cref="ProcessStatusEnum.Started"/></description></item>
+    ///	<item><description>Set state to: <see cref="ProcessStatusEnum.Paused"/> or <see cref="ProcessStatusEnum.ProcessedSucc"/> or <see cref="ProcessStatusEnum.ProcessedFail"/></description></item>
+    /// </list>
+    /// <exception cref="InvalidTaskStateException">When task state is invalid for operation.</exception>
+    /// </summary>
+	/// <param name="reason">Reason message to stop.</param>
+	/// <param name="willPause">If true, then <see cref="Status"/> will be set to <see cref="ProcessStatusEnum.Paused"/> otherwise to <see cref="ProcessStatusEnum.ProcessedSucc"/> or <see cref="ProcessStatusEnum.ProcessedFail"/>.</param>
+	/// <param name="willFail">If true, then <see cref="Status"/> will be set to <see cref="ProcessStatusEnum.ProcessedFail"/> otherwise to <see cref="ProcessStatusEnum.ProcessedSucc"/>.</param>
+    void Stop(string reason, bool willPause = true, bool willFail = false);
+
+    /// <summary>
+    /// Resets task to initial state and sets <see cref="Status"/> to <see cref="ProcessStatusEnum.Pending"/>.
+    /// Uses current validity to re-calculate <see cref="ProcessingValidFrom"/> and <see cref="ProcessingValidTo"/> to now + <see cref="postponeValid"/>.
+    /// </summary>
+    /// <param name="reason">Reason to reset task.</param>
+    /// <param name="postponeValid">Postpone current validity from now.</param>
+    void Reset(string reason, TimeSpan postponeValid);
 	#endregion
 }