fix(news): retry rolled-back scheduled publication deadlocks

This commit is contained in:
Simo committed 2026-09-13 20:23:29 +02:00
1 parent 9a2d73a6f6
commit 275a574203
2 files changed
+129 -3

No files matched your search

File diff suppressed because it is too large. Load diff
+33 -2
View File
@@ -7,11 +7,34 @@ import { db } from "@/lib/db";
import { logger } from "@/lib/logger"; import { logger } from "@/lib/logger";
import { invalidateNewsCache } from "./news-cache"; import { invalidateNewsCache } from "./news-cache";
/** MySQL rolls the entire victim transaction back on this specific error. */
function isDeadlock(error: unknown): boolean {
let cause = error;
for (
let depth = 0;
cause && typeof cause === "object" && depth < 5;
depth++
) {
const record = cause as {
code?: unknown;
errno?: unknown;
cause?: unknown;
};
if (record.code === "ER_LOCK_DEADLOCK" || record.errno === 1213)
return true;
cause = record.cause;
}
return false;
}
/** Publish a bounded batch and persist its refresh work before committing. */ /** Publish a bounded batch and persist its refresh work before committing. */
export async function publishDueArticles(now = new Date()): Promise<number> { export async function publishDueArticles(now = new Date()): Promise<number> {
// Match Drizzle's TIMESTAMP writes; raw Date parameters use the host timezone. // Match Drizzle's TIMESTAMP writes; raw Date parameters use the host timezone.
const utcNow = now.toISOString().slice(0, -1).replace("T", " "); const utcNow = now.toISOString().slice(0, -1).replace("T", " ");
const published = await db.transaction(async (tx) => { let published: number;
for (let attempt = 0; ; attempt++) {
try {
published = await db.transaction(async (tx) => {
const [rows] = await tx.execute( const [rows] = await tx.execute(
sql`SELECT CAST(id AS CHAR) AS id,user_id AS userId,title,publish_at AS publishAt FROM website_articles WHERE status='scheduled' AND publish_at<=${utcNow} ORDER BY publish_at,id LIMIT 100 FOR UPDATE`, sql`SELECT CAST(id AS CHAR) AS id,user_id AS userId,title,publish_at AS publishAt FROM website_articles WHERE status='scheduled' AND publish_at<=${utcNow} ORDER BY publish_at,id LIMIT 100 FOR UPDATE`,
); );
@@ -25,7 +48,9 @@ export async function publishDueArticles(now = new Date()): Promise<number> {
const [result] = await tx.execute( const [result] = await tx.execute(
sql`UPDATE website_articles SET status='published',published_at=${utcNow},updated_at=${utcNow} WHERE id=${article.id} AND status='scheduled' AND publish_at<=${utcNow}`, sql`UPDATE website_articles SET status='published',published_at=${utcNow},updated_at=${utcNow} WHERE id=${article.id} AND status='scheduled' AND publish_at<=${utcNow}`,
); );
if ((result as unknown as { affectedRows: number }).affectedRows !== 1) if (
(result as unknown as { affectedRows: number }).affectedRows !== 1
)
continue; continue;
const operationId = randomUUID(); const operationId = randomUUID();
const hash = operationHash({ const hash = operationHash({
@@ -41,6 +66,12 @@ export async function publishDueArticles(now = new Date()): Promise<number> {
} }
return published; return published;
}); });
break;
} catch (error) {
// Retry only known rollbacks, never timeouts or an unknown commit outcome.
if (attempt >= 2 || !isDeadlock(error)) throw error;
}
}
if (published > 0) { if (published > 0) {
try { try {
await invalidateNewsCache(); await invalidateNewsCache();