Class MaintenanceService
Maintenance windows. See IMaintenanceService for what durability means here.
public sealed class MaintenanceService : BaseService<MaintenanceOperationEntity>, IMaintenanceService
- Inheritance
-
MaintenanceService
- Implements
- Inherited Members
Remarks
The two per-tier services are injected and are the only things that write a server's
control columns. This service reads world_servers and scene_servers
directly to observe them, the way the server board does, but it never writes
them: a second implementation of "schedule a shutdown" that drifted from the first would
be a shard that half stops.
Constructors
MaintenanceService(INpgsqlDbContextFactory, IWorldServerService, ISceneServerService)
public MaintenanceService(INpgsqlDbContextFactory dbContextFactory, IWorldServerService worldServers, ISceneServerService sceneServers)
Parameters
dbContextFactoryINpgsqlDbContextFactoryworldServersIWorldServerServicesceneServersISceneServerService
Fields
MaxDrainSeconds
Longest drain that may be scheduled: 24 hours.
public const int MaxDrainSeconds = 86400
Field Value
Remarks
The same ceiling the single-server shutdown enforces, and for the same reason: it bounds a typo rather than a policy. A mistyped drain should be refused, not lock a shard for eleven weeks.
MaxTargets
Most servers in one window.
public const int MaxTargets = 64
Field Value
Remarks
Generous for any real shard — a dozen is a big one — and low enough that a malformed request cannot ask for ten thousand writes.
ShutdownGraceSeconds
How long past the deadline a server may still be pulsing before the target is Failed.
public const int ShutdownGraceSeconds = 120
Field Value
Remarks
A healthy server acts on its deadline within a pulse. Two minutes is room for a slow save and a missed beat; past that, something is wrong and saying "shutting down" would be a guess dressed as a status.
StaleAfterSeconds
How long without a pulse before a server counts as silent.
public const int StaleAfterSeconds = 60
Field Value
Remarks
Matches the server board, so the two surfaces never disagree about a server.
Methods
AdvanceAsync(CancellationToken)
Brings every live window up to date with what its servers' rows now say, and finishes writing any target the start did not manage to write.
public Task<DatabaseResult<int>> AdvanceAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<int>>
How many live windows were brought up to date.
Remarks
Idempotent and safe to call concurrently: it re-derives each target's status from the server's own row rather than stepping a state machine forward, so two callers racing reach the same answer. Every read on this service calls it.
Concurrency rests on row locks, not on the re-derivation alone. Each target's write and
the record of it are one transaction holding that target's row lock, and the
derivation locks every live target before reading, so it never sees a server written
but the write unrecorded. An unwritten target is failed only once
ShutdownGraceSeconds past its deadline — the instant the write step itself
stops trying — so a zero-second drain is not failed by a pass that runs before its
first write lands.
It is not what makes a shutdown happen — the servers do that from the deadline on their own rows — so a deployment with nothing calling this still takes its shards down on time. What it costs is that the record only catches up when somebody looks.
AgeSeconds(DateTime, DateTime)
Seconds from stampUtc to databaseNowUtc, never
negative.
public static double AgeSeconds(DateTime databaseNowUtc, DateTime stampUtc)
Parameters
Returns
Remarks
Both instants must come from the database clock: a stamp it wrote, and a "now" read from it (ReadDatabaseUtcNowAsync(NpgsqlDbContext, CancellationToken)). Clamped at zero because a pulse that commits between the clock read and the row read is a few milliseconds newer than the instant it is measured against, as on the server board.
CancelAsync(long, string, string, CancellationToken)
Clears the scheduled shutdowns. The targets stay locked.
public Task<DatabaseResult<MaintenanceOperationData>> CancelAsync(long operationId, string cancelledBy, string reason, CancellationToken cancellationToken = default)
Parameters
operationIdlongThe window to call off.
cancelledBystringThe operator account cancelling it. Recorded.
reasonstringWhy. Mandatory.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<MaintenanceOperationData>>
The cancelled window. Every target whose
LockWrittenUtcis set is still locked, and the caller must say so.
Remarks
A server that has already read its deadline and begun stopping does not come back because the column was cleared; the returned notes say which targets were past that point.
The clears and the record are one transaction under the targets' row locks, so a concurrent AdvanceAsync(CancellationToken) cannot write a deadline back between them: a write already in flight is cleared here, and a later one finds the target cancelled.
FetchAsync(long, CancellationToken)
One window, with per-target progress.
public Task<DatabaseResult<MaintenanceOperationData>> FetchAsync(long operationId, CancellationToken cancellationToken = default)
Parameters
operationIdlongThe window to read.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Advances it first, so what is returned reflects the servers as they are now.
IsSilent(double)
Whether a server whose last pulse is this many seconds old counts as silent.
public static bool IsSilent(double pulseAgeSeconds)
Parameters
pulseAgeSecondsdouble
Returns
Remarks
Strictly past StaleAfterSeconds: a pulse exactly that old is still a pulsing server. The one rule behind a target's PulsingAtStart, the derivation's "silent" and the panel's not-pulsing badge.
ListAsync(int, CancellationToken)
Windows, newest first, each with its targets.
public Task<DatabaseResult<IReadOnlyList<MaintenanceOperationData>>> ListAsync(int limit = 50, CancellationToken cancellationToken = default)
Parameters
limitintHow many to return. Clamped by the service.
cancellationTokenCancellationTokenCancellation token.
Returns
Remarks
Advances every live window first, so the listing is current.
StartAsync(string, string, string, int, IReadOnlyList<MaintenanceTargetRequest>, CancellationToken)
Plans a window, locks every target immediately, and schedules the shutdown for the end of the drain.
public Task<DatabaseResult<MaintenanceOperationData>> StartAsync(string name, string startedBy, string reason, int drainSeconds, IReadOnlyList<MaintenanceTargetRequest> targets, CancellationToken cancellationToken = default)
Parameters
namestringWhat to call it, for the listing.
startedBystringThe operator account planning it. Recorded.
reasonstringWhy. Mandatory.
drainSecondsintHow long players get. Zero stops at the next pulse.
targetsIReadOnlyList<MaintenanceTargetRequest>The world and scene servers to take down.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<MaintenanceOperationData>>
The planned window, with each target's outcome.
Remarks
The plan is committed before any server is touched, so a crash between the two leaves a visible half-written window rather than a locked shard with no record. AdvanceAsync(CancellationToken) finishes the writing on its next pass.
A target that cannot be written is recorded as Failed and the rest of the window
continues. Refusing the whole window because one server's row had gone would leave an
operator to do the other eleven by hand, which is the thing this exists to prevent.
A transient failure is not a refusal: the target stays pending, its note says why,
and every AdvanceAsync(CancellationToken) retries it until ShutdownGraceSeconds
past the deadline, after which it is Failed as never written.
Refused outright when: the reason is empty; there are no targets; the drain is negative or beyond a day; a named server does not exist; or a named server is already inside a live window, since two plans would fight over one deadline column.
Safe to retry after a lost commit: the plan carries a request key taken once per call, and an attempt that finds its own key already written returns that window rather than counting it as the live window its servers clash with.