Class HealthMonitor
- Namespace
- AppHealthMonitor
- Assembly
- AppHealthMonitor.dll
Monitors the health and lifecycle of a single application process. Provides automatic restart capabilities, resource monitoring, and port health checks.
public sealed class HealthMonitor : IAsyncDisposable
- Inheritance
-
HealthMonitor
- Implements
- Inherited Members
Constructors
HealthMonitor(AppConfig, IReadOnlyList<IHealthChecker>, bool, CancellationToken, ISupervisionHost?)
Initializes a new instance of the HealthMonitor class.
public HealthMonitor(AppConfig config, IReadOnlyList<IHealthChecker> healthCheckers, bool headless, CancellationToken cancellationToken, ISupervisionHost? supervisionHost = null)
Parameters
configAppConfigThe application configuration. Must have TryApplyDefaultsAndValidate(out string) called first.
healthCheckersIReadOnlyList<IHealthChecker>The health checkers to use for port monitoring. Empty list for process-only monitoring.
headlessboolWhether to launch the process in headless mode (no window, shell execution disabled).
cancellationTokenCancellationTokenToken to signal cancellation of monitoring operations.
supervisionHostISupervisionHostThe monitoring cycle this monitor runs in, asked for supervision back when an operator starts or restarts an application whose loop has already ended. Null means no revival is possible and such a command is refused rather than leaving an unwatched process.
Exceptions
- ArgumentNullException
Thrown when config is null.
Properties
IsMonitoringLoopEnded
Gets whether the monitoring loop has ended for this monitor (restart attempts exhausted, initial launch failed, or the cycle was cancelled).
public bool IsMonitoringLoopEnded { get; }
Property Value
IsOperatorStopped
Gets whether an operator deliberately stopped this application and it must stay stopped.
public bool IsOperatorStopped { get; }
Property Value
Name
Gets the display name of the monitored application, used for diagnostics and logging.
public string Name { get; }
Property Value
Methods
DisposeAsync()
Disposes of the monitor by killing any active process. Health checkers are NOT disposed here — they are shared across monitoring cycles and owned by DaemonOrchestrator.
public ValueTask DisposeAsync()
Returns
GetStatus()
Returns a snapshot of the current monitor status for diagnostics.
public HealthMonitorStatus GetStatus()
Returns
- HealthMonitorStatus
A HealthMonitorStatus representing the current state.
KillApplicationAsync()
Asynchronously terminates the monitored application process. Attempts graceful shutdown first (SIGTERM on Linux/macOS, CloseMainWindow on Windows), then force-kills if the process does not exit within the configured timeout. Uses a standalone bounded timeout (not linked to the monitoring lifecycle token) to ensure graceful shutdown is always honored, even during stop/shutdown disposal.
public Task KillApplicationAsync()
Returns
- Task
A task representing the asynchronous kill operation.
RestartByOperatorAsync(CancellationToken)
Stops and starts the application on an operator's instruction, clearing any operator stop.
public Task<(bool Succeeded, string Outcome)> RestartByOperatorAsync(CancellationToken token)
Parameters
tokenCancellationTokenToken cancelled when monitoring or the daemon shuts down.
Returns
- Task<(bool Succeeded, string Outcome)>
Whether the restart succeeded, and a sentence describing what happened.
StartByOperatorAsync(CancellationToken)
Starts the application on an operator's instruction, clearing any operator stop.
public Task<(bool Succeeded, string Outcome)> StartByOperatorAsync(CancellationToken token)
Parameters
tokenCancellationTokenToken cancelled when monitoring or the daemon shuts down.
Returns
- Task<(bool Succeeded, string Outcome)>
Whether the start succeeded, and a sentence describing what happened.
StartMonitoringAsync()
Starts the monitoring loop for the application. Continuously checks application health and performs restarts as needed. Exits when cancelled, when maximum restart attempts are exhausted, or when the initial launch fails.
public Task StartMonitoringAsync()
Returns
- Task
A task that represents the asynchronous monitoring operation.
StopByOperatorAsync(CancellationToken)
Stops the application on an operator's instruction and leaves it stopped.
public Task<(bool Succeeded, string Outcome)> StopByOperatorAsync(CancellationToken token)
Parameters
tokenCancellationTokenToken cancelled when monitoring or the daemon shuts down.
Returns
- Task<(bool Succeeded, string Outcome)>
Whether the stop succeeded, and a sentence describing what happened.
Remarks
Sets the operator-stop flag before killing, so the monitoring loop stops health-checking it and every launch path refuses until an operator starts or restarts it. The flag is the mechanism: killing alone would simply be seen as an unhealthy process on the next check and restarted.