Example #1
0
        public void StopTask(Int64 taskId, bool waitResponse = false)
        {
            if (taskManagerState == TaskManagerState.ServerOnly)
            {
                throw new InvalidOperationException("taskManagerState == TaskManagerState.ServerOnly");
            }

            logger.WriteDebug("Preparing for stopping task... Updating the task in the database...");

            taskDataContext.SetTaskState(taskId, TaskStates.Cancel);

            logger.WriteDebug("The task has been marked as cancelled. Task identifier = {0}", taskId);

            if (waitResponse)
            {
                //TODO: Wait response from the task if needed.
            }
        }
        private void OnCompletedEventHandler(object sender, RunningTaskEventArg eventArg)
        {
            IRunningTask currentTask = (IRunningTask)sender;

            currentTask.OnCompletedEventHandler -= OnCompletedEventHandler;

            TaskStates taskState;
            Int64      taskId = currentTask.TaskModel.Id;

            switch (eventArg)
            {
            case RunningTaskEventArg.Completed:
                logger.WriteTrace("The task has been completed: {0}", taskId);
                taskState = TaskStates.Successed;
                break;

            case RunningTaskEventArg.Cancelled:
                logger.WriteTrace("The task has been canceled: {0}", taskId);
                taskState = TaskStates.Cancelled;
                break;

            case RunningTaskEventArg.Failed:
                logger.WriteTrace("The task has been failed: {0}", taskId);
                taskState = TaskStates.Failed;
                break;

            default:
                throw new NotImplementedException();
            }

            if (taskState == TaskStates.Successed || taskState == TaskStates.Cancelled)
            {
                lock (activeTasks)
                {
                    if (activeTasks.ContainsKey(taskId) == false)
                    {
                        return;
                    }
                    activeTasks.Remove(taskId);
                }

                TaskStateModel taskStateModel = new TaskStateModel(taskId, currentTask.TaskModel.QueueId, currentTask.TaskModel.ServerId, taskState);
                taskDataContext.SetTaskState(taskStateModel);
            }
            else
            {
                //TODO: correct message and details.
                RestartTask(currentTask.TaskModel, "Exception inside", new Exception("TODO"));
            }
        }