Send each Discord announcement exactly once
Use SADD's return value to announce new devlog posts once, and retry when Discord fails.
RedisDiscordWebhooks
New devlog posts are announced in Discord by a job that runs every 5 minutes. To post each one only once, the job adds the post's slug to a Redis set. SADD returns 1 only when the member is new, so that's the signal to announce it. If the webhook call fails, the slug comes back out of the set and the next run tries again.
ts
import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
const ANNOUNCED = "devlog:announced";
export async function announce(post: { slug: string; title: string; url: string }) {
// 1 the first time this slug is added, 0 every time after.
const isNew = (await redis.sadd(ANNOUNCED, post.slug)) === 1;
if (!isNew) return;
const res = await fetch(process.env.DISCORD_WEBHOOK_URL!, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
allowed_mentions: { parse: [] }, // never ping @everyone from content
embeds: [{ title: post.title, url: post.url, color: 0x4ade80 }],
}),
signal: AbortSignal.timeout(6000),
}).catch(() => null);
// Discord didn't take it: forget we tried, so the next run retries.
if (!res?.ok) await redis.srem(ANNOUNCED, post.slug);
}Claiming before sending means two overlapping runs can't both post it. The cost is that a crash between SADD and the webhook call skips that post. For announcements that's the right trade-off.