Esempio n. 1
0
        static TaskMatch MatchTasks(CronTask task)
        {
            //enum TaskMatch { Unset=0x00,Minute = 0x01, Hour = 0x02, DayOfMonth = 0x04, Month = 0x08, DayOfWeek = 0x16 ,Complete = 0x31};
            TaskMatch ret = TaskMatch.Unset;
            DateTime  dt  = DateTime.Now;          // new DateTime(2011, 4, 26, 10, 17, 0);// DateTime.Today;

            //Console.WriteLine("Diff: " + ((TimeSpan)(dt - task.LastRun)).TotalSeconds.ToString());
            if ((dt - task.LastRun).TotalSeconds < 60)
            {
                return(ret);
            }

            ParseMatch(task.Minute, dt.Minute, ref ret, TaskMatch.Minute);
            ParseMatch(task.Hour, dt.Hour, ref ret, TaskMatch.Hour);
            ParseMatch(task.DayOfMonth, dt.Day, ref ret, TaskMatch.DayOfMonth);
            ParseMatch(task.Month, dt.Month, ref ret, TaskMatch.Month);
            ParseMatch(task.DayOfWeek, (int)dt.DayOfWeek, ref ret, TaskMatch.DayOfWeek);
            return(ret);
        }
Esempio n. 2
0
        public bool Load()
        {
            Tasks.Clear();
            if (!File.Exists)
            {
                return(false);
            }

            using (StreamReader sr = File.OpenText())
            {
                int uid       = 1;
                int lineCount = 1;
                while (!sr.EndOfStream)
                {
                    //Strip line of inline white space
                    String line = RemoveDuplicateWhiteSpace(sr.ReadLine().Trim());

                    //Match crontab format
                    Regex rgx = new Regex(@"\A(?:(?:[0-9*]+|,[0-9]+)+\s+){5}[a-zA-Z0-9\.]+\s+.+", RegexOptions.Singleline);
                    Match m   = rgx.Match(line);
                    if (m.Success)
                    {
                        try
                        {
                            String[] tokens = line.Split(' ');
                            CronTask task   = new CronTask {
                                Minute = tokens[0].Trim(), Hour = tokens[1].Trim(), DayOfMonth = tokens[2].Trim(), Month = tokens[3].Trim(), DayOfWeek = tokens[4].Trim(), User = tokens[5].Trim()
                            };
                            StringBuilder command = new StringBuilder();
                            for (int i = 6; i < tokens.Length; i++)
                            {
                                command.Append(tokens[i].Trim() + ' ');
                            }
                            task.Command  = command.ToString();
                            task.UniqueID = uid++;
                            task.LastRun  = new DateTime(2010, 1, 1);                                   //arbitrary date set to some time at least 1 day in time.  Allows first execution of a task that might be in the middle of a CronCore cycle
                            Tasks.Add(task);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Warning, poorly formed crontab line[" + lineCount.ToString() + "]: " + line);
                        }
                    }
                    else
                    {
                        if (line.Length > 0)
                        {
                            if (line[0] != '#')
                            {
                                Console.WriteLine("Warning, poorly formed crontab line[" + lineCount.ToString() + "]: " + line);
                            }
                        }
                    }
                    lineCount++;
                }
            }
            //Last modified time is the last time this file was written to
            LastModified = File.LastWriteTime;

            return(true);
        }