Table of Contents

Class EmailQueueService

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

Outbound email queue service. Uses parameterized raw SQL for writes and EF Core compiled queries for reads, following the existing service pattern.

public sealed class EmailQueueService : BaseService<EmailQueueEntity>, IEmailQueueService
Inheritance
EmailQueueService
Implements
Inherited Members

Constructors

EmailQueueService(INpgsqlDbContextFactory)

public EmailQueueService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

Methods

DequeueNextAsync(string, CancellationToken)

Atomically claims and dequeues the next pending email using FOR UPDATE SKIP LOCKED. Safe for concurrent LoginServers. Throws DatabaseEntityNotFoundException if the queue is empty.

public Task<DatabaseResult<EmailQueueData>> DequeueNextAsync(string claimedBy, CancellationToken cancellationToken = default)

Parameters

claimedBy string

Identifier of the claiming server (e.g. server name).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<EmailQueueData>>

EnqueueAsync(string, string, string, string, EmailKind, CancellationToken)

Enqueues an email for asynchronous delivery.

public Task<DatabaseResult> EnqueueAsync(string recipientEmail, string recipientUsername, string subject, string body, EmailKind kind = EmailKind.Verification, CancellationToken cancellationToken = default)

Parameters

recipientEmail string

Recipient email address.

recipientUsername string

Associated account username.

subject string

Email subject line.

body string

Email body (plain-text or HTML).

kind EmailKind

What the mail is for. Defaults to Verification, which is both the column default and the meaning every row had before kinds existed, so a caller that does not care keeps the behaviour it always had. A password reset MUST pass PasswordReset: the drain stamps verification_email_sent_at after delivering a verification mail, and stamping it for a reset would end an unverified account's grace period — locking the player out of the game and the panel at the moment they recovered their password.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

HasPendingForUserAsync(string, EmailKind?, CancellationToken)

Returns true if the specified user already has an unsent email of kind in the queue. Prevents duplicate mail piling up.

public Task<DatabaseResult<bool>> HasPendingForUserAsync(string recipientUsername, EmailKind? kind = null, CancellationToken cancellationToken = default)

Parameters

recipientUsername string
kind EmailKind?
cancellationToken CancellationToken

Returns

Task<DatabaseResult<bool>>

Remarks

Pass the kind you are about to send. The queue carries more than one kind, so a blind check answers a different question than the caller asked: a pending password reset would suppress the verification mail an unverified player is waiting for. Null means any kind, and is almost never what a caller wants.

MarkFailedAsync(long, string, int, CancellationToken)

Marks an email delivery attempt as failed and increments the attempt counter.

public Task<DatabaseResult> MarkFailedAsync(long id, string error, int maxAttempts = 5, CancellationToken cancellationToken = default)

Parameters

id long
error string
maxAttempts int
cancellationToken CancellationToken

Returns

Task<DatabaseResult>

Remarks

While attempts remain the claim is released, which is what puts the message back in line for another login server to pick up. Once maxAttempts is reached the claim is kept, which takes the message out of circulation and leaves it in the queue with its error for an operator to investigate or retry by hand.

MarkSentAsync(long, CancellationToken)

Marks an email as successfully sent.

public Task<DatabaseResult> MarkSentAsync(long id, CancellationToken cancellationToken = default)

Parameters

id long
cancellationToken CancellationToken

Returns

Task<DatabaseResult>