/// <summary> /// Add the task to the configuration /// </summary> /// <exception cref="System.InvalidOperationException"> /// Missing the attributes for the Monthly task /// or /// Recursion is not set /// </exception> public override void Add() { IRecur recur = null; switch (_recursion) { case Recursion.Daily: recur = new Daily(_repeat); break; case Recursion.Weekly: recur = new Weekly(_repeat, _weekdays); break; case Recursion.Monthly: if (_weeks != Tasks.Weeks.None && _weekdays != Tasks.Weekdays.None && _months != Tasks.Months.None) { recur = new MonthlyByWeekdays(_months, _weekdays, _weeks); } else if (_days > 1 && _months != Tasks.Months.None) { recur = new MonthlyByDay(_months, _days); } else { throw new InvalidOperationException($"Missing the attributes for the Monthly task"); } break; default: throw new InvalidOperationException("Recursion is not set"); } Config.InternalRunners.Add(new ScheduledTaskRunner(Name, TaskType, Parameters, recur, _startDate, _taskTime, _timeZone, TraceType)); }
/// <summary> /// Initializes a new instance of the <see cref="ScheduledTaskRunner" /> class. /// </summary> /// <param name="name">The name.</param> /// <param name="taskType">Type of the task.</param> /// <param name="parameters">The parameters.</param> /// <param name="recur">The recur.</param> /// <param name="startDate">The start date.</param> /// <param name="taskTime">The task time.</param> /// <param name="timeZoneInfo">The time zone information.</param> /// <param name="traceType">Type of the trace.</param> /// <exception cref="System.ArgumentOutOfRangeException">Task time should be between 00:00:00 and 23:59:59</exception> /// <exception cref="System.ArgumentNullException">startDate should be less than now</exception> public ScheduledTaskRunner(string name, Type taskType, Dictionary <string, object> parameters, IRecur recur, DateTime startDate, TimeSpan taskTime, TimeZoneInfo timeZoneInfo, LogType traceType) : base(name, taskType, parameters, traceType) { recur.NotNull(nameof(recur)); if (taskTime < Time.DayMinTime || taskTime > Time.DayMaxTime) { throw new ArgumentOutOfRangeException(nameof(taskTime), "Task time should be between 00:00:00 and 23:59:59"); } if (startDate > DateTime.Now) { throw new ArgumentNullException("Start Date should be less than now"); } ///If the start time is not configured set it to Sunday of this week _startDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek) : startDate; _recur = recur; _taskTime = taskTime; _timeZoneInfo = timeZoneInfo ?? TimeZoneInfo.Local; _lastDateTime = new DateTime(_startDate.Year, _startDate.Month, _startDate.Day, _taskTime.Hours, _taskTime.Minutes, _taskTime.Seconds, DateTimeKind.Unspecified); }
/// <summary> /// Initializes a new instance of the <see cref="ScheduledTaskRunner" /> class. /// </summary> /// <param name="name">The name.</param> /// <param name="taskType">Type of the task.</param> /// <param name="parameters">The parameters.</param> /// <param name="repeat">The repeat.</param> /// <param name="recursion">The recursion.</param> /// <param name="schedule">The schedule.</param> /// <param name="startDate">The start date.</param> /// <param name="taskTime">The task time.</param> /// <param name="timeZoneInfo">The time zone information.</param> /// <param name="traceType">Type of the trace.</param> /// <exception cref="System.ArgumentOutOfRangeException"> /// Repeat should be positive number for number of times to be repeated or 0 for infinite number of times /// or /// Task time should be between 00:00:00 and 23:59:59 /// or /// Start date should be less than now /// or /// Schedule does not have the weekdays defined for the Weekly tasks /// or /// Schedule does not have the weekdays defined for the Weekly tasks /// or /// Schedule does not conform to the format /// or /// Schedule does not have the months defined for the Monthly tasks /// or /// Schedule does not have the weeks defined for the Monthly tasks /// or /// Schedule does not have the weekdays defined for the Monthly tasks /// or /// Schedule does not have the day defined for the Monthly tasks in a valid range /// or /// Schedule does not conform to the format /// or /// Schedule does not conform to the format /// or /// Schedule does not conform to the format /// </exception> /// <exception cref="System.ArgumentNullException">Schedule cannot be null</exception> public ScheduledTaskRunner(string name, Type taskType, Dictionary <string, object> parameters, uint repeat, Recursion recursion, string schedule, DateTime startDate, TimeSpan taskTime, TimeZoneInfo timeZoneInfo, LogType traceType) : base(name, taskType, parameters, traceType) { if (repeat < 1) { throw new ArgumentOutOfRangeException(nameof(repeat), "Repeat should be positive number for number of times to be repeated or 0 for infinite number of times"); } if (taskTime < Time.DayMinTime || taskTime > Time.DayMaxTime) { throw new ArgumentOutOfRangeException(nameof(taskTime), "Task time should be between 00:00:00 and 23:59:59"); } if (schedule == null) { throw new ArgumentNullException(nameof(schedule), "Schedule cannot be null"); } if (startDate > DateTime.Now) { throw new ArgumentOutOfRangeException(nameof(startDate), "Start date should be less than now"); } _schedule = schedule.ToUpper(); _recursion = recursion; ///If the start time is not configured set it to Sunday of this week _startDate = startDate == DateTime.MinValue ? DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek) : startDate; _repeat = repeat; _taskTime = taskTime; _timeZoneInfo = timeZoneInfo ?? TimeZoneInfo.Local; switch (recursion) { case Recursion.Daily: _recur = new Daily(repeat); break; case Recursion.Weekly: //WD:Tuesday, Friday, Monday Weekdays weekdays = Weekdays.None; if (schedule.Length < 3 || schedule.Substring(0, 3) != "WD:") { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the weekdays defined for the Weekly tasks"); } if (!Enum.TryParse <Weekdays>(schedule.Substring(3), true, out weekdays) || weekdays == Weekdays.None) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the weekdays defined for the Weekly tasks"); } _recur = new Weekly(repeat, weekdays); break; case Recursion.Monthly: //"MN:January,March,December|WK:First,Last|WD:THursday,Friday" //"MN:January,March,December|DY:1,2,3,Last" Months months = Months.None; weekdays = Weekdays.None; Weeks week = Weeks.None; uint days = 0; foreach (var item in schedule.Split('|')) { if (item.Length < 4) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not conform to the format"); } switch (item.Substring(0, 3)) { case "MN:": if (!Enum.TryParse <Months>(item.Substring(3), true, out months) || months == Months.None) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the months defined for the Monthly tasks"); } break; case "WK:": if (!Enum.TryParse <Weeks>(item.Substring(3), true, out week) || week == Weeks.None) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the weeks defined for the Monthly tasks"); } break; case "WD:": if (!Enum.TryParse <Weekdays>(item.Substring(3), true, out weekdays) || weekdays == Weekdays.None) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the weekdays defined for the Monthly tasks"); } break; case "DY:": foreach (var d in item.Substring(3).Split(',')) { int x = 0; uint day = 1; if (d == "LAST") { days |= 0x80000000; //Last day of the month } else if (d == "ALL") { days |= 0x7FFFFFFF; //This is for all 1-31 days } else if (!int.TryParse(d, out x) || x < 1 || x > 31) { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not have the day defined for the Monthly tasks in a valid range"); } else { days |= day << (x - 1); } } break; default: throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not conform to the format"); } } if (week != Weeks.None && weekdays != Weekdays.None && months != Months.None) { _recur = new MonthlyByWeekdays(months, weekdays, week); } else if (days > 1 && months != Months.None) { _recur = new MonthlyByDay(months, days); } else { throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not conform to the format"); } break; default: throw new ArgumentOutOfRangeException(nameof(schedule), "Schedule does not conform to the format"); } _lastDateTime = new DateTime(_startDate.Year, _startDate.Month, _startDate.Day, _taskTime.Hours, _taskTime.Minutes, _taskTime.Seconds, DateTimeKind.Unspecified); ///If not daily task, initialize to correct start marker i.e. next schedule to align with the configuration ///If the task is configured for every Monday and this process starts on Thursday //if (recursion != Recursion.Daily) // _lastDateTime = Next(_lastDateTime); }