private static bool RecoursiveCheckGraph(Task root, ImmutableList<Guid> previousTasks)
        {
            Contract.Requires(root != null);
            Contract.Requires(root.Inputs != null);
            Contract.Requires(previousTasks != null);

            if (previousTasks.Contains(root.Id))
            {
                Logger.Write(
                    LogCategories.Error(string.Format(
                    "{0} is cycled.", root),
                    LogCategories.TaskServices));
                return false;
            }

            foreach (var task in root.Inputs)
            {
                if (previousTasks.Contains(task.Id))
                {
                    Logger.Write(
                        LogCategories.Error(string.Format(
                        "{0} is cycled.", task),
                        LogCategories.TaskServices));
                    return false;
                }

                if (!RecoursiveCheckGraph(task, previousTasks.Add(root.Id)))
                    return false;
            }
            return true;
        }
Пример #2
0
        /// <summary>
        /// Removes task from task manager.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/>.
        /// </param>
        public void DeleteTask(Task task)
        {
            lock (task)
            {
                switch (task.State)
                {
                    case TaskState.NotReady:
                        FromNotReady(task);
                        break;

                    case TaskState.Completed:
                        FromCompleted(task);
                        break;

                    case TaskState.Failed:
                        FromFailed(task);
                        break;

                    case TaskState.Cancelled:
                        FromCancelled(task);
                        break;

                    default:
                        throw new InvalidOperationException(
                           string.Format("Task {0} has unexpected state {1} on TaskQueue::DeleteTask()", task, task.State));
                }

                task.State = TaskState.Deleted;
            }
        }
Пример #3
0
        void ITaskQueue.TaskFailed(Task task, ErrorReport errorReport)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentNullException>(errorReport != null);
            Contract.Requires<ArgumentException>(task.State == TaskState.Processing);
            Contract.Ensures(task.State == TaskState.Failed);
            Contract.Ensures(task.Error == errorReport);

            throw new NotImplementedException();
        }
Пример #4
0
        /// <summary>
        /// Cancels execution of specified task.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/>.
        /// </param>
        public void CancelTask(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(
                task.State == TaskState.NotReady ||
                task.State == TaskState.Pending ||
                task.State == TaskState.Processing);
            Contract.Ensures(task.State == TaskState.Cancelled);

            throw new NotImplementedException();
        }
Пример #5
0
        /// <summary>
        /// Removes task from task manager.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/>.
        /// </param>
        public void DeleteTask(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(
                task.State == TaskState.Completed ||
                task.State == TaskState.Failed ||
                task.State == TaskState.Cancelled ||
                task.State == TaskState.Unavailible);
            Contract.Ensures(task.State == TaskState.Deleted);

            throw new NotImplementedException();
        }
Пример #6
0
        /// <summary>
        /// Cancels execution of specified task.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/>.
        /// </param>
        public void CancelTask(Task task)
        {
            lock (task)
            {
                switch (task.State)
                {
                    case TaskState.Processing:
                        FromProcessing(task);
                        break;
                    case TaskState.Pending:
                        FromPending(task);
                        break;
                    case TaskState.NotReady:
                        FromNotReady(task);
                        break;
                    default:
                        throw new InvalidOperationException(
                            string.Format("Task {0} has unexpected state {1} on TaskQueue::CancelTask()", task, task.State));
                }

                ToCancelled(task);
            }
        }
Пример #7
0
        private void ToFailed(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State != TaskState.Failed);
            Contract.Ensures(task.State == TaskState.Failed);

            task.State = TaskState.Failed;
            lock (failedTasks)
            {
                failedTasks.Add(task);
            }
        }
Пример #8
0
        private void FromUnavailible(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            lock (unavailibleTasks)
            {
                unavailibleTasks.Remove(task);
            }
        }
Пример #9
0
 /// <summary>
 /// Reinitializes specified task.
 /// </summary>
 /// <param name="task">
 /// An instance of <see cref="Task"/>.
 /// </param>
 public void RestartTask(Task task)
 {
     lock (task)
     {
         FromCancelled(task);
         ToUnavailible(task);
         StartTask(task);
         ResetDependencyCounter(task);
     }
 }
Пример #10
0
        /// <summary>
        /// Informs task queue that specified <paramref name="task"/> has been rejected by worker.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/> that has been executed.
        /// </param>
        public void TaskRejected(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State == TaskState.Processing);
            Contract.Ensures(task.State == TaskState.Pending);

            throw new NotImplementedException();
        }
Пример #11
0
        /// <summary>
        /// Registers a task in task manager and sets its UID.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="DataModel.Task"/>.
        /// </param>
        public void RegisterTask(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State == TaskState.Unattached);
            Contract.Ensures(task.State == TaskState.Unavailible);

            throw new NotImplementedException();
        }
Пример #12
0
        private void ToUnavailible(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State != TaskState.Unavailible);
            Contract.Ensures(task.State == TaskState.Unavailible);

            task.State = TaskState.Unavailible;
            lock (unavailibleTasks)
            {
                unavailibleTasks.Add(task);
            }
        }
Пример #13
0
        private void FromCompleted(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            lock (completedTasks)
            {
                completedTasks.Remove(task);
            }
        }
Пример #14
0
 private static void ResetDependencyCounter(Task task)
 {
     task.DependenciesCount = (uint)task.Inputs.Count;
 }
Пример #15
0
 /// <summary>
 /// Informs task queue that specified <paramref name="task"/> has been rejected by worker.
 /// </summary>
 /// <param name="task">
 /// An instance of <see cref="Task"/> that has been executed.
 /// </param>
 public void TaskRejected(Task task)
 {
     lock (task)
     {
         FromProcessing(task);
         ToPending(task);
     }
 }
Пример #16
0
        /// <summary>
        /// Informs task queue that specified <paramref name="task"/> has failed.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/> that failed.
        /// </param>
        /// <param name="errorReport">
        /// Error report.
        /// </param>
        public void TaskFailed(Task task, ErrorReport errorReport)
        {
            lock (task)
            {
                FromProcessing(task, terminateProcessing: false);
                ToFailed(task);
                task.Error = errorReport;

                var job = task.Job;
                lock (job)
                {
                    Logger.Write(
                        LogCategories.Error(
                            string.Format("Task {0} failed because of {1}. This caused job {2} to fail.", task,
                                          errorReport.Error, task.Job),
                            LogCategories.TaskServices,
                            LogCategories.TaskQueue));

                    job.State = JobState.Failed;

                    foreach (var t in task.Job.Tasks)
                    {
                        lock (t)
                        {
                            switch (t.State)
                            {
                                case TaskState.NotReady:
                                    FromNotReady(t);
                                    break;

                                case TaskState.Processing:
                                    FromProcessing(t);
                                    break;

                                case TaskState.Failed:
                                    continue;

                                case TaskState.Pending:
                                    FromPending(t);
                                    break;

                                case TaskState.Completed:
                                    FromCompleted(t);
                                    break;

                                default:
                                    throw new InvalidOperationException(
                                        string.Format("Task {0} has unexpected state {1} on TaskQueue::TaskFailed()", task, task.State));
                            }

                            ToFailed(t);
                        }
                    }
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Informs task queue that specified <paramref name="task"/> has been executed successfully.
        /// Task queue performs recalculation of tasks' dependencies.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/> that has been executed.
        /// </param>
        public void TaskCompleted(Task task)
        {
            lock (task)
            {
                if (task.State == TaskState.Failed)
                {
                    // Job failed, there is nothing else to do. The task is already in 'failed' list.
                    return;
                }

                FromProcessing(task);
                ToCompleted(task);

                // Recalculate dependents' states
                foreach (var t in task.Outputs)
                {
                    lock (t)
                    {
                        if (t.State == TaskState.NotReady)
                        {
                            t.DependenciesCount--;
                            if (t.DependenciesCount == 0)
                            {
                                t.State = TaskState.Pending;
                                FromNotReady(t);
                                ToPending(t);
                            }
                        }
                    }
                }

                task.Job.NotifyTaskCompleted(task);
            }
        }
Пример #18
0
 /// <summary>
 /// Starts execution of specified task.
 /// </summary>
 /// <param name="task">
 /// An instance of <see cref="Task"/>.
 /// </param>
 public void StartTask(Task task)
 {
     lock (task)
     {
         ResetDependencyCounter(task);
         FromUnavailible(task);
         if (task.DependenciesCount == 0)
         {
             ToPending(task);
         }
         else
         {
             ToNotReady(task);
         }
     }
 }
Пример #19
0
        private void ToNotReady(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State != TaskState.NotReady);
            Contract.Ensures(task.State == TaskState.NotReady);

            task.State = TaskState.NotReady;
            lock (notReadyTasks)
            {
                notReadyTasks.Add(task);
            }
        }
Пример #20
0
        private void ToPending(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State != TaskState.Pending);
            Contract.Ensures(task.State == TaskState.Pending);

            task.State = TaskState.Pending;
            pendingTasks.Enqueue(task);
        }
Пример #21
0
        private void FromFailed(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            lock (failedTasks)
            {
                failedTasks.Remove(task);
            }
        }
Пример #22
0
 /// <summary>
 /// Initalizes new instance of <see cref="TaskEventArgs"/> class.
 /// </summary>
 /// <param name="task">
 /// An instance of <see cref="Task"/> that provides data for current event.
 /// </param>
 public TaskEventArgs(Task task)
 {
     Task = task;
 }
Пример #23
0
        /// <summary>
        /// Notifies job that <paramref name="task"/> has been failed.
        /// </summary>
        /// <param name="task">
        /// Task that has been failed.
        /// </param>
        public void NotifyTaskFailed(Task task)
        {
            Contract.Requires(task != null);

            lock (this)
            {
                State = JobState.Failed;
                InvokeOnJobFailed();
            }

            Logger.Write(LogCategories.Error(string.Format("Task {0} in job {1} failed.", task, this), LogCategories.TaskServices));
        }
Пример #24
0
        /// <summary>
        /// Starts execution of specified task.
        /// </summary>
        /// <param name="task">
        /// An instance of <see cref="Task"/>.
        /// </param>
        public void StartTask(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);
            Contract.Requires<ArgumentException>(task.State == TaskState.Unavailible);
            Contract.Ensures(task.State == TaskState.Pending ||
                             task.State == TaskState.NotReady);

            throw new NotImplementedException();
        }
Пример #25
0
        private void FromProcessing(Task task, bool terminateProcessing = true)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            if (terminateProcessing)
            {
                var worker = task.Worker as IWorkerChannel;
                if (worker != null)
                {
                    worker.Cancel();
                }
            }

            lock (processingTasks)
            {
                processingTasks.Remove(task);
            }
        }
Пример #26
0
        private void FromNotReady(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            lock (notReadyTasks)
            {
                notReadyTasks.Remove(task);
            }
        }
Пример #27
0
 /// <summary>
 /// Registers a task in task manager and sets its UID.
 /// </summary>
 /// <param name="task">
 /// An instance of <see cref="DataModel.Task"/>.
 /// </param>
 public void RegisterTask(Task task)
 {
     lock (task)
     {
         ToUnavailible(task);
     }
 }
Пример #28
0
        private void FromPending(Task task)
        {
            Contract.Requires<ArgumentNullException>(task != null);

            pendingTasks.Remove(task);
        }
Пример #29
0
        /// <summary>
        /// Notifies job that <paramref name="task"/> has been successfully executed.
        /// </summary>
        /// <param name="task">
        /// Task that has been executed.
        /// </param>
        public void NotifyTaskCompleted(Task task)
        {
            Contract.Requires(task != null);

            Logger.Write(LogCategories.Information(string.Format("Task {0} in job {1} completed.", task, this), LogCategories.TaskServices));

            lock (this)
            {
                incompletedTasksCount--;
                if (incompletedTasksCount == 0)
                {
                    State = JobState.Completed;
                    Logger.Write(LogCategories.Information(string.Format("Job {0} completed.", this), LogCategories.TaskServices));
                    InvokeOnJobCompleted();
                }
            }
        }