Table of Contents

Class SmsQueueService

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

Outbound SMS queue service, the twin of EmailQueueService. Parameterized raw SQL for writes, EF for reads.

public sealed class SmsQueueService : BaseService<SmsQueueEntity>, ISmsQueueService
Inheritance
SmsQueueService
Implements
Inherited Members

Remarks

No provider is wired yet; the sender that drains this queue only logs. The claim, retry and give-up semantics are nonetheless the email queue's exactly, so a real provider slots into the drain without changing a caller.

Constructors

SmsQueueService(INpgsqlDbContextFactory)

Creates the service.

public SmsQueueService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

Methods

DequeueNextAsync(string, CancellationToken)

Atomically claims and dequeues the next pending message using FOR UPDATE SKIP LOCKED. Safe for concurrent senders. Returns a NOT_FOUND failure when the queue is empty.

public Task<DatabaseResult<SmsQueueData>> 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<SmsQueueData>>

EnqueueAsync(string, string, string, SmsKind, CancellationToken)

Enqueues a text message for asynchronous delivery.

public Task<DatabaseResult> EnqueueAsync(string recipientPhone, string recipientUsername, string body, SmsKind kind = SmsKind.Verification, CancellationToken cancellationToken = default)

Parameters

recipientPhone string

Recipient number in E.164 form: + then up to 15 digits.

recipientUsername string

Associated account username.

body string

Message text, at most 480 characters.

kind SmsKind

What the message is for. Defaults to Verification, the column default.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult>

HasPendingForUserAsync(string, SmsKind?, CancellationToken)

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

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

Parameters

recipientUsername string
kind SmsKind?
cancellationToken CancellationToken

Returns

Task<DatabaseResult<bool>>

Remarks

Pass the kind you are about to send. A blind check answers a different question: a pending notification would suppress the verification code a player is waiting for. Null means any kind, and is almost never what a caller wants.

MarkFailedAsync(long, string, int, CancellationToken)

Marks a 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 sender 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 a message as successfully sent.

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

Parameters

id long
cancellationToken CancellationToken

Returns

Task<DatabaseResult>