diff Implab/Components/RunnableComponent.cs @ 203:4d9830a9bbb8 v2

Added 'Fail' method to RunnableComponent which allows component to move from Running to Failed state. Added PollingComponent a timer based runnable component More tests Added FailPromise a thin class to wrap exceptions Fixed error handling in SuccessPromise classes.
author cin
date Tue, 18 Oct 2016 17:49:54 +0300
parents 2651cb9a4250
children 8200ab154c8a
line wrap: on
line diff
--- a/Implab/Components/RunnableComponent.cs	Tue Oct 18 01:03:49 2016 +0300
+++ b/Implab/Components/RunnableComponent.cs	Tue Oct 18 17:49:54 2016 +0300
@@ -71,12 +71,15 @@
 
         protected RunnableComponent(bool initialized) {
             m_stateMachine = new StateMachine(initialized ? ExecutionState.Ready : ExecutionState.Created);
+            DisposeTimeout = 10000;
         }
 
-        protected virtual int DisposeTimeout {
-            get {
-                return 10000;
-            }
+        /// <summary>
+        /// Gets or sets the timeout to wait for the pending operation to complete. If the pending operation doesn't finish than the component will be disposed anyway.
+        /// </summary>
+        protected int DisposeTimeout {
+            get;
+            set;
         }
 
         void ThrowInvalidCommand(Commands cmd) {
@@ -91,6 +94,23 @@
                 ThrowInvalidCommand(cmd);
         }
 
+        /// <summary>
+        /// Moves the component from running to failed state.
+        /// </summary>
+        /// <param name="error">The exception which is describing the error.</param>
+        /// <returns>Returns true if the component is set to the failed state, false - otherwise.
+        /// This method works only for the running state, in any other state it will return false.</returns>
+        protected bool Fail(Exception error) {
+            lock (m_stateMachine) {
+                if(m_stateMachine.State == ExecutionState.Running) {
+                    m_stateMachine.Move(Commands.Fail);
+                    m_lastError = error;
+                    return true;
+                }
+            }
+            return false;
+        }
+
         void Invoke(Commands cmd, Action action) {
             lock (m_stateMachine) 
                 Move(cmd);
@@ -210,8 +230,7 @@
 
         public ExecutionState State {
             get {
-                lock (m_stateMachine)
-                    return m_stateMachine.State;
+                return m_stateMachine.State;
             }
         }
 
@@ -225,6 +244,18 @@
 
         #region IDisposable implementation
 
+        /// <summary>
+        /// Releases all resource used by the <see cref="Implab.Components.RunnableComponent"/> object.
+        /// </summary>
+        /// <remarks>
+        /// <para>Will not try to stop the component, it will just release all resources.
+        /// To cleanup the component gracefully use <see cref="Stop()"/> method.</para>
+        /// <para>
+        /// In normal cases the <see cref="Dispose()"/> method shouldn't be called, the call to the <see cref="Stop()"/>
+        /// method is sufficient to cleanup the component. Call <see cref="Dispose()"/> only to cleanup after errors,
+        /// especially if <see cref="Stop"/> method is failed. Using this method insted of <see cref="Stop()"/> may
+        /// lead to the data loss by the component.
+        /// </para></remarks>
         public void Dispose() {
             IPromise pending;
             lock (m_stateMachine) {