diff --git a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs index 156defd..eebe3e5 100644 --- a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs +++ b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs @@ -14,7 +14,8 @@ enum Frequency Daily = 2, Weekly = 3, Monthly = 4, - Hourly = 5 + Hourly = 5, + MonthlyByWeekAndDay = 6 } /// @@ -80,10 +81,11 @@ class ServiceSettings /// The frequency for installing Windows Update. /// The format and Possible Values : /// 1. Monthly,5,12:22:32 - /// 2. Weekly,Tuesday,12:22:32 - /// 3. Daily,12:22:32 - /// 4. Once,12-12-2017,12:22:32 - /// 5. None + /// 2. MonthlyByWeekAndDay,2,Friday,21:00:00 + /// 3. Weekly,Tuesday,12:22:32 + /// 4. Daily,12:22:32 + /// 5. Once,12-12-2017,12:22:32 + /// 6. None /// public string WUFrequency { get; set; } @@ -99,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; } @@ -161,6 +167,22 @@ private void ParseWUFrequency() } break; + case Frequency.MonthlyByWeekAndDay: + if (arr.Count() != 4) + { + throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency); + } + + this.WeekOfMonth = (int)Convert.ChangeType(arr[1], typeof(int)); + this.DayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), arr[2]); + this.Time = TimeSpan.Parse(arr[3]); + + if (this.WeekOfMonth < 1 || this.WeekOfMonth > 4) + { + throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency + ". The WeekOfMonth should be between 1 to 4."); + } + break; + case Frequency.Weekly: if (arr.Count() != 3) { diff --git a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs index 269b24f..98b2152 100644 --- a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs +++ b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs @@ -638,6 +638,18 @@ private DateTime GetNextSchedulingTime() return next; } + case Frequency.MonthlyByWeekAndDay: + { + var next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime, settings); + + if (DateTime.Compare(next, currentTime) < 0) + { + next = this.GetNextDateTimeForMonthlyByWeekAndDayFrequency(currentTime.AddMonths(1), settings); + } + + return next; + } + case Frequency.Weekly: { DateTime next = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second); @@ -684,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);