Class SmsQueueService
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
dbContextFactoryINpgsqlDbContextFactory
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
claimedBystringIdentifier of the claiming server (e.g. server name).
cancellationTokenCancellationTokenCancellation token.
Returns
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
recipientPhonestringRecipient number in E.164 form:
+then up to 15 digits.recipientUsernamestringAssociated account username.
bodystringMessage text, at most 480 characters.
kindSmsKindWhat the message is for. Defaults to Verification, the column default.
cancellationTokenCancellationTokenCancellation token.
Returns
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
recipientUsernamestringkindSmsKind?cancellationTokenCancellationToken
Returns
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
idlongerrorstringmaxAttemptsintcancellationTokenCancellationToken
Returns
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
idlongcancellationTokenCancellationToken