Interface IWorldServerService
- Namespace
- FishMMO.Database.Npgsql.Services.Interfaces
- Assembly
- FishMMO-DB.dll
Service interface for world server registration and management operations. Provides async methods for server registration, heartbeat updates, and retrieval.
public interface IWorldServerService : IFetchByKeyAction<long, WorldServerData>
- Inherited Members
Remarks
Execution Strategy: All write operations (PersistAsync, PulseAsync, DeleteAsync) use execution strategy wrappers to handle transient database failures with automatic retry. FromSqlRaw and ExecuteSqlRawAsync calls do not automatically retry without manual wrapping.
Read Operations: Read operations use LINQ queries and are executed via BaseService execution wrappers for consistent exception mapping. Explicit transactions are used only when a write requires multiple database statements.
Error Handling: All database operations return DatabaseResult or DatabaseResult<T> with structured error codes (e.g., STALE_STATE, NOT_FOUND, UNIQUE_VIOLATION, DATABASE_ERROR) for comprehensive, safe error handling.
Methods
DeleteAsync(long, CancellationToken)
Task<DatabaseResult> DeleteAsync(long serverId, CancellationToken cancellationToken = default)
Parameters
serverIdlongcancellationTokenCancellationToken
Returns
FetchActiveAsync(float, CancellationToken)
Fetches active world servers that have pulsed within the timeout window. Filters servers by last_pulse timestamp to return only servers that are currently online.
Task<DatabaseResult<List<WorldServerData>>> FetchActiveAsync(float idleTimeoutSeconds = 60, CancellationToken cancellationToken = default)
Parameters
idleTimeoutSecondsfloatIdle timeout in seconds before server considered inactive (default 60).
cancellationTokenCancellationTokenCancellation token for async operation.
Returns
- Task<DatabaseResult<List<WorldServerData>>>
DatabaseResult containing List of active WorldServerData ordered by name; empty list if no active servers.
Remarks
Operation: LINQ query filtering by last_pulse >= (UtcNow - timeout), ordered by name.
Execution Strategy: BaseService handles retries and centralized exception mapping; explicit transactions are used only when a write requires multiple database statements.
FetchControlStateAsync(long, CancellationToken)
Reads a world server's lock and shutdown state without writing a pulse.
Task<DatabaseResult<ServerControlState>> FetchControlStateAsync(long serverId, CancellationToken cancellationToken = default)
Parameters
serverIdlongWorld server row to read.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ServerControlState>>
The control state, with the time left before any scheduled shutdown measured by the database clock, or NotFound when the row is gone.
Remarks
For scene servers, which host scenes on behalf of a world but do not pulse its row. A world-wide shutdown has to reach them so they can warn their players and clear the world's characters out on the same deadline.
PersistAsync(string, string, ushort, int, bool, CancellationToken)
Persists a world server registration (insert or update). Uses an insert-first approach and falls back to update on unique constraint conflicts.
Task<DatabaseResult<(long ServerId, WorldServerData ServerData, ServerControlState Control)>> PersistAsync(string name, string address, ushort port, int characterCount, bool locked, CancellationToken cancellationToken = default)
Parameters
namestringServer name (unique identifier for conflict resolution).
addressstringServer IP address or hostname.
portushortServer port number.
characterCountintCurrent character count on server.
lockedboolWhether server is locked from accepting new connections.
cancellationTokenCancellationTokenCancellation token for async operation.
Returns
- Task<DatabaseResult<(long ServerId, WorldServerData ServerData, ServerControlState Control)>>
DatabaseResult containing (ServerId, ServerData, Control) if successful. Control is the row's operator state — the lock and any scheduled shutdown — read in the same statement, the same state PulseAsync(long, int, CancellationToken) returns, so a server adopts it from the moment it registers rather than from its first heartbeat.
Remarks
Operation: Attempts INSERT; on unique violation, loads the existing row and updates it.
Returns: The returned ServerId is populated after SaveChanges completes inside the BaseService execution wrapper.
Returns: Failure if name/address empty or operation fails; Success with (ServerId, ServerData, Control) on success.
PulseAsync(long, int, CancellationToken)
Task<DatabaseResult<ServerControlState>> PulseAsync(long serverId, int characterCount, CancellationToken cancellationToken = default)
Parameters
serverIdlongcharacterCountintcancellationTokenCancellationToken
Returns
SetLockedAsync(long, bool, CancellationToken)
Opens or closes this world server to new connections.
Task<DatabaseResult> SetLockedAsync(long serverId, bool locked, CancellationToken cancellationToken = default)
Parameters
serverIdlongWorld server row to update.
lockedboolTrue to close it to new arrivals.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
Success, or NotFound when the row is gone.
Remarks
The row is the authority; the server adopts it on its next pulse. Locking drains rather than evicts — see Locked.
SetShutdownAsync(long, DateTime?, CancellationToken)
Schedules or cancels this world server's shutdown.
Task<DatabaseResult> SetShutdownAsync(long serverId, DateTime? shutdownAtUtc, CancellationToken cancellationToken = default)
Parameters
serverIdlongWorld server row to update.
shutdownAtUtcDateTime?Absolute UTC stop time, or
nullto cancel.cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult>
Success, or NotFound when the row is gone.
Remarks
Scheduling also locks the server, in the same statement. Cancelling does not unlock it: halting a shutdown and reopening to players are separate decisions.
For a caller that already holds an absolute instant shared by several rows, such as a maintenance window stopping every target at one deadline. A request phrased as "in N seconds" belongs on SetShutdownInAsync(long, int, CancellationToken): turning it into an instant here means adding the seconds to the caller's own clock, which moves the deadline by that clock's skew from the database the servers count down against.
SetShutdownInAsync(long, int, CancellationToken)
Schedules this world 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
serverIdlongWorld server row to update.
secondsintDelay in seconds. Zero means now; negative is refused.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<DateTime>>
The UTC deadline written, or NotFound when the row is gone.
Remarks
The deadline is the database's own time plus seconds, taken inside
the statement that writes it. Every server counts a shutdown down against the database
clock (ShutdownInSeconds), so the delay asked for is the
delay that elapses, whatever the requesting host's clock says. Locks in the same
statement, as SetShutdownAsync(long, DateTime?, CancellationToken) does.
A retry after a connection lost past the commit writes the deadline again from the retry's own instant: later by the reconnect, never earlier, and the returned deadline is the one the row holds.