Table of Contents

Interface IMaintenanceService

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

Maintenance windows: locking a set of servers, draining them, and stopping them together.

public interface IMaintenanceService

Remarks

This service sequences the two controls that already exist; it does not reimplement them. Every server is locked and scheduled through SetShutdownAsync(long, DateTime?, CancellationToken) and SetShutdownAsync(long, DateTime?, CancellationToken), which write locked = true and shutdown_at_utc in one statement. Nothing here opens a connection to a process.

What makes a window durable is that it is fully actuated when it starts. The drain is not a timer somebody has to keep running: the deadline is an absolute instant written onto each server's own row, and each server counts down to it inside its own process, warning its players as it goes. An operator can start a twenty minute drain, close their laptop, and the shard still goes down on time with the panel switched off. That is the whole reason the plan is rows.

What is not automatic is the bookkeeping. AdvanceAsync(CancellationToken) is what moves an operation from Draining to ShuttingDown to Completed, and it only runs when something calls it — every read here calls it first, so the status is correct whenever anybody looks. With nothing polling, a finished window whose targets are long gone will still read "Draining" until the next person opens the page. The servers stopped on time regardless; it is the record that is late. Hosting this on a timer is one IHostedService that calls AdvanceAsync(CancellationToken) every few seconds.

Cancelling does not unlock anything. CancelAsync(long, string, string, CancellationToken) clears the deadline column and nothing else, because that is all the underlying primitive does — the lock that scheduling applied stays applied. A cancelled window therefore leaves every target closed to new arrivals, and every caller has to say so, or a shard is left where nobody can log in and the cause is nowhere near where anyone will look.

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.

Task<DatabaseResult<int>> AdvanceAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation 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.

CancelAsync(long, string, string, CancellationToken)

Clears the scheduled shutdowns. The targets stay locked.

Task<DatabaseResult<MaintenanceOperationData>> CancelAsync(long operationId, string cancelledBy, string reason, CancellationToken cancellationToken = default)

Parameters

operationId long

The window to call off.

cancelledBy string

The operator account cancelling it. Recorded.

reason string

Why. Mandatory.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<MaintenanceOperationData>>

The cancelled window. Every target whose LockWrittenUtc is 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.

Task<DatabaseResult<MaintenanceOperationData>> FetchAsync(long operationId, CancellationToken cancellationToken = default)

Parameters

operationId long

The window to read.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<MaintenanceOperationData>>

Remarks

Advances it first, so what is returned reflects the servers as they are now.

ListAsync(int, CancellationToken)

Windows, newest first, each with its targets.

Task<DatabaseResult<IReadOnlyList<MaintenanceOperationData>>> ListAsync(int limit = 50, CancellationToken cancellationToken = default)

Parameters

limit int

How many to return. Clamped by the service.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<IReadOnlyList<MaintenanceOperationData>>>

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.

Task<DatabaseResult<MaintenanceOperationData>> StartAsync(string name, string startedBy, string reason, int drainSeconds, IReadOnlyList<MaintenanceTargetRequest> targets, CancellationToken cancellationToken = default)

Parameters

name string

What to call it, for the listing.

startedBy string

The operator account planning it. Recorded.

reason string

Why. Mandatory.

drainSeconds int

How long players get. Zero stops at the next pulse.

targets IReadOnlyList<MaintenanceTargetRequest>

The world and scene servers to take down.

cancellationToken CancellationToken

Cancellation 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.