Example #1
0
        public (bool, Task) TryAddTask(TaskStructure task)
        {
            if (task == null)
            {
                Logger.Log("Task is null.", Enums.LogLevels.Warn);
                return(false, null);
            }

            if (TaskFactoryCollection.Count > 0)
            {
                foreach (TaskStructure t in TaskFactoryCollection)
                {
                    if (t.TaskIdentifier.Equals(task.TaskIdentifier))
                    {
                        Logger.Log("Such a task already exists. cannot add again!", Enums.LogLevels.Warn);
                        return(false, t.Task);
                    }
                }
            }

            TaskFactoryCollection.Add(task);
            OnTaskAdded(TaskFactoryCollection.IndexOf(task));
            Logger.Log("Task added.", Enums.LogLevels.Trace);
            return(true, task.Task);
        }
Example #2
0
        private void OnTaskAdded(int index)
        {
            if (TaskFactoryCollection.Count <= 0)
            {
                return;
            }

            TaskStructure item = TaskFactoryCollection[index];

            if (item == null)
            {
                return;
            }

            if (item.ExecutionTime < DateTime.Now)
            {
                Logger.Log($"TASK >> {item.TaskIdentifier} is out of its execution time.", Enums.LogLevels.Warn);
                Logger.Log($"TASK >> Removing task {item.TaskIdentifier}");
                TryRemoveTask(item.TaskIdentifier);
                return;
            }

            double taskExecutionSpan = (item.ExecutionTime - DateTime.Now).TotalSeconds;

            Helpers.ScheduleTask(item, TimeSpan.FromSeconds(taskExecutionSpan), item.LongRunning);
            Logger.Log($"TASK >>> {item.TaskIdentifier} will be executed in {TimeSpan.FromSeconds(taskExecutionSpan).TotalMinutes} minutes from now.");
        }
Example #3
0
        public bool TryRemoveTask(float taskId)
        {
            if (TaskFactoryCollection.Count <= 0)
            {
                return(false);
            }

            foreach (TaskStructure task in TaskFactoryCollection)
            {
                if (task.TaskIdentifier.Equals(taskId))
                {
                    PreviousRemovedTask = task;
                    TaskFactoryCollection.Remove(task);
                    Logger.Log($"task with identifier {taskId} has been removed from the queue.", Enums.LogLevels.Trace);
                    OnTaskRemoved(PreviousRemovedTask);
                    return(true);
                }
            }

            return(false);
        }
Example #4
0
 private void OnTaskRemoved(TaskStructure item)
 {
 }