Ejemplo n.º 1
0
        /// <summary>
        /// Launches the guardian Thread and overwrites a possible existing one.
        /// </summary>
        /// <returns></returns>
        public static bool LaunchGuardian(HashSet <GuardianTask> initialTasks, object[] parameters)
        {
            try
            {
                _taskPool      = new ConcurrentDictionary <GTIdentifier, GuardianTask>();
                _upcomingTasks = new HashSet <GTIdentifier>();

                // Add the intial tasks
                foreach (var task in initialTasks)
                {
                    AddTask(task);
                }

                UtilityParameters.HandleFortressParameters(parameters);

                _guardianThread = new Thread(new ThreadStart(GuardianThread))
                {
                    Name         = "GUARDIAN_THREAD",
                    IsBackground = true
                };
                _guardianIsRunning = true; // Call this before starting the thread!
                _guardianThread.Start();
                GuardianStarted?.Invoke();
                return(true);
            }
            catch (Exception ex)
            {
                GuardianThrewException?.Invoke(ex, "Couldn't launch the guardian.");
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Makes the guardian reloads it's task pool with the given List
        /// </summary>
        public static void ReloadTasks(HashSet <GuardianTask> initialTasks)
        {
            try
            {
                _taskPool.Clear();

                foreach (var task in initialTasks)
                {
                    AddTask(task);
                }
            }
            catch (Exception ex)
            {
                GuardianThrewException?.Invoke(ex, "There was an error reloading your scheduled configs.");
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Stops the gurdian.
 /// </summary>
 /// <returns></returns>
 public static bool StopGuardian(bool supressMessage = false)
 {
     try
     {
         _guardianIsRunning = false;
         _taskPool.Clear();
         _upcomingTasks.Clear();
         if (!supressMessage)
         {
             GuardianStopped?.Invoke("Guardian has been manually stopped.");
         }
         return(true);
     }
     catch (Exception ex)
     {
         GuardianThrewException?.Invoke(ex, "Couldn't stop the guardian.");
         return(false);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// The continous thread that stays in the background and completes task after task.
        /// </summary>
        private static void GuardianThread()
        {
            lock (_lock)
            {
                try
                {
                    while (_guardianIsRunning)
                    {
                        CheckUpcomingTasks();

                        if (_upcomingTasks.Count > 0)
                        {
                            // Take the next possible task
                            var nextTask = _upcomingTasks.FirstOrDefault(t => t.ExecutionDate <= DateTime.Now);
                            if (nextTask != default)
                            {
                                //Handle the task now!
                                if (_taskPool.TryGetValue(nextTask, out var task))
                                {
                                    HandleTask(task);
                                }
                                else
                                {
                                    ;// Inform someone that a task is missing.
                                }
                            }
                        }
                        // Maybe let the user decide the frequency of the Guardian later.
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception ex)
                {
                    GuardianThrewException?.Invoke(ex, ex.Message);
                    GuardianStopped?.Invoke($"The guardian detected an error and has been shut down. " +
                                            $"After the error has been fixed, try to restart him manually via the Home-Hub. {Environment.NewLine}" +
                                            $"Error: {ex.Message}");
                }
            }
        }