Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        public CrontabService(IServiceProvider services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            var options = services.GetService <CrontabOptions>();

            if (options == null)
            {
                options = new CrontabOptions();
            }
            this.logger = services.GetService <ILogger>();
            var types = GetTypeInfos(options);
            var list  = new List <CrontabExecutor>();

            foreach (var type in types)
            {
                foreach (var method in type.DeclaredMethods)
                {
                    var attribute = method.GetCustomAttribute <CrontabScheduleAttribute>();
                    if (attribute != null)
                    {
                        if (string.IsNullOrEmpty(attribute.Name))
                        {
                            throw new CustomAttributeFormatException("Crontab name is empty");
                        }
                        var arr = attribute.Schedule.Split('|');
                        if (arr.Length == 0)
                        {
                            throw new CustomAttributeFormatException($"Crontab '{attribute.Name}' does not have any schedule");
                        }
                        var schedules = new CrontabSchedule[arr.Length];
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (CrontabSchedule.TryParse(arr[i], out CrontabSchedule schedule))
                            {
                                schedules[i] = schedule;
                            }
                            else
                            {
                                throw new CustomAttributeFormatException($"Crontab '{attribute.Name}' schedule '{arr[i]}' format error");
                            }
                        }
                        var name = attribute.Name;
                        if (string.IsNullOrEmpty(name))
                        {
                            name = method.Name;
                        }
                        var item = new CrontabExecutor(name, services, cts.Token, type, method, schedules, attribute.AllowConcurrentExecution, attribute.RunImmediately);
                        if (!executorDict.ContainsKey(item.Name))
                        {
                            executorDict.Add(item.Name, item);
                            if (attribute.AutoEnable)
                            {
                                item.Enable();
                            }
                        }
                        else
                        {
                            throw new CustomAttributeFormatException($"Crontab '{item.Name}' name is duplicate");
                        }
                    }
                }
            }

            var dt = DateTime.Now;

            dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
            var next = dt.AddMinutes(1);
            var due  = Convert.ToInt32(Math.Ceiling((next - DateTime.Now).TotalMilliseconds));

            Task.Factory.StartNew(async() => {
                try {
                    await Task.Delay(due, cts.Token);
                    Execute();
                }
                catch (Exception ex) {
                    if (logger != null)
                    {
                        logger.LogError(ex, "Execute error");
                    }
                }
            }, cts.Token);
        }
Example #2
0
        /// <summary>
        /// Try to parse the value for the schedule
        /// </summary>
        /// <param name="value"></param>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public static bool TryParse(string value, out CrontabSchedule schedule)
        {
            schedule = null;
            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }
            string[] array = value.Split(Separator, StringSplitOptions.RemoveEmptyEntries);
            if (array.Length == 0)
            {
                return(false);
            }
            var v1 = array[0];

            if (CrontabValueMinute.TryParse(v1, out CrontabValueMinute minute))
            {
                CrontabValueHour  hour  = null;
                CrontabValueDay   day   = null;
                CrontabValueMonth month = null;
                CrontabValueWeek  week  = null;
                if (array.Length >= 2)
                {
                    if (!CrontabValueHour.TryParse(array[1], out hour))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 3)
                {
                    if (!CrontabValueDay.TryParse(array[2], out day))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 4)
                {
                    if (!CrontabValueMonth.TryParse(array[3], out month))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 5)
                {
                    if (!CrontabValueWeek.TryParse(array[4], out week))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 6)
                {
                    return(false);
                }
                var timeGroup = new CrontabValueHourMinuteGroup(minute, hour);
                var dateGroup = new CrontabValueMonthDayGroup(day, month);
                schedule = new CrontabSchedule(value, timeGroup, dateGroup, week);
                return(true);
            }
            else if (CrontabValueTimeRange.TryParse(v1, out CrontabValueTimeRange timeRange))
            {
                CrontabValueDay   day   = null;
                CrontabValueMonth month = null;
                CrontabValueWeek  week  = null;
                if (array.Length >= 2)
                {
                    if (!CrontabValueDay.TryParse(array[1], out day))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 3)
                {
                    if (!CrontabValueMonth.TryParse(array[2], out month))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 4)
                {
                    if (!CrontabValueWeek.TryParse(array[3], out week))
                    {
                        return(false);
                    }
                }
                if (array.Length >= 5)
                {
                    return(false);
                }
                var timeGroup = new CrontabValueTimeRangeGroup(timeRange);
                var dateGroup = new CrontabValueMonthDayGroup(day, month);
                schedule = new CrontabSchedule(value, timeGroup, dateGroup, week);
                return(true);
            }
            else
            {
                return(false);
            }
        }