private static string GenerateCronExpression(TimeSpan voteStartTimeSpan, DayOfWeek voteStartDayOfWeek)
        {
            string expression = CronGenerator.GenerateSetDayCronExpression(voteStartTimeSpan, voteStartDayOfWeek).Trim();

            // The expression produced is nearly correct but it's designed for Quartz not standard Cron and takes a nonstandard 7th field so
            // we have to remove that last field.
            return(expression.Substring(0, expression.LastIndexOf(' ')));
        }
예제 #2
0
        public IReadOnlyList <string> Create(Job job)
        {
            if (job.CronExpressionOverride.HasValue())
            {
                return(new [] { job.CronExpressionOverride });
            }

            if (job.RepeatInterval == RepeatInterval.Daily)
            {
                return(new [] { CronGenerator.GenerateDailyCronExpression(job.StartAt.TimeOfDay) });
            }

            if (job.RepeatInterval == RepeatInterval.Weekly)
            {
                return(new [] { CronGenerator.GenerateSetDayCronExpression(job.StartAt.TimeOfDay, job.StartAt.DayOfWeek) });
            }

            if (job.RepeatInterval == RepeatInterval.BiMonthly)
            {
                return(new []
                {
                    CronGenerator.GenerateSetDayMonthlyCronExpression(job.StartAt.TimeOfDay, TimeOfMonthToRun.First, job.StartAt.DayOfWeek, 1),
                    CronGenerator.GenerateSetDayMonthlyCronExpression(job.StartAt.TimeOfDay, TimeOfMonthToRun.Third, job.StartAt.DayOfWeek, 1)
                });
            }

            if (job.RepeatInterval == RepeatInterval.Monthly)
            {
                return(new [] { CronGenerator.GenerateSetDayMonthlyCronExpression(job.StartAt.TimeOfDay, TimeOfMonthToRun.First, job.StartAt.DayOfWeek, 1) });
            }

            if (job.RepeatInterval == RepeatInterval.Quarterly)
            {
                return(new [] { CronGenerator.GenerateSetDayMonthlyCronExpression(job.StartAt.TimeOfDay, TimeOfMonthToRun.First, job.StartAt.DayOfWeek, 3) });
            }

            return(null);
        }
        /// <summary>
        ///     method for calculate cronexpression
        /// </summary>
        /// <param name="typeEnum">ScheduleTypeEnum</param>
        /// <param name="time">DateTime</param>
        /// <param name="intervarl">int</param>
        /// <param name="monday">bool</param>
        /// <param name="tuesday">bool</param>
        /// <param name="wednesday">bool</param>
        /// <param name="thursday">bool</param>
        /// <param name="friday">bool</param>
        /// <param name="saturday">bool</param>
        /// <param name="sunday">bool</param>
        /// <returns>string</returns>
        public string GetCronExpression(ScheduleTypeEnum typeEnum, DateTime time, int intervarl, bool monday,
                                        bool tuesday, bool wednesday, bool thursday, bool friday, bool saturday, bool sunday)
        {
            var result  = string.Empty;
            var runTime = new TimeSpan(time.Ticks);
            var dayofTheMonthToRunOn = time.Day;

            switch (typeEnum)
            {
                #region RegularIntervals

            case ScheduleTypeEnum.RegularIntervals:
                return(CronGenerator.GenerateMinutesCronExpression(TimeSpan.FromSeconds(intervarl).Minutes));

                #endregion RegularIntervals

                #region Daily

            case ScheduleTypeEnum.Daily:
                var daysToRun = new List <DayOfWeek>();
                if (monday)
                {
                    daysToRun.Add(DayOfWeek.Monday);
                }
                if (tuesday)
                {
                    daysToRun.Add(DayOfWeek.Tuesday);
                }
                if (wednesday)
                {
                    daysToRun.Add(DayOfWeek.Wednesday);
                }
                if (thursday)
                {
                    daysToRun.Add(DayOfWeek.Thursday);
                }
                if (friday)
                {
                    daysToRun.Add(DayOfWeek.Friday);
                }
                if (saturday)
                {
                    daysToRun.Add(DayOfWeek.Saturday);
                }
                if (sunday)
                {
                    daysToRun.Add(DayOfWeek.Sunday);
                }
                return(CronGenerator.GenerateMultiDayCronExpression(runTime, daysToRun));

                #endregion Daily

                #region Weekly

            case ScheduleTypeEnum.Weekly:
                var dayOfWeek = time.DayOfWeek;
                return(CronGenerator.GenerateSetDayCronExpression(runTime, dayOfWeek));

                #endregion Weekly

                #region Monthly

            case ScheduleTypeEnum.Monthly:
                var monthsToRunOn = time.Month;
                return(CronGenerator.GenerateMonthlyCronExpression(runTime, dayofTheMonthToRunOn, monthsToRunOn));

                #endregion Monthly

                #region Yearly

            case ScheduleTypeEnum.Yearly:
                MonthOfYear monthToRunOn = (MonthOfYear)time.Month;
                return(CronGenerator.GenerateYearlyCronExpression(runTime, dayofTheMonthToRunOn, monthToRunOn));

                #endregion Yearly

            default:
                break;
            }

            return(result);
        }