Skip to content

Commit

Permalink
PR: Analytics runbooks are scheduled at incorrect times (#23)
Browse files Browse the repository at this point in the history
* Fix #22
* Add tests
  • Loading branch information
jesusfer authored Jul 29, 2024
1 parent 1d2c70e commit 5c6bd57
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public override async Task<AutomationAccountResource> ExecuteTaskReturnResult(ob
// Schedules
_logger.LogInformation($"Creating/updating automation schedules for '{_config.ResourceName}'...");

var nextSunday1pm = Next(DateTime.UtcNow.AddHours(13), DayOfWeek.Sunday);
var nextSunday6pm = Next(DateTime.UtcNow.AddHours(13), DayOfWeek.Sunday);
var nextSunday11pm = Next(DateTime.UtcNow.AddHours(13), DayOfWeek.Sunday);
var nextSunday1pm = NextSundayAt(13, DateTimeKind.Utc);
var nextSunday6pm = NextSundayAt(18, DateTimeKind.Utc);
var nextSunday11pm = NextSundayAt(23, DateTimeKind.Utc);
var nextSunday1pmSchedule = new AutomationScheduleCreateOrUpdateContent("Weekly Sunday 1pm", nextSunday1pm, AutomationScheduleFrequency.Week) { Interval = BinaryData.FromString("1") };
var nextSunday6pmSchedule = new AutomationScheduleCreateOrUpdateContent("Weekly Sunday 6pm", nextSunday1pm, AutomationScheduleFrequency.Week) { Interval = BinaryData.FromString("1") };
var nextSunday11pmSchedule = new AutomationScheduleCreateOrUpdateContent("Weekly Sunday 11pm", nextSunday1pm, AutomationScheduleFrequency.Week) { Interval = BinaryData.FromString("1") };
var nextSunday6pmSchedule = new AutomationScheduleCreateOrUpdateContent("Weekly Sunday 6pm", nextSunday6pm, AutomationScheduleFrequency.Week) { Interval = BinaryData.FromString("1") };
var nextSunday11pmSchedule = new AutomationScheduleCreateOrUpdateContent("Weekly Sunday 11pm", nextSunday11pm, AutomationScheduleFrequency.Week) { Interval = BinaryData.FromString("1") };

var schedules = automationAccount.GetAutomationSchedules();
await schedules.CreateOrUpdateAsync(WaitUntil.Completed, nextSunday1pmSchedule.Name, nextSunday1pmSchedule);
Expand Down Expand Up @@ -132,5 +132,18 @@ public static DateTime Next(DateTime from, DayOfWeek dayOfWeek)
target += 7;
return from.AddDays(target - start);
}

/// <summary>
/// Returns a DateTime object for the next Sunday at a specific time
/// </summary>
/// <param name="hour"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public static DateTime NextSundayAt(int hour24, DateTimeKind kind = DateTimeKind.Local)
{
var now = kind == DateTimeKind.Utc ? DateTime.UtcNow : DateTime.Now;
var nextSunday = Next(now, DayOfWeek.Sunday);
return new DateTime(nextSunday.Year, nextSunday.Month, nextSunday.Day, hour24, 0, 0, kind);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using App.ControlPanel.Engine.InstallerTasks;
using App.ControlPanel.Engine.InstallerTasks.Adoptify;
using App.ControlPanel.Engine.InstallerTasks.Adoptify.Models;
using App.ControlPanel.Engine.InstallerTasks.Tasks;
using App.ControlPanel.Engine.Models;
using App.ControlPanel.Engine.SharePointModelBuilder;
using App.ControlPanel.Engine.SharePointModelBuilder.ValueLookups;
Expand Down Expand Up @@ -542,6 +543,59 @@ public async Task AdoptifyArmResourcesInstallJob()
await job.Install();
}
}

[TestMethod]
public void Automation_Next_Tuesday()
{
var thisDayWasTuesday = new DateTime(2024, 7, 23, 1, 0, 0, DateTimeKind.Utc);
var aSunday = AutomationAccountTask.Next(thisDayWasTuesday, DayOfWeek.Sunday);
Assert.IsTrue(aSunday.DayOfWeek == DayOfWeek.Sunday);
Assert.IsTrue(aSunday == new DateTime(2024, 7, 28, 1, 0, 0, DateTimeKind.Utc));
}

[TestMethod]
public void Automation_Next_Sunday()
{
var thisDayWasSunday = new DateTime(2024, 7, 28, 1, 0, 0, DateTimeKind.Utc);
var aSunday = AutomationAccountTask.Next(thisDayWasSunday, DayOfWeek.Sunday);
Assert.IsTrue(aSunday.DayOfWeek == DayOfWeek.Sunday);
Assert.IsTrue(aSunday == new DateTime(2024, 8, 4, 1, 0, 0, DateTimeKind.Utc));
}

[TestMethod]
public void Automation_Next_Sunday_Midnight()
{
var thisDayWasSunday = new DateTime(2024, 7, 28, 0, 0, 0, DateTimeKind.Utc);
var aSunday = AutomationAccountTask.Next(thisDayWasSunday, DayOfWeek.Sunday);
Assert.IsTrue(aSunday.DayOfWeek == DayOfWeek.Sunday);
Assert.IsTrue(aSunday == new DateTime(2024, 8, 4, 0, 0, 0, DateTimeKind.Utc));
}

[TestMethod]
public void Automation_NextSundayAt_UTC()
{
var now = DateTime.UtcNow;
var nextSunday = AutomationAccountTask.Next(now, DayOfWeek.Sunday);

var nextSunday1pm = AutomationAccountTask.NextSundayAt(13);
Assert.IsTrue(nextSunday1pm.DayOfWeek == DayOfWeek.Sunday);
Assert.IsTrue(nextSunday1pm.Hour == 13);
Assert.IsTrue(nextSunday1pm.Minute == 0);
Assert.IsTrue(nextSunday1pm.Date == nextSunday.Date);
}

[TestMethod]
public void Automation_NextSundayAt_Local()
{
var now = DateTime.Now;
var nextSunday = AutomationAccountTask.Next(now, DayOfWeek.Sunday);

var nextSunday4pm = AutomationAccountTask.NextSundayAt(16);
Assert.IsTrue(nextSunday4pm.DayOfWeek == DayOfWeek.Sunday);
Assert.IsTrue(nextSunday4pm.Hour == 16);
Assert.IsTrue(nextSunday4pm.Minute == 0);
Assert.IsTrue(nextSunday4pm.Date == nextSunday.Date);
}
}

public class AzureTestsConfigReader
Expand Down

0 comments on commit 5c6bd57

Please sign in to comment.