/// <summary>
 /// Constructs a new instance based on the specified crontab expression
 /// </summary>
 /// <param name="cronTabExpression">The crontab expression defining the schedule</param>
 public CronSchedule(string cronTabExpression)
 {
     CrontabSchedule.ParseOptions options = new CrontabSchedule.ParseOptions()
     {
         IncludingSeconds = true
     };
     _cronSchedule = CrontabSchedule.Parse(cronTabExpression, options);
 }
            public override Collection <Attribute> GetAttributes()
            {
                Collection <Attribute> attributes = new Collection <Attribute>();

                string schedule     = Context.GetMetadataValue <string>("schedule");
                bool   runOnStartup = Context.GetMetadataValue <bool>("runOnStartup");
                bool   useMonitor   = Context.GetMetadataValue <bool>("useMonitor", true);

                if (_environment.IsWindowsConsumption())
                {
                    // pre-resolve app setting specifiers
                    schedule = _nameResolver.ResolveWholeString(schedule);

                    var options = new CrontabSchedule.ParseOptions()
                    {
                        IncludingSeconds = true
                    };
                    if (CrontabSchedule.TryParse(schedule, options) == null)
                    {
                        throw new ArgumentException(string.Format("'{0}' is not a valid CRON expression.", schedule));
                    }
                }

                attributes.Add(new TimerTriggerAttribute(schedule)
                {
                    RunOnStartup = runOnStartup,
                    UseMonitor   = useMonitor
                });

                return(attributes);
            }
Example #3
0
            public override Collection <Attribute> GetAttributes()
            {
                Collection <Attribute> attributes = new Collection <Attribute>();

                string schedule     = Context.GetMetadataValue <string>("schedule");
                bool   runOnStartup = Context.GetMetadataValue <bool>("runOnStartup");

                if (Utility.IsDynamic)
                {
                    // pre-resolve app setting specifiers
                    var resolver = new DefaultNameResolver();
                    schedule = resolver.ResolveWholeString(schedule);

                    var options = new CrontabSchedule.ParseOptions()
                    {
                        IncludingSeconds = true
                    };
                    if (CrontabSchedule.TryParse(schedule, options) == null)
                    {
                        throw new ArgumentException(string.Format("'{0}' is not a valid CRON expression.", schedule));
                    }
                }

                attributes.Add(new TimerTriggerAttribute(schedule)
                {
                    RunOnStartup = runOnStartup
                });

                return(attributes);
            }
Example #4
0
        public static CrontabSchedule ParseCron(ILogger logger, AixScheduleTaskInfo task)
        {
            string          cron   = task.Cron;
            CrontabSchedule result = null;

            try
            {
                if (string.IsNullOrEmpty(cron))
                {
                    throw new Exception($"Aix.ScheduleTask任务{task.Id},{task.TaskName}表达式配置为空");
                }
                if (CrontabScheduleCache.TryGetValue(cron, out result))
                {
                    return(result);
                }
                var options = new CrontabSchedule.ParseOptions
                {
                    IncludingSeconds = cron.Split(' ').Length > 5,
                };
                result = CrontabSchedule.Parse(cron, options);
                CrontabScheduleCache.TryAdd(cron, result);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Aix.ScheduleTask定时任务解析出错 {task.Id},{task.TaskName},{task.Cron}");
            }
            return(result);
        }
        static CronCallHandler CronCall(ParseOptions options)
        {
            return((startTimeString, cronExpression, nextTimeString, expectException) =>
            {
                var start = Time(startTimeString);

                try
                {
                    var schedule = CrontabSchedule.Parse(cronExpression, options);

                    if (expectException)
                    {
                        Assert.Fail("The expression <{0}> cannot be valid.", cronExpression);
                    }

                    var next = schedule.GetNextOccurrence(start);

                    Assert.AreEqual(nextTimeString, TimeString(next),
                                    "Occurrence of <{0}> after <{1}>.", cronExpression, startTimeString);
                }
                catch (CrontabException e)
                {
                    if (!expectException)
                    {
                        Assert.Fail("Unexpected ParseException while parsing <{0}>: {1}", cronExpression, e.ToString());
                    }
                }
            });
        }
Example #6
0
 /// <summary>
 /// Constructs a new instance based on the specified crontab expression
 /// </summary>
 /// <param name="cronTabExpression">The crontab expression defining the schedule</param>
 public CronSchedule(string cronTabExpression)
 {
     CrontabSchedule.ParseOptions options = new CrontabSchedule.ParseOptions()
     {
         IncludingSeconds = true
     };
     _cronSchedule = CrontabSchedule.Parse(cronTabExpression, options);
 }
        public void Formatting(string format, string expression, bool includingSeconds)
        {
            var options = new ParseOptions {
                IncludingSeconds = includingSeconds
            };

            Assert.AreEqual(format, CrontabSchedule.Parse(expression, options).ToString());
        }
        private static CrontabSchedule.ParseOptions CreateParseOptions(string cronExpression)
        {
            var options = new CrontabSchedule.ParseOptions()
            {
                IncludingSeconds = HasSeconds(cronExpression)
            };

            return(options);
        }
        public ScheduledBackgroundService(ILogger logger, string cronExpression)
        {
            _logger = logger;
            var includingSeconds = new CrontabSchedule.ParseOptions {
                IncludingSeconds = true
            };

            _crontabSchedule = CrontabSchedule.Parse(cronExpression, includingSeconds);

            _logger.LogDebug($"cronExpression: [{cronExpression}]");
        }
Example #10
0
        public CrontabSchedule GetCrontab(string value)
        {
            bool includingSeconds = value?.Count(char.IsWhiteSpace) == 5;
            var  parseOptions     = new CrontabSchedule.ParseOptions
            {
                IncludingSeconds = includingSeconds,
            };
            CrontabSchedule crontab = CrontabSchedule.TryParse(value
                                                               , parseOptions);

            return(crontab);
        }
        static CronFiniteHandler CronFinite(ParseOptions options)
        {
            return((cronExpression, startTimeString, endTimeString) =>
            {
                var schedule = CrontabSchedule.Parse(cronExpression, options);
                var occurrence = schedule.GetNextOccurrence(Time(startTimeString), Time(endTimeString));

                Assert.AreEqual(endTimeString, TimeString(occurrence),
                                "Occurrence of <{0}> after <{1}> did not terminate with <{2}>.",
                                cronExpression, startTimeString, endTimeString);
            });
        }
Example #12
0
 public void Formatting()
 {
     Assert.AreEqual("* 1-3 * * *", CrontabSchedule.Parse("* 1-2,3 * * *").ToString());
     Assert.AreEqual("* * * 1,3,5,7,9,11 *", CrontabSchedule.Parse("* * * */2 *").ToString());
     Assert.AreEqual("10,25,40 * * * *", CrontabSchedule.Parse("10-40/15 * * * *").ToString());
     Assert.AreEqual("* * * 1,3,8 1-2,5", CrontabSchedule.Parse("* * * Mar,Jan,Aug Fri,Mon-Tue").ToString());
     var includingSeconds = new ParseOptions { IncludingSeconds = true };
     Assert.AreEqual("1 * 1-3 * * *", CrontabSchedule.Parse("1 * 1-2,3 * * *", includingSeconds).ToString());
     Assert.AreEqual("22 * * * 1,3,5,7,9,11 *", CrontabSchedule.Parse("22 * * * */2 *", includingSeconds).ToString());
     Assert.AreEqual("33 10,25,40 * * * *", CrontabSchedule.Parse("33 10-40/15 * * * *", includingSeconds).ToString());
     Assert.AreEqual("55 * * * 1,3,8 1-2,5", CrontabSchedule.Parse("55 * * * Mar,Jan,Aug Fri,Mon-Tue", includingSeconds).ToString());
 }
Example #13
0
        public InspectorService(IServiceScopeFactory scopeFactory)
        {
            _scopeFactory = scopeFactory;

            var parseOptions = new CrontabSchedule.ParseOptions()
            {
                IncludingSeconds = true
            };

            _schedule = CrontabSchedule.Parse(_scheduleExpression, parseOptions);
            _nextRun  = _schedule.GetNextOccurrence(DateTime.Now);
        }
Example #14
0
        static int Main(string[] args)
        {
            var verbose = false;

            try
            {
                var argList      = new List <string>(args);
                var verboseIndex = argList.IndexOf("--verbose");
                // ReSharper disable once AssignmentInConditionalExpression
                if (verbose = verboseIndex >= 0)
                {
                    argList.RemoveAt(verboseIndex);
                }

                if (argList.Count < 3)
                {
                    throw new ApplicationException("Missing required arguments. You must at least supply CRONTAB-EXPRESSION START-DATE END-DATE.");
                }

                var expression = argList[0].Trim();
                var options    = new CrontabSchedule.ParseOptions
                {
                    IncludingSeconds = expression.Split(' ').Length > 5,
                };

                var start  = ParseDateArgument(argList[1], "start");
                var end    = ParseDateArgument(argList[2], "end");
                var format =
                    argList.Count > 3 ? argList[3]
                    : options.IncludingSeconds ? "ddd, dd MMM yyyy HH:mm:ss"
                    : "ddd, dd MMM yyyy HH:mm";

                var schedule = CrontabSchedule.Parse(expression, options);

                foreach (var occurrence in schedule.GetNextOccurrences(start, end))
                {
                    Console.Out.WriteLine(occurrence.ToString(format));
                }

                return(0);
            }
            catch (Exception e)
            {
                var error =
                    verbose
                    ? e.ToString()
                    : e is ApplicationException
                    ? e.Message : e.GetBaseException().Message;
                Console.Error.WriteLine(error);
                return(1);
            }
        }
        private static Scheduler BuildScheduler(string cronExpression, bool includingSeconds)
        {
            var options = new CrontabSchedule.ParseOptions();

            if (includingSeconds)
            {
                options.IncludingSeconds = true;
            }

            var schedule = CrontabSchedule.Parse(cronExpression, options);

            return(new Scheduler(schedule));
        }
Example #16
0
        public InvokationMethodTarget(string cron, Type type, MethodInfo info,
                                      object instanse = null, bool enabled = true)
        {
            Type         = type;
            this.cron    = cron;
            MethodInfo   = info;
            Instanse     = instanse;
            this.enabled = enabled;

            options = new CrontabSchedule.ParseOptions {
                IncludingSeconds = true
            };
            CreateSheduler();
        }
 internal static bool TryCreate(string cronExpression, out CronSchedule cronSchedule)
 {
     cronSchedule = null;
     CrontabSchedule.ParseOptions options = new CrontabSchedule.ParseOptions()
     {
         IncludingSeconds = true
     };
     CrontabSchedule crontabSchedule = CrontabSchedule.TryParse(cronExpression, options);
     if (crontabSchedule != null)
     {
         cronSchedule = new CronSchedule(crontabSchedule);
         return true;
     }
     return false;
 }
        public void Formatting()
        {
            Assert.AreEqual("* 1-3 * * *", CrontabSchedule.Parse("* 1-2,3 * * *").ToString());
            Assert.AreEqual("* * * 1,3,5,7,9,11 *", CrontabSchedule.Parse("* * * */2 *").ToString());
            Assert.AreEqual("10,25,40 * * * *", CrontabSchedule.Parse("10-40/15 * * * *").ToString());
            Assert.AreEqual("* * * 1,3,8 1-2,5", CrontabSchedule.Parse("* * * Mar,Jan,Aug Fri,Mon-Tue").ToString());
            var includingSeconds = new ParseOptions {
                IncludingSeconds = true
            };

            Assert.AreEqual("1 * 1-3 * * *", CrontabSchedule.Parse("1 * 1-2,3 * * *", includingSeconds).ToString());
            Assert.AreEqual("22 * * * 1,3,5,7,9,11 *", CrontabSchedule.Parse("22 * * * */2 *", includingSeconds).ToString());
            Assert.AreEqual("33 10,25,40 * * * *", CrontabSchedule.Parse("33 10-40/15 * * * *", includingSeconds).ToString());
            Assert.AreEqual("55 * * * 1,3,8 1-2,5", CrontabSchedule.Parse("55 * * * Mar,Jan,Aug Fri,Mon-Tue", includingSeconds).ToString());
        }
Example #19
0
        internal static bool TryCreate(string cronExpression, out CronSchedule cronSchedule)
        {
            cronSchedule = null;
            CrontabSchedule.ParseOptions options = new CrontabSchedule.ParseOptions()
            {
                IncludingSeconds = true
            };
            CrontabSchedule crontabSchedule = CrontabSchedule.TryParse(cronExpression, options);

            if (crontabSchedule != null)
            {
                cronSchedule = new CronSchedule(crontabSchedule);
                return(true);
            }
            return(false);
        }
        public CrontabSchedule ParseCron(string cron)
        {
            CrontabSchedule result;

            if (CrontabScheduleCache.TryGetValue(cron, out result))
            {
                return(result);
            }
            var options = new CrontabSchedule.ParseOptions
            {
                IncludingSeconds = cron.Split(' ').Length > 5,
            };

            result = CrontabSchedule.Parse(cron, options);
            CrontabScheduleCache.TryAdd(cron, result);
            return(result);
        }
Example #21
0
        public CronService(ILogger <CronService> logger, IEnumerable <ICronTask> scheduledTasks)
        {
            _logger = logger;

            var referenceTime = DateTime.UtcNow;

            foreach (var scheduledTask in scheduledTasks)
            {
                var opts = new CrontabSchedule.ParseOptions()
                {
                    IncludingSeconds = scheduledTask.IncludeSeconds
                };

                _scheduledTasks.Add(new CronTaskWrapper
                {
                    Schedule    = CrontabSchedule.Parse(scheduledTask.Schedule, opts),
                    Task        = scheduledTask,
                    NextRunTime = referenceTime
                });
            }
        }
Example #22
0
            public override Collection <Attribute> GetAttributes()
            {
                Collection <Attribute> attributes = new Collection <Attribute>();

                string schedule     = Context.GetMetadataValue <string>("schedule");
                bool   runOnStartup = Context.GetMetadataValue <bool>("runOnStartup");

                CrontabSchedule.ParseOptions options = new CrontabSchedule.ParseOptions()
                {
                    IncludingSeconds = true
                };
                if (CrontabSchedule.TryParse(schedule, options) == null)
                {
                    throw new ArgumentException(string.Format("'{0}' is not a valid CRON expression.", schedule));
                }

                attributes.Add(new TimerTriggerAttribute(schedule)
                {
                    RunOnStartup = runOnStartup
                });

                return(attributes);
            }
        static void CronCall(string startTimeString, string cronExpression, string nextTimeString, ParseOptions options)
        {
            var schedule = CrontabSchedule.Parse(cronExpression, options);
            var next     = schedule.GetNextOccurrence(Time(startTimeString));

            Assert.AreEqual(nextTimeString, TimeString(next),
                            "Occurrence of <{0}> after <{1}>.", cronExpression, startTimeString);
        }
Example #24
0
        static CronFiniteHandler CronFinite(ParseOptions options)
        {
            return (cronExpression, startTimeString, endTimeString) =>
            {
                var schedule = CrontabSchedule.Parse(cronExpression, options);
                var occurrence = schedule.GetNextOccurrence(Time(startTimeString), Time(endTimeString));

                Assert.AreEqual(endTimeString, TimeString(occurrence),
                    "Occurrence of <{0}> after <{1}> did not terminate with <{2}>.",
                    cronExpression, startTimeString, endTimeString);
            };
        }
Example #25
0
        static CronCallHandler CronCall(ParseOptions options)
        {
            return (startTimeString, cronExpression, nextTimeString, expectException) =>
            {
                var start = Time(startTimeString);

                try
                {
                    var schedule = CrontabSchedule.Parse(cronExpression, options);

                    if (expectException)
                        Assert.Fail("The expression <{0}> cannot be valid.", cronExpression);

                    var next = schedule.GetNextOccurrence(start);

                    Assert.AreEqual(nextTimeString, TimeString(next),
                        "Occurrence of <{0}> after <{1}>.", cronExpression, startTimeString);
                }
                catch (CrontabException e)
                {
                    if (!expectException)
                        Assert.Fail("Unexpected ParseException while parsing <{0}>: {1}", cronExpression, e.ToString());
                }
            };
        }
Example #26
0
        static void CronCall(string startTimeString, string cronExpression, string nextTimeString, ParseOptions options)
        {
            var schedule = CrontabSchedule.Parse(cronExpression, options);
            var next = schedule.GetNextOccurrence(Time(startTimeString));

            Assert.AreEqual(nextTimeString, TimeString(next),
                "Occurrence of <{0}> after <{1}>.", cronExpression, startTimeString);
        }
Example #27
0
 public void Formatting(string format, string expression, bool includingSeconds)
 {
     var options = new ParseOptions { IncludingSeconds = includingSeconds };
     Assert.AreEqual(format, CrontabSchedule.Parse(expression, options).ToString());
 }