コード例 #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;
            }
        }
コード例 #2
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 crontab expression
        /// <a href="http://en.wikipedia.org/wiki/Cron#CRON_expression"/> or a <see cref="TimeSpan"/> string.</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;
            }
        }
コード例 #3
0
        internal static TimerSchedule Create(TimerTriggerAttribute attribute, INameResolver nameResolver)
        {
            TimerSchedule schedule = null;

            if (!string.IsNullOrEmpty(attribute.ScheduleExpression))
            {
                string resolvedExpression = nameResolver.ResolveWholeString(attribute.ScheduleExpression);

                CronSchedule cronSchedule = null;
                if (CronSchedule.TryCreate(resolvedExpression, 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
                        attribute.UseMonitor = false;
                    }
                    else if (!attribute.UseMonitor.HasValue)
                    {
                        // if the user hasn't specified a value
                        // set to true
                        attribute.UseMonitor = true;
                    }
                }
                else
                {
                    TimeSpan periodTimespan = TimeSpan.Parse(resolvedExpression);
                    schedule = new ConstantSchedule(periodTimespan);

                    if (periodTimespan.TotalMinutes < 1)
                    {
                        // for very frequent constant schedules, we want to disable persistence
                        attribute.UseMonitor = false;
                    }
                    else if (!attribute.UseMonitor.HasValue)
                    {
                        // if the user hasn't specified a value
                        // set to true
                        attribute.UseMonitor = true;
                    }
                }
            }
            else
            {
                schedule = (TimerSchedule)Activator.CreateInstance(attribute.ScheduleType);
                if (!attribute.UseMonitor.HasValue)
                {
                    // if the user hasn't specified a value
                    // set to true
                    attribute.UseMonitor = true;
                }
            }

            return(schedule);
        }
コード例 #4
0
        public void GetNextOccurrence_ReturnsExpected()
        {
            ConstantSchedule schedule = new ConstantSchedule(TimeSpan.FromHours(1));

            DateTime now = DateTime.Now;

            for (int i = 0; i < 10; i++)
            {
                DateTime nextOccurrence = schedule.GetNextOccurrence(now);
                Assert.Equal(new TimeSpan(1, 0, 0), nextOccurrence - now);

                now = nextOccurrence;
            }
        }
コード例 #5
0
        internal static TimerSchedule Create(TimerTriggerAttribute attribute, INameResolver nameResolver)
        {
            TimerSchedule schedule = null;

            if (!string.IsNullOrEmpty(attribute.ScheduleExpression))
            {
                string resolvedExpression = nameResolver.ResolveWholeString(attribute.ScheduleExpression);

                CronSchedule cronSchedule = null;
                TimeSpan     periodTimespan;
                if (CronSchedule.TryCreate(resolvedExpression, 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
                        attribute.UseMonitor = false;
                    }
                }
                else if (TimeSpan.TryParse(resolvedExpression, out periodTimespan))
                {
                    schedule = new ConstantSchedule(periodTimespan);

                    if (periodTimespan.TotalMinutes < 1)
                    {
                        // for very frequent constant schedules, we want to disable persistence
                        attribute.UseMonitor = false;
                    }
                }
                else
                {
                    throw new ArgumentException("The schedule expression was not recognized as a valid cron expression or timespan string.");
                }
            }
            else
            {
                schedule = (TimerSchedule)Activator.CreateInstance(attribute.ScheduleType);
            }

            return(schedule);
        }
コード例 #6
0
        public void SetNextInterval_OverridesNextInterval()
        {
            ConstantSchedule schedule = new ConstantSchedule(TimeSpan.FromSeconds(30));

            DateTime now = DateTime.Now;
            DateTime nextOccurrence = schedule.GetNextOccurrence(now);
            Assert.Equal(new TimeSpan(0, 0, 30), nextOccurrence - now);
            now = nextOccurrence;
            
            // next interval is overidden
            schedule.SetNextInterval(new TimeSpan(1, 0, 0));
            nextOccurrence = schedule.GetNextOccurrence(now);
            Assert.Equal(new TimeSpan(1, 0, 0), nextOccurrence - now);
            now = nextOccurrence;

            // subsequent intervals are not
            nextOccurrence = schedule.GetNextOccurrence(now);
            Assert.Equal(new TimeSpan(0, 0, 30), nextOccurrence - now);
            now = nextOccurrence;
        }
コード例 #7
0
        internal static TimerSchedule Create(TimerTriggerAttribute attribute, INameResolver nameResolver, ILogger logger)
        {
            TimerSchedule schedule = null;

            if (!string.IsNullOrEmpty(attribute.ScheduleExpression))
            {
                string resolvedExpression = nameResolver.ResolveWholeString(attribute.ScheduleExpression);
                if (CronSchedule.TryCreate(resolvedExpression, out CronSchedule cronSchedule))
                {
                    schedule = cronSchedule;
                    if (attribute.UseMonitor && ShouldDisableScheduleMonitor(cronSchedule, DateTime.Now))
                    {
                        logger.LogDebug("UseMonitor changed to false based on schedule frequency.");
                        attribute.UseMonitor = false;
                    }
                }
                else if (TimeSpan.TryParse(resolvedExpression, out TimeSpan periodTimespan))
                {
                    schedule = new ConstantSchedule(periodTimespan);

                    if (attribute.UseMonitor && periodTimespan.TotalMinutes < 1)
                    {
                        // for very frequent constant schedules, we want to disable persistence
                        logger.LogDebug("UseMonitor changed to false based on schedule frequency.");
                        attribute.UseMonitor = false;
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("The schedule expression '{0}' was not recognized as a valid cron expression or timespan string.", resolvedExpression));
                }
            }
            else
            {
                schedule = (TimerSchedule)Activator.CreateInstance(attribute.ScheduleType);
            }

            return(schedule);
        }
コード例 #8
0
        public void ToString_ReturnsCorrectValue()
        {
            ConstantSchedule schedule = new ConstantSchedule(TimeSpan.FromSeconds(30));

            Assert.Equal("Constant: 00:00:30", schedule.ToString());
        }