Exemplo n.º 1
0
        /// <summary>
        /// Constructs a new instance based on the schedule expression passed in.
        /// </summary>
        /// <param name="expression">A schedule expression. This can either be a 6 field crontab expression
        /// <a href="http://en.wikipedia.org/wiki/Cron#CRON_expression"/> or a <see cref="TimeSpan"/>
        /// string (e.g. "00:30:00").</param>
        public TimerTriggerAttribute(string expression)
        {
            CronSchedule cronSchedule = null;

            if (CronSchedule.TryCreate(expression, out cronSchedule))
            {
                Schedule = cronSchedule;

                DateTime[] nextOccurrences = cronSchedule.InnerSchedule.GetNextOccurrences(DateTime.Now, DateTime.Now + TimeSpan.FromMinutes(1)).ToArray();
                if (nextOccurrences.Length > 1)
                {
                    // if there is more than one occurrence due in the next minute,
                    // assume that this is a sub-minute constant schedule and disable
                    // persistence
                    UseMonitor = false;
                }
                else
                {
                    UseMonitor = true;
                }
            }
            else
            {
                TimeSpan periodTimespan = TimeSpan.Parse(expression);
                Schedule = new ConstantSchedule(periodTimespan);

                // for very frequent constant schedules, we want to disable persistence
                UseMonitor = periodTimespan.TotalMinutes >= 1;
            }
        }
Exemplo n.º 2
0
        public void NullExpression_ReturnsFalseAndNullcronSchedule()
        {
            var result = CronSchedule.TryCreate(null, out CronSchedule schedule);

            Assert.Null(schedule);
            Assert.False(result);
        }
Exemplo n.º 3
0
        public void ToString_ReturnsExpectedValue()
        {
            var result = CronSchedule.TryCreate("0 59 11 * * 1-5", out CronSchedule schedule);

            Assert.True(result);
            Assert.Equal("Cron: '0 59 11 * * 1-5'", schedule.ToString());
        }
Exemplo n.º 4
0
        public void GetNextOccurrence_FivePartCronExpression_NowEqualToNext_ReturnsCorrectValue()
        {
            CronSchedule.TryCreate("* * * * *", out CronSchedule schedule);

            var now  = schedule.GetNextOccurrence(DateTime.Now);
            var next = schedule.GetNextOccurrence(now);

            Assert.True(next > now);
        }
        public void ShouldDisableScheduleMonitor_ReturnsExpectedValue(string schedule, string nowTimestamp, bool expected)
        {
            DateTime now;

            if (!string.IsNullOrEmpty(nowTimestamp))
            {
                now = DateTime.Parse(nowTimestamp);
            }
            else
            {
                now = DateTime.Now;
            }

            CronSchedule.TryCreate(schedule, out CronSchedule cronSchedule);

            Assert.Equal(expected, TimerSchedule.ShouldDisableScheduleMonitor(cronSchedule, now));
        }
Exemplo n.º 6
0
        public void GetNextOccurrence_ThreeDaySchedule_MultipleScheduleIterations()
        {
            // 11:59AM on Mondays, Tuesdays, Wednesdays, Thursdays and Fridays
            CronSchedule.TryCreate("0 59 11 * * 1-5", out CronSchedule schedule);

            DateTime now = new DateTime(2015, 5, 23, 9, 0, 0);

            TimeSpan expectedTime = new TimeSpan(11, 59, 0);

            for (int i = 1; i <= 5; i++)
            {
                DateTime nextOccurrence = schedule.GetNextOccurrence(now);

                Assert.Equal((DayOfWeek)i, nextOccurrence.DayOfWeek);
                Assert.Equal(expectedTime, nextOccurrence.TimeOfDay);
                now = nextOccurrence + TimeSpan.FromSeconds(1);
            }
        }