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
discordClientDiscordSocketClientThe Discord socket client.
dbContextFactoryNpgsqlDbContextFactoryFactory for creating database contexts.
chatServiceIChatServiceChat persistence; the relay reads new rows through it.
loggerILogger<ChatPollingService>Logger instance.
configurationIConfigurationApplication configuration.
dynamicChannelManagerDynamicChannelManagerServiceService managing dynamic Discord channels.
bridgeBanServiceBridgeBanServiceaccountLinkingServiceAccountLinkingServicerelayPolicyChatRelayPolicyAllowlist 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
cancellationTokenCancellationTokenIndicates that the start process has been aborted.
Returns
StopAsync(CancellationToken)
Triggered when the application host is performing a graceful shutdown.
public Task StopAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenIndicates that the shutdown process should no longer be graceful.