Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Gate transactional campaign queueing to the 12h UTC window and mark stuck sending campaigns as sent.

Kiran K723005ce5a71
diff --git a/apps/web/app/(ee)/api/cron/campaigns/queue-scheduled/route.ts b/apps/web/app/(ee)/api/cron/campaigns/queue-scheduled/route.ts
index 6f3d4ebf06..99cd26be48 100644
--- a/apps/web/app/(ee)/api/cron/campaigns/queue-scheduled/route.ts
+++ b/apps/web/app/(ee)/api/cron/campaigns/queue-scheduled/route.ts
@@ -55,6 +55,12 @@ export const GET = withCron(async () => {
);
});
+// First 5 minutes of 00:00/12:00 UTC. QStash dedup lasts 10 minutes, so this
+// absorbs Vercel cron jitter without leaking a second publish after expiry.
+function isTransactionalTick(now: Date) {
+ return now.getUTCHours() % 12 === 0 && now.getUTCMinutes() < 5;
+}
+
async function queueTransactionalCampaigns(now: Date) {
// Matches the 12h enrollment window in executeSendCampaignWorkflow.
// 5-minute window absorbs Vercel cron jitter; QStash dedup (10 min) collapses extra publishes.
diff --git a/apps/web/scripts/migrations/delete-campaign-qstash-schedules.ts b/apps/web/scripts/migrations/delete-campaign-qstash-schedules.ts
index f054a62a7e..3eeed52a2c 100644
--- a/apps/web/scripts/migrations/delete-campaign-qstash-schedules.ts
+++ b/apps/web/scripts/migrations/delete-campaign-qstash-schedules.ts
@@ -1,11 +1,17 @@
+// @ts-ignore
import "dotenv-flow/config";
import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
-import { CampaignType } from "@prisma/client";
+import { CampaignStatus, CampaignType } from "@prisma/client";
-// Remove the existing schedules from QStash
async function main() {
+ await deleteTransactionalCampaignQstashSchedules();
+ await markStuckSendingCampaignsAsSent();
+}
+
+// Remove the existing schedules from QStash
+async function deleteTransactionalCampaignQstashSchedules() {
const campaigns = await prisma.campaign.findMany({
where: {
type: CampaignType.transactional,
@@ -51,4 +57,45 @@ async function main() {
console.log(`Done. deleted=${deleted} skipped=${skipped} failed=${failed}`);
}
+// Marketing campaigns that started sending in mid-March 2026 and never
+// flipped to `sent` after the next QStash batch failed (~665 / ~673 emails already
+// delivered). Mark these campaigns as `sent` so they leave the in-progress UI and a stray QStash retry cannot continue.
+async function markStuckSendingCampaignsAsSent() {
+ const stuckSendingCampaignIds = [
+ "cmp_1KKSXA2G25J2KBNGZQERMAPEX",
+ "cmp_1KKZ458FAK2PY9W9677TT3VS2",
+ ];
+
+ const campaigns = await prisma.campaign.findMany({
+ where: {
+ id: {
+ in: stuckSendingCampaignIds,
+ },
+ },
+ select: {
+ id: true,
+ name: true,
+ status: true,
+ scheduledAt: true,
+ updatedAt: true,
+ },
+ });
+
+ console.table(campaigns);
+
+ const result = await prisma.campaign.updateMany({
+ where: {
+ id: {
+ in: stuckSendingCampaignIds,
+ },
+ status: CampaignStatus.sending,
+ },
+ data: {
+ status: CampaignStatus.sent,
+ },
+ });
+
+ console.log(`Marked ${result.count} stuck sending campaigns as sent.`);
+}
+
main();