Exemplo n.º 1
0
        /// <summary>
        /// Handles and executes a task.
        /// </summary>
        /// <param name="task"></param>
        private static void HandleTask(GuardianTask task)
        {
            if (task is ScheduledConfig config)
            {
                // This is obv not gerenic. Maybe if the settings are getting way more, we could be more generic here.
                switch (config.Name)
                {
                case "DIP_AutomaticBackupIntervall":
                    GuardianRequest?.Invoke(RequestTypes.Backup);
                    break;

                case "DI_AutomaticScans":
                    GuardianRequest?.Invoke(RequestTypes.Scan);
                    break;

                case "DI_AutomaticSaves":
                    GuardianRequest?.Invoke(RequestTypes.Save);
                    break;

                default:
                    throw new GuardianException($"{task.Name} could not be handled.");
                }
                // Task has been handled => Inform the View and delete this instance.
                _taskPool.Remove(config.Gtid, out var obsolete);
                _upcomingTasks.Remove(config.Gtid);
                GuardianHandledTask?.Invoke(config);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a task to the task pool of the guardian.
        /// </summary>
        /// <param name="task"></param>
        public static void AddTask(GuardianTask task)
        {
            if (_taskPool == null)
            {
                throw new GuardianException($"The guardian has not been launched yet!");
            }

            if (!_taskPool.TryAdd(task.Gtid, task))
            {
                throw new GuardianException($"Given task can not be added into the task-pool. The Gtid must be unique.");
            }
        }
Exemplo n.º 3
0
 private void Log_GuardianHandledTask(GuardianTask task) => Log("Guardian executed a task", $"Executed: {task.Description}", EntryType.Success);
Exemplo n.º 4
0
        /// <summary>
        /// When a scheduled setting (DI/DIP) has been executed, the time for it's next execution will be updated here.
        /// </summary>
        public static GuardianTask RaiseHandledSetting(GuardianTask task)
        {
            var splited = task.Name.Split('_');

            if (splited[0] != "DI" && splited[0] != "DIP")
            {
                throw new FileFormatException($"{task.Name} could not be raised after being executed - SettingInterval does not match.");
            }

            if (task is ScheduledConfig config)
            {
                switch (config.Gtid.Interval.Trim())
                {
                case "Hourly":
                    while (config.Gtid.ExecutionDate.Date <= DateTime.Now.Date && config.Gtid.ExecutionDate.TimeOfDay < DateTime.Now.TimeOfDay)
                    {
                        config.Gtid.ExecutionDate = config.Gtid.ExecutionDate.AddHours(1);
                    }
                    break;

                case "Daily":
                    while (config.Gtid.ExecutionDate.Date <= DateTime.Now.Date)
                    {
                        config.Gtid.ExecutionDate = config.Gtid.ExecutionDate.AddDays(1);
                    }
                    break;

                case "Weekly":
                    while (config.Gtid.ExecutionDate.Date <= DateTime.Now.Date)
                    {
                        config.Gtid.ExecutionDate = config.Gtid.ExecutionDate.AddDays(7);
                    }
                    break;

                case "Monthly":
                    while (config.Gtid.ExecutionDate.Date <= DateTime.Now.Date)
                    {
                        config.Gtid.ExecutionDate = config.Gtid.ExecutionDate.AddMonths(1);
                    }
                    break;

                case "Yearly":
                    while (config.Gtid.ExecutionDate.Date <= DateTime.Now.Date)
                    {
                        config.Gtid.ExecutionDate = config.Gtid.ExecutionDate.AddYears(1);
                    }
                    break;

                default:
                    throw new FileFormatException($"{config.Name} could not be raised after being executed.");
                }

                var newValue = (splited[0] == "DI") ?
                               $"{(task.Gtid.ExecutionDate)},{config.Parameters[1]}" :
                               $"{(task.Gtid.ExecutionDate)},{config.Parameters[1]},{config?.Parameters[2]}";

                // Save setting and inform others that a config has been raised.
                SaveSetting(config.Name, newValue);
                HandledSettingHasBeenRaised?.Invoke(config);

                Logger.log.Info($"{config.Name} has been raised to {config.Gtid.ExecutionDate}.");
                return(task);
            }
            else
            {
                throw new FileFormatException($"{task.Name} was not a raisable setting.");
            }
        }