From 29c02429d66c1d4e7100c07066c03c96de2ee73a Mon Sep 17 00:00:00 2001 From: Adib Alwani Date: Tue, 21 Apr 2020 13:56:58 -0700 Subject: [PATCH 1/2] POA WUFrequency support for MonthlyByWeekAndDay --- .../src/Manager/ServiceSettings.cs | 32 ++++++++++++++++--- .../src/Manager/TimerManager.cs | 11 +++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/ServiceSettings.cs index 156defd..df2f1bd 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; } @@ -161,6 +163,26 @@ private void ParseWUFrequency() } break; + case Frequency.MonthlyByWeekAndDay: + if (arr.Count() != 4) + { + 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]); + + 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) + { + throw new ArgumentException("Illegal WUFrequency Parameter : " + WUFrequency + ". The day of month should be between 1 to 28."); + } + this.Date = firstWeekOfMonth.AddDays(daysToAdd); + this.Date = this.Date.Date + TimeSpan.Parse(arr[3]); + 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..25bae2a 100644 --- a/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs +++ b/src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/TimerManager.cs @@ -638,6 +638,17 @@ private DateTime GetNextSchedulingTime() return next; } + case Frequency.MonthlyByWeekAndDay: + { + var next = new DateTime(currentTime.Year, currentTime.Month, settingsDateTime.Day, + settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second); + if (DateTime.Compare(next, currentTime) < 0) + { + next = next.AddMonths(1); + } + return next; + } + case Frequency.Weekly: { DateTime next = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, settingsDateTime.Hour, settingsDateTime.Minute, settingsDateTime.Second); From 4441f7a75cf79149c75d3c7a3d5a1b28b4a72fd0 Mon Sep 17 00:00:00 2001 From: Adib Alwani Date: Tue, 28 Apr 2020 12:02:00 -0700 Subject: [PATCH 2/2] Fixing next patching attempt date --- .../src/Manager/ServiceSettings.cs | 18 +++++++++--------- .../src/Manager/TimerManager.cs | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) 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);