Table of Contents

Class ChatPollingService

Namespace
FishMMO.DiscordBot.Services
Assembly
FishMMO-DiscordBot.dll

Background service that polls the game database for new chat messages and forwards them to the appropriate Discord channels. Uses a reentrancy guard to prevent overlapping polls.

public class ChatPollingService : IHostedService, IDisposable
Inheritance
ChatPollingService
Implements
Inherited Members

Remarks

A window, not an ID cursor. The relay used to read id > last id seen. Chat IDs are taken when a row is INSERTed and the row becomes visible when its transaction commits, so a row that committed after a higher ID had already been read was skipped for good (hot-path audit H5). It now reads through ChatReadWindow, the same window the scene servers' pump keeps: each read starts a commit window behind what it has settled and skips by ID what it has already handled, so no row is skipped and none is relayed twice.

Bounded. Each poll reads at most FishMMO.DiscordBot.Services.ChatPollingService.RelayMaxPagesPerPoll pages of FishMMO.DiscordBot.Services.ChatPollingService.RelayPageSize rows. A relay that has fallen behind — Discord was unreachable for a while — catches up over several polls instead of in one query of unbounded size.

No replay at startup. The first read starts at the database's own "now", and nothing stamped before it is ever read, so a restarted bot does not republish history.

Constructors

ChatPollingService(DiscordSocketClient, NpgsqlDbContextFactory, IChatService, ILogger<ChatPollingService>, IConfiguration, DynamicChannelManagerService, BridgeBanService, AccountLinkingService, ChatRelayPolicy)

Initializes a new instance of the ChatPollingService class.

public ChatPollingService(DiscordSocketClient discordClient, NpgsqlDbContextFactory dbContextFactory, IChatService chatService, ILogger<ChatPollingService> logger, IConfiguration configuration, DynamicChannelManagerService dynamicChannelManager, BridgeBanService bridgeBanService, AccountLinkingService accountLinkingService, ChatRelayPolicy relayPolicy)

Parameters

discordClient DiscordSocketClient

The Discord socket client.

dbContextFactory NpgsqlDbContextFactory

Factory for creating database contexts.

chatService IChatService

Chat persistence; the relay reads new rows through it.

logger ILogger<ChatPollingService>

Logger instance.

configuration IConfiguration

Application configuration.

dynamicChannelManager DynamicChannelManagerService

Service managing dynamic Discord channels.

bridgeBanService BridgeBanService
accountLinkingService AccountLinkingService
relayPolicy ChatRelayPolicy

Allowlist deciding which game channels may reach Discord.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

StartAsync(CancellationToken)

Triggered when the application host is ready to start the service.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the start process has been aborted.

Returns

Task

A Task that represents the asynchronous Start operation.

StopAsync(CancellationToken)

Triggered when the application host is performing a graceful shutdown.

public Task StopAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns

Task

A Task that represents the asynchronous Stop operation.