// enqueue a task normally scheduled on the heartbeat
        public static void Run_OnCommand(CommandEventArgs e)
        {
            Mobile from = e.Mobile;

            if (from.AccessLevel >= AccessLevel.Administrator)
            {
                string cmd = e.GetString(0);
                if (cmd.Length > 0)
                {
                    CronEventEntry cee = Cron.Find(cmd);
                    if (cee == null)
                    {
                        e.Mobile.SendMessage("No task named \"{0}\".", cmd);
                    }
                    else
                    {                           // queue it!
                        lock (m_TaskQueue.SyncRoot)
                        {
                            m_TaskQueue.Enqueue(cee);
                        }
                        e.Mobile.SendMessage("Task \"{0}\" enqueued.", cee.Name);
                    }
                }
                else
                {
                    e.Mobile.SendMessage("Usage: run <TaskID>");
                }
            }
        }
        public static string[] List(string pattern)
        {
            ArrayList list    = new ArrayList();
            Regex     Pattern = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.ECMAScript);

            lock (m_Handlers.SyncRoot)
            {
                foreach (object o in m_Handlers)
                {
                    CronEventEntry cee = o as CronEventEntry;
                    if (cee == null)
                    {
                        continue;
                    }

                    if (Pattern.IsMatch(cee.Name))
                    {
                        list.Add(String.Format("Job: '{0}', Cron: '{1}', Task: {2}\r\n",
                                               cee.Name,
                                               cee.Cronjob.Specification,
                                               cee.Handler.Target == null ? cee.Name : cee.Handler.Target.ToString()));
                    }
                }
                return((string[])list.ToArray(typeof(string)));
            }
        }
        }         // CronProcess()

        private static void CronProcess(CronEventEntry cee)
        {
            if (cee == null)
            {
                return;
            }

            try
            {
                // run the user code
                if (cee.Running == false)
                {
                    Console.WriteLine("Skipping queued Job {0} because it was killed", cee.Name);
                }
                else
                {
                    // okay, run the scheduled task.
                    Utility.TimeCheck tc = new Utility.TimeCheck();
                    Console.Write("{0}: ", cee.Name);
                    tc.Start();                                                 // track the toal time for [lag forensics
                    cee.Handler();                                              // execute the next task in the queue
                    tc.End();
                    AuditTask(cee, tc);                                         // maintain our list of 5 most recent tasks
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
                Console.WriteLine("Exception caught in scheduled task: {0}", ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        public static bool Run(string pattern)
        {
            lock (m_Handlers.SyncRoot)
            {
                foreach (object o in m_Handlers)
                {
                    CronEventEntry cee = o as CronEventEntry;
                    if (cee == null)
                    {
                        continue;
                    }

                    if (pattern.ToLower() == cee.Name.ToLower())
                    {
                        try
                        {                           // call as a foreground process
                            cee.Handler();
                        }
                        catch (Exception ex)
                        {
                            LogHelper.LogException(ex);
                            Console.WriteLine("Exception caught in User Code: {0}", ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }
        private static void AuditTask(CronEventEntry cee, Utility.TimeCheck tc)
        {
            // here we maintain our list of 5 most recent tasks
            if (m_RecentTasks.Count == 5)
            {
                m_RecentTasks.RemoveAt(0);                  //remove first (oldest) task
            }
            else if (m_RecentTasks.Count > 5)
            {
                m_RecentTasks.Clear();                      // this shouldn't be possible
            }
            // but stranger things have happened!

            // add new task and elapsed time as a LagStats struct at bottom of the list
            m_RecentTasks.Add(new LagStats(cee.Name, tc.TimeTaken));
        }
 public static void QueueTask(string Name, CronEventHandler handler, string CronSpec, bool Unique)
 {
     lock (m_TaskQueue.SyncRoot)
     {
         CronEventEntry task = new CronEventEntry(Name, handler, new CronJob(CronSpec), Unique, null);
         if (Unique == true)
         {                   // only one
             if (m_TaskQueue.Contains(task) == false)
             {
                 m_TaskQueue.Enqueue(task);
             }
         }
         else
         {
             m_TaskQueue.Enqueue(task);
         }
     }
 }
        public static string[] Kill(string pattern)
        {
            lock (m_Handlers.SyncRoot)
            {
                ArrayList list     = new ArrayList();
                ArrayList ToDelete = new ArrayList();
                Regex     Pattern  = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.ECMAScript);

                foreach (object o in m_Handlers)
                {
                    CronEventEntry cee = o as CronEventEntry;
                    if (cee == null)
                    {
                        continue;
                    }

                    if (Pattern.IsMatch(cee.Name))
                    {
                        list.Add(String.Format("Deleted: '{0}', Cron: '{1}', Task: {2}\r\n",
                                               cee.Name,
                                               cee.Cronjob.Specification,
                                               cee.Handler.Target == null ? cee.Name : cee.Handler.Target.ToString()));

                        ToDelete.Add(cee);
                    }
                }

                foreach (object o in ToDelete)
                {
                    CronEventEntry cee = o as CronEventEntry;
                    if (cee == null)
                    {
                        continue;
                    }

                    cee.Running = false;                            // stop any ones queued in the 'temp' list from running
                    m_Handlers.Remove(cee);                         // remove from master list
                }

                return((string[])list.ToArray(typeof(string)));
            }
        }
        public static CronEventEntry Find(string pattern)
        {
            lock (m_Handlers.SyncRoot)
            {
                foreach (object o in m_Handlers)
                {
                    CronEventEntry cee = o as CronEventEntry;
                    if (cee == null)
                    {
                        continue;
                    }

                    if (pattern.ToLower() == cee.Name.ToLower())
                    {
                        return(cee);
                    }
                }
            }

            return(null);
        }
        public static void CronSlice()
        {               // get datetime adjusted for DST
                        //	please find the full explanation of how we manage time in the developers library
            DateTime Now = Cron.GameTimeNow;

            try
            {                   // initialize for first run
                if (lastTime == DateTime.MinValue)
                {
                    lastTime = Now;
                }
                bool quit = false;
                while (quit == false)
                {
                    // get the time
                    DateTime thisTime = Now;

                    // wait for the minute to roll over
                    if (lastTime.Minute == thisTime.Minute)
                    {
                        break;
                    }

                    // remember this time
                    lastTime = thisTime;

                    lock (m_Handlers.SyncRoot)
                    {
                        foreach (object o in m_Handlers)
                        {
                            CronEventEntry cee = o as CronEventEntry;
                            if (cee == null)
                            {
                                continue;
                            }

                            // match times based on DST adjusted server time
                            if (cee.Cronjob.Match(thisTime))
                            {
                                // process special CronLimit specifications, like 3rd Sunday
                                if (cee.Limit != null && cee.Limit.Execute() == false)
                                {
                                    Console.WriteLine("Note: Scheduled Job '{0}' does not meet nth day limitation.", cee.Name);
                                    continue;
                                }

                                // we have a match, queue it!
                                lock (m_TaskQueue.SyncRoot)
                                {
                                    bool Add = true;

                                    // only queue 'unique' tasks
                                    if (m_TaskQueue.Count > 0 && cee.Unique && m_TaskQueue.Contains(cee))
                                    {
                                        Add = false;
                                        Console.WriteLine("Note: Duplicate Job '{0}' ignored.", cee.Name);
                                    }

                                    // max job queue size = 128
                                    if (Add && m_TaskQueue.Count == 128)
                                    {                                           // should probably add an exception here
                                        CronEventEntry temp = m_TaskQueue.Dequeue() as CronEventEntry;
                                        Console.WriteLine("Warning: Job queue overflow. Lost job '{0}'", temp.Name);
                                    }

                                    // add the task to the queue
                                    if (Add == true)
                                    {
                                        m_TaskQueue.Enqueue(cee);
                                    }
                                }
                            }
                        }
                    }                     // lock

                    // since we're not running as a thread, just one pass
                    quit = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.LogException(ex);
                Console.WriteLine("Exception caught in CronScheduler: {0}", ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }         // CronScheduler()
Beispiel #10
0
		private static void AuditTask(CronEventEntry cee, Utility.TimeCheck tc)
		{
			// here we maintain our list of 5 most recent tasks
			if (m_RecentTasks.Count == 5)
				m_RecentTasks.RemoveAt(0);  //remove first (oldest) task
			else if (m_RecentTasks.Count > 5)
				m_RecentTasks.Clear();      // this shouldn't be possible 
			// but stranger things have happened!

			// add new task and elapsed time as a LagStats struct at bottom of the list
			m_RecentTasks.Add(new LagStats(cee.Name, tc.TimeTaken));
		}
Beispiel #11
0
		} // CronProcess()

		private static void CronProcess(CronEventEntry cee)
		{
			if (cee == null) return;

			try
			{
				// run the user code                               
				if (cee.Running == false)
					Console.WriteLine("Skipping queued Job {0} because it was killed", cee.Name);
				else
				{
					// okay, run the scheduled task.
					Utility.TimeCheck tc = new Utility.TimeCheck();
					Console.Write("{0}: ", cee.Name);
					tc.Start();					// track the toal time for [lag forensics
					cee.Handler();				// execute the next task in the queue
					tc.End();
					AuditTask(cee,tc);			// maintain our list of 5 most recent tasks
				}
			}
			catch (Exception ex)
			{
				LogHelper.LogException(ex);
				Console.WriteLine("Exception caught in scheduled task: {0}", ex.Message);
				Console.WriteLine(ex.StackTrace);
			}
		}
Beispiel #12
0
		public static void QueueTask(string Name, CronEventHandler handler, string CronSpec, bool Unique)
		{
			lock (m_TaskQueue.SyncRoot)
			{
				CronEventEntry task = new CronEventEntry(Name, handler, new CronJob(CronSpec), Unique, null);
				if (Unique == true)
				{   // only one
					if (m_TaskQueue.Contains(task) == false)
						m_TaskQueue.Enqueue(task);
				}
				else
					m_TaskQueue.Enqueue(task);
			}
		}