diff --git a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs index df2f1bd..eebe3e5 100644 --- a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs +++ b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs @@ -101,6 +101,10 @@ class ServiceSettings public DayOfWeek DayOfWeek { get; private set; } + public int WeekOfMonth { get; private set; } + + public TimeSpan Time { get; private set; } + public DateTime Date { get; private set; } public bool IsLastDayOfMonth { get; private set; } @@ -169,18 +173,14 @@ private void ParseWUFrequency() throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency); } - var weekOfMonth = (int)Convert.ChangeType(arr[1], typeof(int)); - var dayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), arr[2]); + this.WeekOfMonth = (int)Convert.ChangeType(arr[1], typeof(int)); + this.DayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), arr[2]); + this.Time = TimeSpan.Parse(arr[3]); - var firstDayOfMonth = new DateTime(currentDateTime.Year, currentDateTime.Month, day: 1); - var firstWeekOfMonth = firstDayOfMonth.AddDays((dayOfWeek + 7 - firstDayOfMonth.DayOfWeek) % 7); - var daysToAdd = 7 * (weekOfMonth - 1); - if ((firstWeekOfMonth.Day + daysToAdd) > MaxSupportedDayOfMonth) + if (this.WeekOfMonth < 1 || this.WeekOfMonth > 4) { - throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency + ". The day of month should be between 1 to 28."); + throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency + ". The WeekOfMonth should be between 1 to 4."); } - this.Date = firstWeekOfMonth.AddDays(daysToAdd); - this.Date = this.Date.Date + TimeSpan.Parse(arr[3]); break; case Frequency.Weekly: diff --git a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs index 25bae2a..98b2152 100644 --- a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs +++ b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs @@ -640,12 +640,13 @@ private DateTime GetNextSchedulingTime() case Frequency.MonthlyByWeekAndDay: { - var next = new DateTime(currentTime.Year, currentTime.Month, settingsDateTime.Day, - settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second); + var next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime, settings); + if (DateTime.Compare(next, currentTime) < 0) { - next = next.AddMonths(1); + next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime.AddMonths(1), settings); } + return next; } @@ -695,6 +696,17 @@ private DateTime GetNextSchedulingTime() } } + private DateTime GetNextDateTimeForMonthlyByWeekAndDayFrequency(DateTime currentTime, ServiceSettings settings) + { + var firstDayOfMonth = new DateTime(currentTime.Year, currentTime.Month, day: 1); + var firstWeekOfMonth = firstDayOfMonth.AddDays((settings.DayOfWeek + 7 - firstDayOfMonth.DayOfWeek) % 7); + var daysToAdd = 7 * (settings.WeekOfMonth - 1); + var dayOfMonth = firstWeekOfMonth.AddDays(daysToAdd); + var dayOfMonthWithTime = dayOfMonth + settings.Time; + + return dayOfMonthWithTime; + } + class CheckpointFileData { public DateTime schedulingDateTime = default(DateTime);