public override void RequestStop() { TaskEventArg request = new TaskEventArg(); request.RequestStop = true; this.HubEventEnqueue(request); }
// // // **** TryGetNextUnsuccessfulChild() **** /// <summary> /// This returns the latest child in the list that is NOT a success, or /// it returns the last child regardless. /// </summary> /// <param name="currentChild"></param> /// <returns>True, if currentChild is not null</returns> public bool TryGetNextUnsuccessfulChild(out TaskEventArg currentChild) { currentChild = null; if (m_Children == null || m_Children.Count == 0) { return(false); } int n = 0; while (n < m_Children.Count && currentChild == null) // its the first event that is not "sucessful" { TaskEventArg child = (TaskEventArg)m_Children[n]; if (child.Status != TaskStatus.Success) // look for first child that is not completed! { currentChild = child; // If found, he is the current working task } n++; } if (currentChild == null) { currentChild = m_Children[m_Children.Count - 1]; // return last child at least. } // Exit. return(true); }//TryGetCurrentChild()
public bool TryToAddChild(TaskEventArg newChild) { if (m_Children == null) { m_Children = new List <TaskEventArg>(); } if (m_Children.Contains(newChild)) { return(false); } m_Children.Add(newChild); newChild.m_Parent = this; return(true); }// TryToAddChild()
// // private void OnTaskCompleted(TaskEventArg completedTask) { Log.NewEntry(LogLevel.Minor, "OnTaskCompleted: {0}", completedTask); if (m_WaitingTasks.Contains(completedTask)) { Log.NewEntry(LogLevel.Minor, "OnTaskCompleted: Removing from wait list.", completedTask); m_WaitingTasks.Remove(completedTask); } if (TaskCompleted != null) { TaskCompleted(this, completedTask); } }
// // #endregion//Constructors #region Private Methods // ***************************************************************** // **** Private Methods **** // ***************************************************************** // private void Reconciler_RequestCompleted(object sender, EventArgs eventArg) { if (eventArg is Misty.Lib.TaskHubs.TaskEventArg) { Misty.Lib.TaskHubs.TaskEventArg request = (Misty.Lib.TaskHubs.TaskEventArg)eventArg; if (Log != null) { Log.NewEntry(LogLevel.Major, "ReconcileForm: Request completed for {0}. Requesting shut down", request); } // Request a shutdown soon. if (Log != null) { Log.NewEntry(LogLevel.Major, "ReconcileForm: Requesting a shut down."); } TaskEventArg task = new TaskEventArg(); task.RequestStop = true; task.StartTime = DateTime.Now.AddSeconds(20); if (m_ReconcilerTaskHubs.Count > 0) { m_ReconcilerTaskHubs[0].AddNewTask(task); } } }//Reconciler_RequestCompleted()
public bool TryGetParent(out TaskEventArg parent) { parent = m_Parent; return(parent != null); }
public bool IsParentOf(TaskEventArg possibleChild) { return(this.m_Children != null && this.m_Children.Contains(possibleChild)); }
}// HubEventHandler() // // // ***************************************************** // **** ProcessTask() **** // ***************************************************** /// <summary> /// Processes tasks from user. /// </summary> private void ProcessTask(TaskEventArg currentTask) { bool triggerTaskCompletedEvent = false; // Process multiple tasks if (currentTask.IsParent()) // This task has children to be completed in sequence. { TaskEventArg currentChildTask = null; // the child-request that we are currently working on. if (currentTask.TryGetNextUnsuccessfulChild(out currentChildTask)) { currentTask.Status = TaskStatus.WaitAndTryAgain; // set parent as "working" if (currentChildTask.Status == TaskStatus.Success) // Usually, the "current" child is not finished successfully, { // but if its the last child, then it signals the end of the task. currentTask.Status = TaskStatus.Success; // Mark parent for success... triggerTaskCompletedEvent = true; } else if (currentChildTask.Status == TaskStatus.Failed) // the last non-successful child says its failed... { currentTask.Status = TaskStatus.Failed; // done. Failed request triggerTaskCompletedEvent = true; } else if (DateTime.Now.CompareTo(currentTask.StartTime) >= 0) { // We are passed the start time, submit request. Log.BeginEntry(LogLevel.Minor, "ProcessTask: {0} ", currentTask); Log.AppendEntry(" ----> {0}", currentChildTask); // output the current child to work. Log.EndEntry(); ProcessTask(currentChildTask); // continue trying to work this task. } else { // We are working on a child task, but not past start time yet. Log.NewEntry(LogLevel.Minor, "ProcessTask: Waiting to start {0}. ", currentTask); if (!m_WaitingTasks.Contains(currentTask)) // make sure that we will revist this parent event again... { m_WaitingTasks.Add(currentTask); } } } } else if (currentTask.RequestStop) { if (DateTime.Now.CompareTo(currentTask.StartTime) >= 0) { Log.NewEntry(LogLevel.Major, "ProcessTask: {0} ----> Stop requested.", currentTask); if (m_Timer != null) { m_Timer.Stop(); m_Timer.Dispose(); m_Timer = null; } currentTask.Status = TaskStatus.Success; base.Stop(); } else { Log.NewEntry(LogLevel.Minor, "ProcessTask: Waiting to start {0}. ", currentTask); if (!m_WaitingTasks.Contains(currentTask)) // make sure that we will revist this parent event again... { m_WaitingTasks.Add(currentTask); } } } else { // // Process requests for task. // if (!string.IsNullOrEmpty(currentTask.EventHandlerName)) { Type thisType = this.GetType(); System.Reflection.MethodInfo[] methodInfos = thisType.GetMethods(); // Task EventHandlerName must match super-class method name! System.Reflection.MethodInfo m = null; // Method to do the task. for (int ptr = 0; ptr < methodInfos.Length; ptr++) { if (methodInfos[ptr].Name.Equals(currentTask.EventHandlerName)) { m = methodInfos[ptr]; break; // stop searching } } if (m != null) { // We found the named method to call. // We ASSUME that the implementation method will set the "Status" of the event properly! // ** This is critical ** m.Invoke(this, new object[] { this, currentTask }); if (currentTask.Status == TaskStatus.New) { Log.NewEntry(LogLevel.Major, "ProcessTask: Method {0} has failed to change the TaskEventArg.Status field!", currentTask.EventHandlerName); Log.Flush(); currentTask.Status = TaskStatus.Failed; throw (new Exception(string.Format("Method {0}(object,EventArg) must set TaskEventArg.Status field!", currentTask.EventHandlerName))); } } else { Log.NewEntry(LogLevel.Major, "ProcessTask: Unable to find method {0}.", currentTask.EventHandlerName); currentTask.Status = TaskStatus.Failed; } } else { Log.NewEntry(LogLevel.Error, "ProcessTask: No eventHanderName."); currentTask.Status = TaskStatus.Failed; } // // Check final status of task // TaskEventArg parent; if (currentTask.TryGetParent(out parent)) { // This Task has a parent Task. if (currentTask.Status == TaskStatus.WaitAndTryAgain) { // The (child) current task wants to try again. Log.NewEntry(LogLevel.Minor, "ProcessTask: {0} has parent, will wait to try again.", currentTask); if (!m_WaitingTasks.Contains(parent)) // make sure that we will revist this parent event again... { m_WaitingTasks.Add(parent); } } else { Log.NewEntry(LogLevel.Minor, "ProcessTask: {0} has parent, will flash parent.", currentTask); this.HubEventEnqueue(parent); // flash parent to continue working itself. } } else if (currentTask.Status == TaskStatus.WaitAndTryAgain && DateTime.Now.CompareTo(currentTask.StopTime) < 0) { // This task is not complete, and is allowed to work more. if (!m_WaitingTasks.Contains(currentTask)) // make sure that we will revist this parent event again... { Log.NewEntry(LogLevel.Minor, "ProcessTask: Adding {0} to waiting queue.", currentTask); m_WaitingTasks.Add(currentTask); } else { Log.NewEntry(LogLevel.Minor, "ProcessTask: {0} already in waiting queue.", currentTask); } } else { // This task is complete, either failed, succeeded or event timed out.. whatever. triggerTaskCompletedEvent = true; } } // Exit if (triggerTaskCompletedEvent) { OnTaskCompleted(currentTask); } }//ProcessRequest()
}//TimerIntervalMinutes // #endregion//Properties #region Public Methods // ***************************************************************** // **** Public Methods **** // ***************************************************************** // // public bool AddNewTask(TaskEventArg newTask) { return(this.HubEventEnqueue(newTask)); }