/// <summary>
 ///     Adds a common task to the queue
 /// </summary>
 /// <param name="task"></param>
 internal static void Add(CommonTask task)
 {
     if (QueuedTasks.All(x => x != task))
     {
         QueuedTasks.Add(task);
     }
 }
Esempio n. 2
0
        public void Dispatch(object sender, EventArgs e)
        {
            //dispatch registered tasks, if dispatching already, return. * shouldn't happen if sensible interval set
            bool taken = false;

            try
            {
                ((System.Timers.Timer)sender).Stop();
                Monitor.TryEnter(lck, lock_wait, ref taken);
                if (!taken)
                {
                    return;
                }

                foreach (var t in Tasks)
                {
                    if (ShouldRunNow(t) && !QueuedTasks.Contains(t.ID))
                    {
                        QueuedTasks.Add(t.ID);
                        System.Threading.Tasks.Task.Factory.StartNew(() => {
                            t.DoRun(this.cancellationToken);
                        }, cancellationToken);
                        //.ContinueWith(x=>OnTaskEnd(t));
                    }
                }
                ;
            }
            finally {
                if (taken)
                {
                    Monitor.Exit(lck);
                }
                ((System.Timers.Timer)sender).Start();
            }
        }
Esempio n. 3
0
 public void Publish(object objectToSend, string routingKey, string exchange)
 {
     try
     {
         Send(objectToSend, routingKey, exchange);
     }
     catch (AlreadyClosedException)
     {
         QueuedTasks.QueueMethod(() => Send(objectToSend, routingKey, exchange));
     }
 }
        /// <summary>
        ///     Runs all scheduled common tasks on a separate thread.
        /// </summary>
        internal static void Run()
        {
            if (GameBase.Game.TimeRunning - LastRunTime <= 5000 || IsRunning || QueuedTasks.Count == 0)
            {
                return;
            }

            var taskList = new List <Task>();

            // Thread that completes all the tasks.
            var taskThread = new Thread(() =>
            {
                foreach (var task in taskList)
                {
                    task.Start();
                }

                // Wait for all the tasks to complete.
                Task.WaitAll(taskList.ToArray());

                // Clear the queue of commonly ran tasks.
                QueuedTasks.Clear();
                IsRunning = false;

                // Set the last run time.
                LastRunTime = GameBase.Game.TimeRunning;
            });

            // Add all common tasks to the queue.
            foreach (var task in QueuedTasks)
            {
                switch (task)
                {
                // Writes the user's cnfig file.
                case CommonTask.WriteConfig:
                    taskList.Add(new Task(async() => await ConfigManager.WriteConfigFileAsync()));
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // Start the thread once we have all the queued tasks setup.
            taskThread.Start();
            IsRunning = true;
        }
Esempio n. 5
0
 public void HandleTaskEnd(object s, DispatchEventArgs e)
 {
     QueuedTasks.Remove(e.Task.ID);
     if (!e.Task.Recurring)
     {
         Tasks.Remove(e.Task);
     }
     if ((PuckCache.UpdateTaskLastRun && !e.Task.Recurring) || (PuckCache.UpdateRecurringTaskLastRun && e.Task.Recurring))
     {
         var repo     = PuckCache.PuckRepo;
         var taskMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.Tasks && x.ID == e.Task.ID).FirstOrDefault();
         if (taskMeta != null)
         {
             taskMeta.Value = JsonConvert.SerializeObject(e.Task);
             repo.SaveChanges();
             repo = null;
         }
     }
 }
Esempio n. 6
0
        public void HandleTaskEnd(object s, DispatchEventArgs e)
        {
            int removedId = 0;

            QueuedTasks.Remove(e.Task.ID, out removedId);
            if (!e.Task.Recurring)
            {
                Tasks.Remove(e.Task);
            }
            if ((PuckCache.UpdateTaskLastRun && !e.Task.Recurring) || (PuckCache.UpdateRecurringTaskLastRun && e.Task.Recurring))
            {
                using (var scope = PuckCache.ServiceProvider.CreateScope())
                {
                    var repo     = scope.ServiceProvider.GetService <I_Puck_Repository>();
                    var taskMeta = repo.GetPuckMeta().Where(x => x.Name == DBNames.Tasks && x.ID == e.Task.ID).FirstOrDefault();
                    if (taskMeta != null)
                    {
                        taskMeta.Value = JsonConvert.SerializeObject(e.Task);
                        repo.SaveChanges();
                        repo = null;
                    }
                }
            }
        }
Esempio n. 7
0
 static void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     QueuedTasks.ExecuteAction();
 }