Skip to content

Commit

Permalink
add configurable maintenance event to local agent (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
tculotta authored Apr 4, 2024
1 parent fd2fd9d commit 194f41a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions AgentInterfaces/MaintenanceSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class MaintenanceEvent

public DateTime? NotBefore { get; set; }

public string Description { get; set; }

public string EventSource { get; set; }

public int DurationInSeconds { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions LocalMultiplayerAgent.UnitTest/ControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public async Task ValidateStandingByToInitializingThrows()

string sessionHostId = "testSessionHostId";

// Settings is accessed in the heartbeat endpoint
Globals.Settings = new();

// send the first heartbeat with "StandingBy"
SessionHostHeartbeatInfo heartbeat = new SessionHostHeartbeatInfo()
{
Expand Down
8 changes: 8 additions & 0 deletions LocalMultiplayerAgent/Config/MultiplayerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class MultiplayerSettings

public int NumHeartBeatsForTerminateResponse { get; set; }

public int NumHeartBeatsForMaintenanceEventResponse { get; set; }

public bool RunContainer { get; set; }

public string OutputFolder { get; set; }
Expand All @@ -49,6 +51,12 @@ public class MultiplayerSettings

public IDictionary<string, string> DeploymentMetadata { get; set; }

public string MaintenanceEventType { get; set; }

public string MaintenanceEventStatus { get; set; }

public string MaintenanceEventSource { get; set; }

public SessionHostsStartInfo ToSessionHostsStartInfo()
{
// Clear mount path in process based, otherwise, agent will kick into back-compat mode and try to strip the
Expand Down
6 changes: 6 additions & 0 deletions LocalMultiplayerAgent/Config/MultiplayerSettingsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public bool IsValid(bool createBuild = false)
Console.WriteLine("NumHeartBeatsForTerminateResponse must be greater than NumHeartBeatsForActivateResponse. Ideally more than 1, since you would like the server to be alive for a few seconds");
isSuccess = false;
}

if (_settings.NumHeartBeatsForTerminateResponse < _settings.NumHeartBeatsForMaintenanceEventResponse)
{
Console.WriteLine("NumHeartBeatsForMaintenanceEventResponse must be less than or equal to NumHeartBeatsForTerminateResponse.");
isSuccess = false;
}
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Expand Down
34 changes: 32 additions & 2 deletions LocalMultiplayerAgent/Controllers/SessionHostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace LocalMultiplayerAgent.Controllers
using Microsoft.Azure.Gaming.VmAgent.Model;
using Microsoft.Extensions.Hosting;
using Instrumentation;
using System.Collections.Generic;

public class SessionHostController : Controller
{
Expand Down Expand Up @@ -54,6 +55,7 @@ public async Task<IActionResult> ProcessHeartbeat(string sessionHostId,
Operation op = Operation.Continue;
SessionConfig config = null;
Console.WriteLine($"CurrentGameState: {heartbeatRequest.CurrentGameState}");
bool sendMaintenanceEvent = false;

if(!ValidateSessionHostStatusTransition(previousSessionHostStatus, currentGameState))
{
Expand All @@ -78,22 +80,50 @@ public async Task<IActionResult> ProcessHeartbeat(string sessionHostId,
config = Globals.SessionConfig;
}

sendMaintenanceEvent = (numHeartBeats == Globals.Settings.NumHeartBeatsForMaintenanceEventResponse);

HeartBeatsCount[sessionHostId]++;
}
else
{
sendMaintenanceEvent = (Globals.Settings.NumHeartBeatsForMaintenanceEventResponse == 1);
HeartBeatsCount.TryAdd(sessionHostId, 1);
}

previousSessionHostStatus = currentGameState;

return Ok(new SessionHostHeartbeatInfo
SessionHostHeartbeatInfo heartbeatResponse = new SessionHostHeartbeatInfo
{
CurrentGameState = currentGameState,
NextHeartbeatIntervalMs = DefaultHeartbeatIntervalMs,
Operation = op,
SessionConfig = config
});
};

if (_wasGsdkVersionLogged && sendMaintenanceEvent)
{
heartbeatResponse.MaintenanceSchedule = new()
{
DocumentIncarnation = "1",
MaintenanceEvents = new List<MaintenanceEvent>()
{
new()
{
EventId = Guid.NewGuid().ToString(),
EventType = Globals.Settings.MaintenanceEventType,
ResourceType = "VirtualMachine",
AffectedResources = new List<string>() { "vmId" },
EventStatus = Globals.Settings.MaintenanceEventStatus,
NotBefore = DateTime.UtcNow.AddMinutes(5),
Description = $"Scheduled {Globals.Settings.MaintenanceEventType} event for VM",
EventSource = Globals.Settings.MaintenanceEventSource,
DurationInSeconds = 300
}
}
};
}

return Ok(heartbeatResponse);
}

private static bool _wasGsdkVersionLogged = false;
Expand Down
5 changes: 5 additions & 0 deletions LocalMultiplayerAgent/MultiplayerSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
"RunContainer": false,
"OutputFolder": "",
"NumHeartBeatsForActivateResponse": 10,
"NumHeartBeatsForMaintenanceEventResponse": 0, // a value < 1 will disable the maintenance event
"NumHeartBeatsForTerminateResponse": 60,
"AgentListeningPort": 56001,
// Valid maintenance event values are explained here: https://learn.microsoft.com/azure/virtual-machines/windows/scheduled-events#event-properties
"MaintenanceEventType": "Reboot",
"MaintenanceEventStatus": "Scheduled",
"MaintenanceEventSource": "Platform",
"AssetDetails": [
{
"MountPath": "C:\\Assets",
Expand Down

0 comments on commit 194f41a

Please sign in to comment.