Skip to content

Commit f1ebee0

Browse files
hhvrcCopilot
andauthored
Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 7fdd173 commit f1ebee0

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

API.IntegrationTests/Tests/EmailOutboxPersistenceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public async Task DueForDelivery_RunsTheJobsRawSql_AgainstTheRealSchema()
103103
// of truth the job uses) against real Postgres: the email_outbox table name, its column names, the
104104
// email_status enum, LIMIT, FOR UPDATE SKIP LOCKED, and SELECT * -> full entity materialization
105105
// (jsonb payload included). A schema rename breaks this test rather than only the Cron host at runtime.
106-
var claimed = await db.EmailOutbox.DueForDelivery(EmailOutboxQueries.ClaimBatchSize).ToListAsync();
106+
var claimed = await db.EmailOutbox.DueForDelivery(1).ToListAsync();
107107

108108
await Assert.That(claimed.Any(m => m.Id == id)).IsTrue();
109109
}

Common/OpenShockDb/EmailOutboxQueries.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ public static class EmailOutboxQueries
1919
/// capped at <paramref name="batchSize"/>, locked with <c>FOR UPDATE SKIP LOCKED</c> so concurrent
2020
/// runs take disjoint batches. Run it inside an explicit transaction to hold the locks for the claim.
2121
/// </summary>
22-
public static IQueryable<EmailOutboxMessage> DueForDelivery(this DbSet<EmailOutboxMessage> outbox, int batchSize) =>
23-
outbox.FromSql(
24-
$"""
25-
SELECT * FROM email_outbox
26-
WHERE next_attempt_at <= now()
27-
AND (status = {EmailStatus.Pending} OR status = {EmailStatus.Sending})
28-
ORDER BY next_attempt_at
29-
LIMIT {batchSize}
30-
FOR UPDATE SKIP LOCKED
31-
""");
22+
public static IQueryable<EmailOutboxMessage> DueForDelivery(this DbSet<EmailOutboxMessage> outbox, int batchSize)
23+
{
24+
if (batchSize <= 0) throw new ArgumentOutOfRangeException(nameof(batchSize));
25+
26+
return outbox.FromSql(
27+
$"""
28+
SELECT * FROM email_outbox
29+
WHERE next_attempt_at <= now()
30+
AND (status = {EmailStatus.Pending} OR status = {EmailStatus.Sending})
31+
ORDER BY next_attempt_at
32+
LIMIT {batchSize}
33+
FOR UPDATE SKIP LOCKED
34+
""");
3235
}

0 commit comments

Comments
 (0)