예제 #1
0
        public static CrontabEntry Create(ScheduleEntry scheduleEntry)
        {
            CrontabEntry ceb = new CrontabEntry();

            // Initialize the flag arrays
            ceb.SecondFlags = new bool[60];
            ceb.MinuteFlags = new bool[60];
            ceb.HourFlags = new bool[24];
            ceb.MonthFlags = new bool[12];
            ceb.DayOfWeekFlags = new bool[7];
            ceb.DayOfMonthFlags = new bool[31];
            ceb.WeekSequence = Sequence.Undefined;
            ceb.MonthSequence = Sequence.Undefined;
            ceb.YearFlags = new bool[2500];

            ParseToken(scheduleEntry.Seconds, ceb.SecondFlags, false);
            ParseToken(scheduleEntry.Minutes, ceb.MinuteFlags, false);
            ParseToken(scheduleEntry.Hours, ceb.HourFlags, false);
            ParseToken(scheduleEntry.Months, ceb.MonthFlags, true);

            Sequence sequence;

            ParseToken(scheduleEntry.DaysOfWeek, ceb.DayOfWeekFlags, false, out sequence);
            ceb.WeekSequence = sequence;

            ParseToken(scheduleEntry.DaysOfMonth, ceb.DayOfMonthFlags, true, out sequence);
            ceb.MonthSequence = sequence;

            ParseToken(scheduleEntry.Years, ceb.YearFlags, false);

            ceb.Tag = scheduleEntry.Tag;

            return ceb;
        }
예제 #2
0
        public void EveryDayAtMidnight()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "*",
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2007, 1, 1);

            DateTime expected = new DateTime(2007, 1, 1);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #3
0
        //-----------------------------------------------------------------------------------------
        /// <summary>
        /// Parses a standard cron string into the schedule.
        /// </summary>
        /// <param name="cronString">Standard cron string.</param>
        /// <returns>A new schedule object based on the specifed string.</returns>
        /// <remarks>
        /// A cron string is made up of a series of fields separated by a space. 
        /// There are normally seven fields in one entry but here only five fields
        //  will be used. The fields are:
        /// second minute hour dom month dow 
        /// 
        /// second  This controls what second of the minute the command will run on,
        ///         and is between '0' and '59'
        ///	minute	This controls what minute of the hour the command will run on,
        ///			and is between '0' and '59'
        ///	hour	This controls what hour the command will run on, and is specified in
        ///			the 24 hour clock, values must be between 0 and 23 (0 is midnight)
        ///	dom		This is the Day of Month, that you want the command run on, e.g. to
        ///			run a command on the 19th of each month, the dom would be 19.
        ///	month	This is the month a specified command will run on, it may be specified
        ///			numerically (1-12), or as the name of the month (e.g. May)
        ///	dow		This is the Day of Week that you want a command to be run on, it can
        ///			also be numeric (0-6) , with sunday as 0 and so on.
        ///
        ///	If you don't wish to specify a value for a field, just place a * in the 
        ///	field.
        ///
        ///	e.g.
        ///	*/5 * * * * *	"Is run every 5 seconds"
        ///	0 01 * * * *	"Is run at one min past every hour"
        ///	0 17 8 * * *	"Is run daily at 8:17:00 am"
        ///	0 17 20 * * *	"Is run daily at 8:17:00 pm"
        ///	0 00 4 * * 0	"Is run at 4 am every Sunday"
        ///	5 42 4 1 * *	"Is run 4:42:05 am every 1st of the month"
        ///	0 01 * 19 07 *	"Is run hourly on the 19th of July"
        ///
        ///	If both the dom and dow are specified, the run time is when both apply ( watch out!)
        ///
        /// * 12 16 * 1		"Is run only when its the 16th and its monday ( next monday 16th )
        ///
        /// The Cron also accepts lists in the fields. Lists can be in the form, 1,2,3 
        ///	(meaning 1 and 2 and 3) or 1-3 (also meaning 1 and 2 and 3).
        ///
        /// 0 59 11 * * 1,2,3,4,5  Will run at 11:59:00 Monday, Tuesday, Wednesday, Thursday and Friday,
        ///	as will:
        /// 0 59 11 * * 1-5
        ///
        ///	Cron also supports 'step' values.
        ///	A value of */2 in the dom field would mean the command runs every two days
        /// and likewise, */5 in the hours field would mean the command runs every 
        ///	5 hours.
        ///	e.g. 
        ///	0 * 12 10-16/2 * * is the same as:
        /// 0 * 12 10,12,14,16 * * 
        ///
        /// 0 */15 9-17 * * * Will run  every 15 mins between the hours or 9am and 5pm
        /// </remarks>
        /// <exception cref="ArgumentException">cronString is null, empty or is incorrectly
        /// formatted</exception>
        public static ScheduleEntry FromChronString(string cronString)
        {
            if (string.IsNullOrEmpty(cronString))
            {
                throw new ArgumentException("cronString");
            }

            ScheduleEntry schedule = new ScheduleEntry();

            string[] tokens = cronString.Split(" \t".ToCharArray());
            int numTokens = tokens.Length;

            // Must have at least 6 token
            if (numTokens < 6)
            {
                throw new ArgumentException("The number of items must be 6 or more ", "cronString");
            }

            for (int i = 0; i < numTokens; i++)
            {
                string token = tokens[i];
                switch (i)
                {
                    // Seconds
                    case 0:
                        schedule.Seconds = token;
                        break;
                    case 1:     // Minutes
                        schedule.Minutes = token;
                        break;
                    case 2:     // Hours
                        schedule.Hours = token;
                        break;
                    case 3:     // Days of month
                        schedule.DaysOfMonth = token;
                        break;
                    case 4:     // Months
                        schedule.Months = token;
                        break;
                    case 5:     // Days of week
                        schedule.DaysOfWeek = token;
                        break;
                    case 6:     // Years
                        schedule.Years = token;
                        break;

                    default:
                        break;
                }
            }

            return schedule;
        }
예제 #4
0
        public void EveryDayForTwoYearsTest()
        {
            // Every day at 00:00:00
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "*",
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            // 2008 is a leap year
            DateTime fromDate = new DateTime(2007, 1, 1);

            while (fromDate.Year < 2009)
            {
                DateTime expected = fromDate;
                DateTime actual = target.NextDateTime(fromDate);
                Assert.AreEqual(expected, actual);

                fromDate = fromDate.AddDays(1);
            }
        }
예제 #5
0
        public void WeekStandardTests()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "1",
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2010, 10, 1);
            while (fromDate.Day < 25)
            {
                int day = fromDate.Day;
                int mondayDay = 1;
                if (day >= 1 && day <= 4)
                    mondayDay = 4;
                else if (day >= 5 && day <= 11)
                    mondayDay = 11;
                else if (day >= 12 && day <= 18)
                    mondayDay = 18;
                else if (day >= 19 && day <= 25)
                    mondayDay = 25;
                DateTime expected = new DateTime(fromDate.Year, fromDate.Month, mondayDay);
                DateTime actual = target.NextDateTime(fromDate);
                Assert.AreEqual(expected, actual);

                fromDate = fromDate.AddDays(1);
            }
        }
예제 #6
0
        public void WeekLastTests()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "4L",      // last thursday
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2010, 10, 1);
            DateTime expected = new DateTime(fromDate.Year, fromDate.Month, 28);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "5L",      // last friday
                Months = "*"
            };
            target = new ScheduleEngine(entry);

            fromDate = new DateTime(2007, 1, 1);
            while (fromDate.Year < 2009)
            {
                expected = FindLastExpectedWeekday(DayOfWeek.Friday, fromDate);
                actual = target.NextDateTime(fromDate);
                Assert.AreEqual(expected, actual);

                fromDate = fromDate.AddDays(1);
            }
        }
예제 #7
0
        public void SpecificMonth()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "7",
                DaysOfWeek = "*",
                Months = "12"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2010, 12, 7);
            DateTime expected = new DateTime(2010, 12, 7);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #8
0
        public void SecondsRangeTest()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "30-40"
            };
            ScheduleEngine target = new ScheduleEngine(entry);
            DateTime fromDate = GetFromDateTime();
            DateTime expected = fromDate.AddSeconds(30);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime(30);
            expected = fromDate;
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime(31);
            expected = fromDate;
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime(40);
            expected = fromDate;
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime(41);
            expected = GetFromDateTime(30).AddMinutes(1);
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #9
0
        public void SecondsIntervalTest()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "*/5"
            };
            ScheduleEngine target = new ScheduleEngine(entry);
            DateTime fromDate = GetFromDateTime();
            DateTime expected = fromDate;
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime().AddSeconds(1);
            expected = GetFromDateTime().AddSeconds(5);
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #10
0
        public void SecondsFixedTest()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "15"
            };
            ScheduleEngine target = new ScheduleEngine(entry);
            DateTime fromDate = GetFromDateTime(0);
            DateTime expected = fromDate.AddSeconds(15);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            fromDate = GetFromDateTime(20);
            expected = GetFromDateTime(15).AddMinutes(1);
            actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #11
0
        public void NastyBugDateTest()
        {
            // Every day at 00:00:00
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "*",
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2007, 11, 30);
            DateTime expected = new DateTime(2007, 11, 30);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);
        }
예제 #12
0
        public void LastThursdayEveryOtherMonth()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "*",
                DaysOfWeek = "4L",
                Months = "*/2"      // jan, mar, may, ... because 0=jan
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2010, 11, 25);
            DateTime expected = new DateTime(2010, 11, 25);
            DateTime actual = target.NextDateTime(fromDate);
            Assert.AreEqual(expected, actual);

            DateTime[] lastThursdays = new DateTime[]
            {
                new DateTime(2010, 1, 28),
                new DateTime(2010, 3, 25),
                new DateTime(2010, 5, 27),
                new DateTime(2010, 7, 29),
                new DateTime(2010, 9, 30),
                new DateTime(2010, 11, 25)
            };

            fromDate = new DateTime(2010,1,1);
            foreach (DateTime lastExpected in lastThursdays)
            {
                actual = target.NextDateTime(fromDate);
                Assert.AreEqual(lastExpected, actual);
                fromDate = actual.AddDays(1);
            }
        }
예제 #13
0
        public void LastDayOfEveryMonth()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "L",
                DaysOfWeek = "*",
                Months = "*"
            };
            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(2007, 1, 1);

            while (fromDate.Year < 2009)
            {
                int daysInMonth = DateTime.DaysInMonth(fromDate.Year, fromDate.Month);
                DateTime expected = new DateTime(fromDate.Year, fromDate.Month, daysInMonth);
                DateTime actual = target.NextDateTime(fromDate);
                Assert.AreEqual(expected, actual, "fromDate = " + fromDate.ToString());

                fromDate = fromDate.AddDays(1);
            }
        }
예제 #14
0
        public void EveryOtherYear()
        {
            ScheduleEntry entry = new ScheduleEntry
            {
                Seconds = "0",
                Minutes = "0",
                Hours = "0",
                DaysOfMonth = "1",
                DaysOfWeek = "*",
                Months = "1",
                Years = "*/2"
            };

            ScheduleEngine target = new ScheduleEngine(entry);

            DateTime fromDate = new DateTime(1969,1,1);
            DateTime expected = new DateTime(1970,1,1);
            DateTime actual;

            while (fromDate.Year < 2400)
            {
                actual = target.NextDateTime(fromDate);
                Assert.AreEqual(expected, actual);

                fromDate = actual.AddDays(1);
                expected = expected.AddYears(2);
            }
        }
예제 #15
0
 public void Add(ScheduleEntry scheduleEntry)
 {
     entries.Add(CrontabEntry.Create(scheduleEntry));
 }
예제 #16
0
 public ScheduleEngine(ScheduleEntry entry)
 {
     entries.Add(CrontabEntry.Create(entry));
 }