b8f4aced81
Restart After Run (Off / Always / When the Script Asks, with a delay up to 60 minutes) is a script option, versioned in the run snapshot and carried in ScriptCommandPayload like the other run options, and Run Script can override it for one run. Asking means exit 3010 or 1641, and on linux and macOS also 194 (what exit 3010 becomes in an 8-bit status); with the option on, an asking exit is a success. The agent restarts only after the server holds the job's result: the result is kept in the local store first, then a restart request on the device's event subject is answered once the server has stored the final state (applied from the request if its JetStream copy lags) and every output line (missing lines are resent). If the server is unreachable, the kept result is sent after the restart. The job shows Restart Requested, Restarting, then Restarted when the device's inventory reports a newer boot time; Refused, Cancelled and Failed are reported too, and every step is audited. Pending restarts carry an opaque continuation for script sequences. Mechanisms, all behind ISystemRestart: Windows InitiateShutdownW (SHUTDOWN_RESTART | SHUTDOWN_FORCE_OTHERS, the delay as grace period, planned Application: Installation reason) after enabling SeShutdownPrivilege; Windows PE tries the same API, then wpeutil reboot; linux uses logind Reboot/ScheduleShutdown over D-Bus (Tmds.DBus.Protocol), falling back to systemd StartUnit(reboot.target); macOS reports it is unsupported. AllowRestart (default on) is a managed agent setting with ADMX/ADML, an MSI property and registry/agent.json support. The agent service is now plain Automatic: install sets it, and the agent switches an earlier Delayed Start when it starts as a service. Migration RestartAfterRun for SQLite, PostgreSQL and SQL Server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
3.9 KiB
C#
124 lines
3.9 KiB
C#
using System.ComponentModel;
|
|
using Agent.Service;
|
|
using Shared.Infrastructure.Constants;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
/// <summary>
|
|
/// The agent service's registration (the CLI's <c>install</c>, which the MSI runs too) through
|
|
/// the service-control seam: plain Automatic, never Delayed Start; and an earlier Delayed Start
|
|
/// switched when the agent starts.
|
|
/// </summary>
|
|
public sealed class ServiceStartTypeTests
|
|
{
|
|
private const uint AutoStart = 0x2;
|
|
private const uint DemandStart = 0x3;
|
|
|
|
private sealed class FakeServiceConfigApi : IServiceConfigApi
|
|
{
|
|
private static readonly IntPtr Scm = new(1);
|
|
private static readonly IntPtr Service = new(2);
|
|
|
|
public bool Exists { get; set; }
|
|
|
|
public uint StartType { get; set; } = AutoStart;
|
|
|
|
public bool Delayed { get; set; }
|
|
|
|
public List<string> Calls { get; } = [];
|
|
|
|
public IntPtr OpenSCManager(uint access) => Scm;
|
|
|
|
public IntPtr OpenService(IntPtr scm, string name, uint access)
|
|
{
|
|
Assert.Equal(Branding.AgentServiceName, name);
|
|
return Exists ? Service : IntPtr.Zero;
|
|
}
|
|
|
|
public IntPtr CreateService(IntPtr scm, string name, string displayName, uint startType, string binaryPath)
|
|
{
|
|
Calls.Add($"create start={startType}");
|
|
StartType = startType;
|
|
Exists = true;
|
|
return Service;
|
|
}
|
|
|
|
public bool ChangeServiceConfig(IntPtr service, uint startType, string binaryPath, string displayName)
|
|
{
|
|
Calls.Add($"change start={startType}");
|
|
StartType = startType;
|
|
return true;
|
|
}
|
|
|
|
public bool SetDescription(IntPtr service, string description) => true;
|
|
|
|
public bool SetDelayedAutoStart(IntPtr service, bool delayed)
|
|
{
|
|
Calls.Add($"delayed={delayed}");
|
|
Delayed = delayed;
|
|
return true;
|
|
}
|
|
|
|
public bool SetFailureActions(IntPtr service, TimeSpan restartDelay, int restarts, TimeSpan resetPeriod)
|
|
{
|
|
Calls.Add($"recovery {restarts}x{restartDelay.TotalSeconds:F0}s");
|
|
return true;
|
|
}
|
|
|
|
public uint? QueryStartType(IntPtr service) => StartType;
|
|
|
|
public bool? QueryDelayedAutoStart(IntPtr service) => Delayed;
|
|
|
|
public void Close(IntPtr handle)
|
|
{
|
|
}
|
|
|
|
public Win32Exception LastError(string operation) => new(5, operation);
|
|
}
|
|
|
|
[Fact]
|
|
public void ANewInstallIsPlainAutomatic()
|
|
{
|
|
var api = new FakeServiceConfigApi();
|
|
|
|
Assert.Equal(0, ServiceLifecycle.Install(api, "\"C:\\Program Files\\ExecuteR\\ExecuteR-Agent.exe\""));
|
|
|
|
Assert.Equal(["create start=2", "delayed=False", "recovery 3x30s"], api.Calls);
|
|
Assert.Equal((AutoStart, false), (api.StartType, api.Delayed));
|
|
}
|
|
|
|
[Fact]
|
|
public void InstallingOverAnEarlierDelayedStartSwitchesIt()
|
|
{
|
|
var api = new FakeServiceConfigApi { Exists = true, StartType = AutoStart, Delayed = true };
|
|
|
|
ServiceLifecycle.Install(api, "\"agent.exe\"");
|
|
|
|
Assert.Equal(["change start=2", "delayed=False", "recovery 3x30s"], api.Calls);
|
|
Assert.False(api.Delayed);
|
|
}
|
|
|
|
[Fact]
|
|
public void AtStartADelayedStartBecomesAutomatic()
|
|
{
|
|
var api = new FakeServiceConfigApi { Exists = true, StartType = AutoStart, Delayed = true };
|
|
|
|
Assert.True(ServiceLifecycle.EnsureAutomaticStart(api));
|
|
|
|
Assert.Equal(["delayed=False"], api.Calls);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(AutoStart, false)] // already plain Automatic
|
|
[InlineData(DemandStart, true)] // an administrator's Manual stays
|
|
public void AtStartAnythingElseIsLeftAlone(uint startType, bool delayed)
|
|
{
|
|
var api = new FakeServiceConfigApi { Exists = true, StartType = startType, Delayed = delayed };
|
|
|
|
Assert.False(ServiceLifecycle.EnsureAutomaticStart(api));
|
|
|
|
Assert.Empty(api.Calls);
|
|
}
|
|
}
|