Class DaemonService
The daemon control plane. See IDaemonService for the security model.
public sealed class DaemonService : BaseService<DaemonCommandEntity>, IDaemonService
- Inheritance
-
DaemonService
- Implements
- Inherited Members
Constructors
DaemonService(INpgsqlDbContextFactory)
public DaemonService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactory
Methods
ClaimCommandsAsync(string, string, int, CancellationToken)
Claims the commands waiting for one host.
public Task<DatabaseResult<IReadOnlyList<DaemonCommandData>>> ClaimCommandsAsync(string hostName, string instanceId, int maxCommands, CancellationToken cancellationToken = default)
Parameters
hostNamestringThe claiming daemon's own host name. It gets nothing else.
instanceIdstringThis daemon process's identity, stamped on what it takes.
maxCommandsintHow many to take at once.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Atomic: the rows are marked as this instance's in the same statement that selects them, so two daemons sharing a host name cannot both execute one command, and a daemon that dies mid-command leaves a claimed row somebody can see rather than one that is quietly done twice.
Expired commands are never claimed. A restart carried out twenty minutes after it was asked for — once the operator has moved on and players are back on the server — is worse than one that never happened. The one expired command that can come back is this call's own claim, retaken by a retry after its first reply was lost, with no time left: the caller refuses it and reports that, rather than leaving it claimed with no outcome.
Expiry is judged by the database clock, which stamped the command, and each returned command carries the seconds it had left at the claim (ExpiresInSeconds). The caller re-checks by counting those down on its own monotonic clock (DaemonCommandExpiry), never by comparing ExpiresUtc with its wall clock.
CompleteCommandAsync(long, string, bool, string, CancellationToken)
Records how a claimed command ended.
public Task<DatabaseResult> CompleteCommandAsync(long commandId, string instanceId, bool succeeded, string outcome, CancellationToken cancellationToken = default)
Parameters
commandIdlonginstanceIdstringsucceededbooloutcomestringcancellationTokenCancellationToken
Returns
Remarks
Only by the instance that claimed it, so a second daemon cannot overwrite the outcome of work it did not do. A refusal — the application is not in this daemon's configuration — is reported through here as a failure, not swallowed: an operator who asked for something impossible must be told.
EnqueueCommandAsync(string, string, DaemonCommandVerb, string, string, TimeSpan, CancellationToken)
Queues a command.
public Task<DatabaseResult<long>> EnqueueCommandAsync(string hostName, string appName, DaemonCommandVerb verb, string requestedBy, string reason, TimeSpan lifetime, CancellationToken cancellationToken = default)
Parameters
hostNamestringThe host to act on.
appNamestringThe supervised application, by name.
verbDaemonCommandVerbStart, stop or restart.
requestedBystringThe operator account.
reasonstringWhy. Recorded here and in the audit log.
lifetimeTimeSpanHow long it stays executable before being abandoned, counted from the database's time at the write.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Refuses a host or application the daemons have not reported. That is not a convenience check: it keeps the set of things a command can name to what some daemon already supervises, so the queue cannot be used to invent a target.
FetchAppEventsAsync(string, string, DaemonAppStatus?, DateTime?, DateTime?, int, int, CancellationToken)
The supervision history: what the daemons observed changing, newest first.
public Task<DatabaseResult<DaemonAppEventPage>> FetchAppEventsAsync(string hostName, string appName, DaemonAppStatus? status, DateTime? fromUtc, DateTime? toUtc, int page, int pageSize, CancellationToken cancellationToken = default)
Parameters
hostNamestringRestrict to one host, or null for all.
appNamestringRestrict to one application by name, or null for all.
statusDaemonAppStatus?Restrict to a resulting status, or null for all.
fromUtcDateTime?Only observations at or after this instant, when given.
toUtcDateTime?Only observations at or before this instant, when given.
pageint1-based page number.
pageSizeintRows per page, clamped by the service.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Not what the command history is. That records what operators asked for through
this panel. This records what the supervisors saw happen on their own — a process that
died at three in the morning, the restarts that followed, and whether it came back —
none of which anybody asked for and none of which is recorded anywhere else. The
current-state row in daemon_apps is overwritten every beat, so a fault that has
already been recovered from leaves no trace at all without this.
And it is not process output. No log line, no exception text and nothing else a supervised process wrote reaches here; the rows are derived server-side from two enum values and two integers. Log contents routinely carry connection strings and tokens inside exception messages, and a panel route that relayed them would turn a read-only operator view into a way of reading them out of the machine.
FetchCommandsAsync(string, int, int, CancellationToken)
The command history, newest first.
public Task<DatabaseResult<DaemonCommandPage>> FetchCommandsAsync(string hostName, int page, int pageSize, CancellationToken cancellationToken = default)
Parameters
hostNamestringpageintpageSizeintcancellationTokenCancellationToken
Returns
FetchHostsAsync(CancellationToken)
Every host and what it supervises, for the panel.
public Task<DatabaseResult<IReadOnlyList<DaemonHostData>>> FetchHostsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
HeartbeatAsync(string, string, string, int, DateTime, IReadOnlyList<DaemonAppReport>, CancellationToken)
Registers a daemon and refreshes its heartbeat, replacing what it supervises.
public Task<DatabaseResult> HeartbeatAsync(string hostName, string daemonVersion, string osDescription, int processorCount, DateTime startedUtc, IReadOnlyList<DaemonAppReport> apps, CancellationToken cancellationToken = default)
Parameters
hostNamestringThe machine's name.
daemonVersionstringThe daemon build.
osDescriptionstringWhat the operating system calls itself.
processorCountintLogical processors.
startedUtcDateTimeWhen the daemon process started.
appsIReadOnlyList<DaemonAppReport>What it supervises, as observed this cycle.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
The reported list is authoritative for that host: an application removed from the daemon's configuration disappears here on the next beat, rather than lingering as a row an operator can still aim a command at.