comparison 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
comparison
equal deleted inserted replaced
202:2651cb9a4250 203:4d9830a9bbb8
69 69
70 readonly StateMachine m_stateMachine; 70 readonly StateMachine m_stateMachine;
71 71
72 protected RunnableComponent(bool initialized) { 72 protected RunnableComponent(bool initialized) {
73 m_stateMachine = new StateMachine(initialized ? ExecutionState.Ready : ExecutionState.Created); 73 m_stateMachine = new StateMachine(initialized ? ExecutionState.Ready : ExecutionState.Created);
74 } 74 DisposeTimeout = 10000;
75 75 }
76 protected virtual int DisposeTimeout { 76
77 get { 77 /// <summary>
78 return 10000; 78 /// 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.
79 } 79 /// </summary>
80 protected int DisposeTimeout {
81 get;
82 set;
80 } 83 }
81 84
82 void ThrowInvalidCommand(Commands cmd) { 85 void ThrowInvalidCommand(Commands cmd) {
83 if (m_stateMachine.State == ExecutionState.Disposed) 86 if (m_stateMachine.State == ExecutionState.Disposed)
84 throw new ObjectDisposedException(ToString()); 87 throw new ObjectDisposedException(ToString());
87 } 90 }
88 91
89 void Move(Commands cmd) { 92 void Move(Commands cmd) {
90 if (!m_stateMachine.Move(cmd)) 93 if (!m_stateMachine.Move(cmd))
91 ThrowInvalidCommand(cmd); 94 ThrowInvalidCommand(cmd);
95 }
96
97 /// <summary>
98 /// Moves the component from running to failed state.
99 /// </summary>
100 /// <param name="error">The exception which is describing the error.</param>
101 /// <returns>Returns true if the component is set to the failed state, false - otherwise.
102 /// This method works only for the running state, in any other state it will return false.</returns>
103 protected bool Fail(Exception error) {
104 lock (m_stateMachine) {
105 if(m_stateMachine.State == ExecutionState.Running) {
106 m_stateMachine.Move(Commands.Fail);
107 m_lastError = error;
108 return true;
109 }
110 }
111 return false;
92 } 112 }
93 113
94 void Invoke(Commands cmd, Action action) { 114 void Invoke(Commands cmd, Action action) {
95 lock (m_stateMachine) 115 lock (m_stateMachine)
96 Move(cmd); 116 Move(cmd);
208 } 228 }
209 } 229 }
210 230
211 public ExecutionState State { 231 public ExecutionState State {
212 get { 232 get {
213 lock (m_stateMachine) 233 return m_stateMachine.State;
214 return m_stateMachine.State;
215 } 234 }
216 } 235 }
217 236
218 public Exception LastError { 237 public Exception LastError {
219 get { 238 get {
223 242
224 #endregion 243 #endregion
225 244
226 #region IDisposable implementation 245 #region IDisposable implementation
227 246
247 /// <summary>
248 /// Releases all resource used by the <see cref="Implab.Components.RunnableComponent"/> object.
249 /// </summary>
250 /// <remarks>
251 /// <para>Will not try to stop the component, it will just release all resources.
252 /// To cleanup the component gracefully use <see cref="Stop()"/> method.</para>
253 /// <para>
254 /// In normal cases the <see cref="Dispose()"/> method shouldn't be called, the call to the <see cref="Stop()"/>
255 /// method is sufficient to cleanup the component. Call <see cref="Dispose()"/> only to cleanup after errors,
256 /// especially if <see cref="Stop"/> method is failed. Using this method insted of <see cref="Stop()"/> may
257 /// lead to the data loss by the component.
258 /// </para></remarks>
228 public void Dispose() { 259 public void Dispose() {
229 IPromise pending; 260 IPromise pending;
230 lock (m_stateMachine) { 261 lock (m_stateMachine) {
231 if (m_stateMachine.State == ExecutionState.Disposed) 262 if (m_stateMachine.State == ExecutionState.Disposed)
232 return; 263 return;