Table of Contents

Class DaemonService

Namespace
FishMMO.Database.Npgsql.Services
Assembly
FishMMO-DB.dll

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

dbContextFactory INpgsqlDbContextFactory

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

hostName string

The claiming daemon's own host name. It gets nothing else.

instanceId string

This daemon process's identity, stamped on what it takes.

maxCommands int

How many to take at once.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<IReadOnlyList<DaemonCommandData>>>

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

commandId long
instanceId string
succeeded bool
outcome string
cancellationToken CancellationToken

Returns

Task<DatabaseResult>

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

hostName string

The host to act on.

appName string

The supervised application, by name.

verb DaemonCommandVerb

Start, stop or restart.

requestedBy string

The operator account.

reason string

Why. Recorded here and in the audit log.

lifetime TimeSpan

How long it stays executable before being abandoned, counted from the database's time at the write.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<long>>

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

hostName string

Restrict to one host, or null for all.

appName string

Restrict to one application by name, or null for all.

status DaemonAppStatus?

Restrict to a resulting status, or null for all.

fromUtc DateTime?

Only observations at or after this instant, when given.

toUtc DateTime?

Only observations at or before this instant, when given.

page int

1-based page number.

pageSize int

Rows per page, clamped by the service.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<DaemonAppEventPage>>

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

hostName string
page int
pageSize int
cancellationToken CancellationToken

Returns

Task<DatabaseResult<DaemonCommandPage>>

FetchHostsAsync(CancellationToken)

Every host and what it supervises, for the panel.

public Task<DatabaseResult<IReadOnlyList<DaemonHostData>>> FetchHostsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<DatabaseResult<IReadOnlyList<DaemonHostData>>>

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

hostName string

The machine's name.

daemonVersion string

The daemon build.

osDescription string

What the operating system calls itself.

processorCount int

Logical processors.

startedUtc DateTime

When the daemon process started.

apps IReadOnlyList<DaemonAppReport>

What it supervises, as observed this cycle.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

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.