allbet gmaing代理:Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解

admin 3个月前 (06-25) 科技 36 0

1.先容

中文意义就是逐日时间距离设计天生

2.API解说

(1)WithInterval、WithIntervalInHours、WithIntervalInMinutes、WithIntervalInSeconds

WithInterval:指定要天生触发器的时间单元和距离。

WithIntervalInHours:指定要天生触发器的距离按小时来

WithIntervalInMinutes:指定要天生触发器的距离按分钟来

WithIntervalInSeconds:指定要天生触发器的距离按秒来

和前面的SimpleSceduleBuilder、CalendarIntervalScheduleBuilder一样的

 

(2)OnDaysOfTheWeek、OnMondayThroughFriday、OnSaturdayAndSunday、OnEveryDay

 

OnDaysOfTheWeek:设置触发器一周中的哪几天

OnMondayThroughFriday:从星期一到星期五

OnSaturdayAndSunday:周六和周日

OnEveryDay:天天

天天10:00到23:10.00的每一分钟执行一次

  var trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                  w => w.OnEveryDay() //设置天天
                      .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(10, 00)) //设置天天最先于几点
                      .EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(23, 10, 0)) //设置逐日竣事于几点
                      .WithIntervalInMinutes(1)//距离分钟
                ).Build();

一周当中的星期二和星期三每秒执行一次

            List<DayOfWeek> dayOfWeeks = new List<DayOfWeek>();

            dayOfWeeks.Add(DayOfWeek.Wednesday);

            dayOfWeeks.Add(DayOfWeek.Tuesday);

            trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(
                 w=>w.OnDaysOfTheWeek(dayOfWeeks)//.OnDaysOfTheWeek(new DayOfWeek[2] { DayOfWeek.Wednesday,DayOfWeek.Tuesday})
                     .WithIntervalInSeconds(1)
                ).Build();

 

 

 

 源码实现

  /// <summary>
        /// Set the trigger to fire on the given days of the week.
        /// </summary>
        /// <param name="onDaysOfWeek">a Set containing the integers representing the days of the week, defined by <see cref="DayOfWeek.Sunday"/> - <see cref="DayOfWeek.Saturday"/>.
        /// </param>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(IReadOnlyCollection<DayOfWeek> onDaysOfWeek)
        {
            if (onDaysOfWeek == null || onDaysOfWeek.Count == 0)
            {
                throw new ArgumentException("Days of week must be an non-empty set.");
            }

            foreach (DayOfWeek day in onDaysOfWeek)
            {
                if (!AllDaysOfTheWeek.Contains(day))
                {
                    throw new ArgumentException("Invalid value for day of week: " + day);
                }
            }

            daysOfWeek = new HashSet<DayOfWeek>(onDaysOfWeek);
            return this;
        }

        /// <summary>
        /// Set the trigger to fire on the given days of the week.
        /// </summary>
        /// <param name="onDaysOfWeek">a variable length list of week days representing the days of the week</param>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder OnDaysOfTheWeek(params DayOfWeek[] onDaysOfWeek)
        {
            return OnDaysOfTheWeek((IReadOnlyCollection<DayOfWeek>) onDaysOfWeek);
        }

        /// <summary>
        /// Set the trigger to fire on the days from Monday through Friday.
        /// </summary>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder OnMondayThroughFriday()
        {
            daysOfWeek = new HashSet<DayOfWeek>(MondayThroughFriday);
            return this;
        }

        /// <summary>
        /// Set the trigger to fire on the days Saturday and Sunday.
        /// </summary>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder OnSaturdayAndSunday()
        {
            daysOfWeek = new HashSet<DayOfWeek>(SaturdayAndSunday);
            return this;
        }

        /// <summary>
        /// Set the trigger to fire on all days of the week.
        /// </summary>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder OnEveryDay()
        {
            daysOfWeek = new HashSet<DayOfWeek>(AllDaysOfTheWeek);
            return this;
        }

(3)StartingDailyAt、EndingDailyAt

StartingDailyAt:最先时间于

EndingDailyAt:竣事时间于

源码实现

        /// <summary>
        /// The TimeOfDay for this trigger to start firing each day.
        /// </summary>
        /// <param name="timeOfDayUtc"></param>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder StartingDailyAt(TimeOfDay timeOfDayUtc)
        {
            startTimeOfDayUtc = timeOfDayUtc ?? throw new ArgumentException("Start time of day cannot be null!");
            return this;
        }

        /// <summary>
        /// The TimeOfDay for this trigger to end firing each day.
        /// </summary>
        /// <param name="timeOfDayUtc"></param>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder EndingDailyAt(TimeOfDay timeOfDayUtc)
        {
            endTimeOfDayUtc = timeOfDayUtc;
            return this;
        }

(4)EndingDailyAfterCount

EndingDailyAfterCount:使用count、interval和StarTimeOfDay盘算并设置EndTimeOfDay。

源码实现

       /// <summary>
        /// Calculate and set the EndTimeOfDay using count, interval and StarTimeOfDay. This means
        /// that these must be set before this method is call.
        /// </summary>
        /// <param name="count"></param>
        /// <returns>the updated DailyTimeIntervalScheduleBuilder</returns>
        public DailyTimeIntervalScheduleBuilder EndingDailyAfterCount(int count)
        {
            if (count <= 0)
            {
                throw new ArgumentException("Ending daily after count must be a positive number!");
            }

            if (startTimeOfDayUtc == null)
            {
                throw new ArgumentException("You must set the StartDailyAt() before calling this EndingDailyAfterCount()!");
            }

            DateTimeOffset today = SystemTime.UtcNow();
            DateTimeOffset startTimeOfDayDate = startTimeOfDayUtc.GetTimeOfDayForDate(today).Value;
            DateTimeOffset maxEndTimeOfDayDate = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59).GetTimeOfDayForDate(today).Value;

            //apply proper offsets according to timezone
            TimeZoneInfo targetTimeZone = timeZone ?? TimeZoneInfo.Local;
            startTimeOfDayDate = new DateTimeOffset(startTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(startTimeOfDayDate.DateTime, targetTimeZone));
            maxEndTimeOfDayDate = new DateTimeOffset(maxEndTimeOfDayDate.DateTime, TimeZoneUtil.GetUtcOffset(maxEndTimeOfDayDate.DateTime, targetTimeZone));

            TimeSpan remainingMillisInDay = maxEndTimeOfDayDate - startTimeOfDayDate;
            TimeSpan intervalInMillis;
            if (intervalUnit == IntervalUnit.Second)
            {
                intervalInMillis = TimeSpan.FromSeconds(interval);
            }
            else if (intervalUnit == IntervalUnit.Minute)
            {
                intervalInMillis = TimeSpan.FromMinutes(interval);
            }
            else if (intervalUnit == IntervalUnit.Hour)
            {
                intervalInMillis = TimeSpan.FromHours(interval);
            }
            else
            {
                throw new ArgumentException("The IntervalUnit: " + intervalUnit + " is invalid for this trigger.");
            }

            if (remainingMillisInDay < intervalInMillis)
            {
                throw new ArgumentException("The startTimeOfDay is too late with given Interval and IntervalUnit values.");
            }

            long maxNumOfCount = remainingMillisInDay.Ticks / intervalInMillis.Ticks;
            if (count > maxNumOfCount)
            {
                throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
            }

            TimeSpan incrementInMillis = TimeSpan.FromTicks((count - 1) * intervalInMillis.Ticks);
            DateTimeOffset endTimeOfDayDate = startTimeOfDayDate.Add(incrementInMillis);

            if (endTimeOfDayDate > maxEndTimeOfDayDate)
            {
                throw new ArgumentException("The given count " + count + " is too large! The max you can set is " + maxNumOfCount);
            }

            DateTime cal = SystemTime.UtcNow().Date;
            cal = cal.Add(endTimeOfDayDate.TimeOfDay);
            endTimeOfDayUtc = TimeOfDay.HourMinuteAndSecondOfDay(cal.Hour, cal.Minute, cal.Second);
            return this;
        }

 

,

www.allbetgaming.net

欢迎进入欧博平台网站(www.aLLbetgame.us),www.aLLbetgame.us开放欧博平台网址、欧博注册、欧博APP下载、欧博客户端下载、欧博游戏等业务。

Allbet声明:该文看法仅代表作者自己,与本平台无关。转载请注明:allbet gmaing代理:Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解

网友评论

  • (*)

最新评论

文章归档

站点信息

  • 文章总数:762
  • 页面总数:0
  • 分类总数:8
  • 标签总数:1359
  • 评论总数:351
  • 浏览总数:33055