Exemplo n.º 1
0
        public void RunTask(IRunningTask runningTask)
        {
            lock (RunningTasks)
            {
                RunningTasks.Add(runningTask.TaskId, runningTask);
            }

            runningTask.OnCompletedEventHandler += OnCompletedEventHandler;
            Interlocked.Decrement(ref slots);
            runningTask.Execute();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Occurs when object should do its action.
        /// </summary>
        public void Execute()
        {
            if (taskManagerState == TaskManagerState.ClientOnly)
            {
                logger.WriteDebug("The server has client algorithm only.");
                return;
            }

            logger.WriteDebug("Searching for new tasks in available queue...");

            IReadOnlyCollection <QueueModel> queues = queueManager.GetAvailableQueues();

            foreach (QueueModel queueModel in queues)
            {
                Int64 queueId  = queueModel.Id;
                Int64 serverId = serverManager.ServerId;
                TaskRequestSpecification specification = new TaskRequestSpecification(queueId, serverId, maxTasksPerQueue);
                List <TaskModel>         taskModels;

                bool isReceived = taskDataContext.TryGetAvailableTask(specification, out taskModels);

                if (isReceived)
                {
                    foreach (TaskModel taskModel in taskModels)
                    {
                        IRunningTask currentTask = null;
                        try
                        {
                            currentTask = taskBuilder.Build(taskModel);

                            lock (activeTasks)
                            {
                                activeTasks.Add(taskModel.Id, currentTask);
                            }

                            currentTask.OnCompletedEventHandler += OnCompletedEventHandler;
                            currentTask.Execute(); //TODO: Provide global Cancelation Token.

                            logger.WriteDebug("The task {0} has been received.", taskModel);
                        }
                        catch (Exception ex)
                        {
                            string msg = string.Format("There was an error during executing task: {0}.", taskModel);
                            logger.WriteError(ex, msg);
                            RestartTask(taskModel, msg, ex);
                        }
                    }
                }
                else
                {
                    logger.WriteDebug("There is no new task in the storage.");
                }

                List <Int64> taskIds;
                isReceived = taskDataContext.TryGetCancelTasks(queueId, serverId, out taskIds);
                if (isReceived)
                {
                    foreach (Int64 taskId in taskIds)
                    {
                        logger.WriteTrace("The task (id = {0}) has been marked for cancelation. Cancelling...", taskId);

                        IRunningTask runningTask;
                        lock (activeTasks)
                        {
                            if (activeTasks.ContainsKey(taskId) == false)
                            {
                                continue;
                            }
                            runningTask = activeTasks[taskId];
                        }

                        logger.WriteTrace("The task ({0}) is running. Cancelling...", taskId);
                        try
                        {
                            runningTask.Cancel();
                        }
                        catch (Exception ex)
                        {
                            //TODO: Decide what to do.
                            logger.WriteError("there was an exception during cancelation task.", ex);
                        }

                        logger.WriteTrace("The task has been marked for canceling.");
                    }
                }
                else
                {
                    logger.WriteDebug("There is no task for cancelation.");
                }
            }

            logger.WriteDebug("All tasks have been found if existed.");
        }