Ejemplo n.º 1
0
        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(' ')));
        }
Ejemplo n.º 2
0
        public void ConvertInterval(
            [Operand(Name = "interval",
                     Description = "The value of the interval")]
            int interval,
            [Operand(Name = "unit",
                     Description = "The unit in seconds, minutes or hours")]
            Units unit,
            [Operand(Name = "command", Description = "The optional command to add to the end of the cron expression")]
            string command = "",
            [Option(LongName = "seconds", ShortName = "s", Description = "Optional to use seconds. Defaults without.")]
            bool seconds = false)
        {
            if (!seconds && isSeconds(unit))
            {
                Console.WriteLine("You must use the -s or --seconds flag to enable second precision intervals.");
                return;
            }
            var icg        = new CronGenerator();
            var results    = icg.ConvertInterval(interval, unit);
            int index      = 0;
            var headerLine = (string.IsNullOrWhiteSpace(command))
                ? "# Cron Expressions"
                : $"# Cron Expressions for {command}";

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine($"# ----------------------------------------------------------------");
            Console.WriteLine(headerLine);
            Console.WriteLine($"# To be executed on {interval} {unit.ToString()} intervals");
            Console.WriteLine($"# ----------------------------------------------------------------");
            Console.ResetColor();
            foreach (var result in results)
            {
                WriteLine(result, index, command, seconds);
                index++;
            }
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine($"# End {interval} {unit.ToString()}");
            Console.WriteLine($"# ----------------------------------------------------------------");
            Console.ResetColor();
        }
Ejemplo n.º 3
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);
        }