Table of Contents

Interface ISceneServerService

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

Service interface for scene server registration and management operations. Provides async methods for server registration, heartbeat updates, and retrieval.

public interface ISceneServerService : IFetchByKeyAction<long, SceneServerData>
Inherited Members

Remarks

Write operations (Persist*, Pulse*, Delete*) in this service use execution strategies to ensure transient database failures are automatically retried according to the retry policy configured on the DbContext. This is critical because ExecuteSqlRawAsync and FromSqlRaw do not automatically retry on transient failures without an execution strategy wrapper. BaseService provides execution wrappers for retry and centralized exception mapping; explicit transactions are used only when a write requires multiple database statements.

All methods return DatabaseResult or DatabaseResult<T> to provide structured error information through the DatabaseException system, helping distinguish between: - Validation failures (invalid parameters) - Not found scenarios (server doesn't exist) - Database errors (connection issues, constraint violations, timeouts) - Entity not found errors - Unexpected runtime errors

PersistAsync uses atomic UPSERT to prevent race conditions during concurrent registrations.

Methods

DeleteAsync(long, CancellationToken)

Deletes a scene server registration.

Task<DatabaseResult> DeleteAsync(long serverId, CancellationToken cancellationToken = default)

Parameters

serverId long

Server ID to delete.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

A DatabaseResult indicating success or containing a DatabaseException on failure. Returns DatabaseEntityNotFoundException if server doesn't exist.

Remarks

Uses ExecuteSqlRawAsync with execution strategy wrapping to ensure transient database failures are automatically retried.

FetchSceneServersByIDsAsync(List<long>, int, CancellationToken)

Retrieves multiple scene servers by their IDs in batches.

Task<DatabaseResult<IReadOnlyList<SceneServerData>>> FetchSceneServersByIDsAsync(List<long> serverIds, int maxBatchSize = 500, CancellationToken cancellationToken = default)

Parameters

serverIds List<long>

List of server IDs to query.

maxBatchSize int

Maximum number of IDs per database round-trip (500–1000).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<IReadOnlyList<SceneServerData>>>

A list of SceneServerData for each found server.

Remarks

Each row carries PulseAgeSeconds, measured by the database clock as it was read, which is what a caller must judge liveness by. So does FetchAsync.

PersistAsync(string, string, ushort, int, bool, CancellationToken)

Persists a scene server registration with atomic UPSERT.

Task<DatabaseResult<(long ServerId, SceneServerData ServerData)>> PersistAsync(string name, string address, ushort port, int characterCount, bool locked, CancellationToken cancellationToken = default)

Parameters

name string

Server name (unique identifier).

address string

Server address.

port ushort

Server port.

characterCount int

Current character count.

locked bool

Whether server is locked.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<(long ServerId, SceneServerData ServerData)>>

A DatabaseResult<T> containing a tuple with (ServerId, ServerData) on success, or a DatabaseException on failure.

Remarks

Uses FromSqlRaw with RETURNING clause and execution strategy wrapping to ensure transient database failures are automatically retried. Uses PostgreSQL ON CONFLICT for atomic UPSERT with full data return.

PulseAsync(long, int, CancellationToken)

Updates the last pulse timestamp and character count for a scene server (heartbeat), and reads its operator control state back in the same statement.

Task<DatabaseResult<ServerControlState>> PulseAsync(long serverId, int characterCount, CancellationToken cancellationToken = default)

Parameters

serverId long

Server ID.

characterCount int

Current character count.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<ServerControlState>>

The row's ServerControlState: its lock, and any scheduled shutdown with the seconds left before it as the database clock measured them (ShutdownInSeconds). NotFound when the row is gone.

Remarks

Transient failures are retried by the execution wrapper. The pulse writes neither control column: the row is the authority for both, and the server adopts what it reads.

SetLockedAsync(long, bool, CancellationToken)

Opens or closes this scene server to new arrivals.

Task<DatabaseResult> SetLockedAsync(long serverId, bool locked, CancellationToken cancellationToken = default)

Parameters

serverId long

Scene server row to update.

locked bool

True to close it.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

Success, or NotFound when the row is gone.

Remarks

A locked scene server is skipped by the world server's routing and stops dequeuing scene-load requests, so it drains as its players leave. Players already on it keep playing. The row is the authority; the server adopts it on its next pulse.

SetShutdownAsync(long, DateTime?, CancellationToken)

Schedules or cancels this scene server's shutdown.

Task<DatabaseResult> SetShutdownAsync(long serverId, DateTime? shutdownAtUtc, CancellationToken cancellationToken = default)

Parameters

serverId long

Scene server row to update.

shutdownAtUtc DateTime?

Absolute UTC stop time, or null to cancel.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

Success, or NotFound when the row is gone.

Remarks

Scheduling also locks the server; cancelling does not unlock it. For an absolute instant the caller already holds; a delay belongs on SetShutdownInAsync(long, int, CancellationToken), for the reason given on SetShutdownAsync(long, DateTime?, CancellationToken).

SetShutdownInAsync(long, int, CancellationToken)

Schedules this scene server's shutdown a number of seconds from now by the database clock, and locks it.

Task<DatabaseResult<DateTime>> SetShutdownInAsync(long serverId, int seconds, CancellationToken cancellationToken = default)

Parameters

serverId long

Scene server row to update.

seconds int

Delay in seconds. Zero means now; negative is refused.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<DateTime>>

The UTC deadline written, or NotFound when the row is gone.

Remarks

The deadline is taken from the database clock inside the writing statement, so the delay asked for is the delay that elapses. See SetShutdownInAsync(long, int, CancellationToken), including what a retry does.