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.

Retry failed domain renewals later through QStash

Rafi929799046f6d
diff --git a/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts b/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
index 0aa8d91759..423614605e 100644
--- a/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/renewal-succeeded/route.ts
@@ -1,10 +1,17 @@
import { parseRegisteredDomainSlugs } from "@/lib/api/domains/is-domain-registration-invoice";
+import { qstash } from "@/lib/cron";
import { withCron } from "@/lib/cron/with-cron";
import { setRenewOption } from "@/lib/dynadot/set-renew-option";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import DomainRenewed from "@dub/email/templates/domain-renewed";
-import { chunk, log, pluralize, sleep } from "@dub/utils";
+import {
+ APP_DOMAIN_WITH_NGROK,
+ chunk,
+ log,
+ pluralize,
+ sleep,
+} from "@dub/utils";
import { RegisteredDomain } from "@prisma/client";
import { addDays, startOfDay } from "date-fns";
import * as z from "zod/v4";
@@ -16,13 +23,17 @@ export const dynamic = "force-dynamic";
const inputSchema = z.object({
invoiceId: z.string(),
+ attempt: z.number().int().min(1).default(1),
});
const BATCH_SIZE = 5;
+const MAX_ATTEMPTS = 3;
+const RETRY_DELAY_SECONDS = 60 * 60;
// POST /api/cron/domains/renewal-succeeded - Renews domains for a given invoice
export const POST = withCron(async ({ rawBody }) => {
- const { invoiceId } = inputSchema.parse(JSON.parse(rawBody));
+ const { invoiceId, attempt } = inputSchema.parse(JSON.parse(rawBody));
+ const isLastAttempt = attempt >= MAX_ATTEMPTS;
const invoice = await prisma.invoice.findUnique({
where: {
@@ -101,6 +112,7 @@ export const POST = withCron(async ({ rawBody }) => {
const renewSucceeded = await setRenewOption({
domain: domain.slug,
autoRenew: true,
+ logFailure: isLastAttempt,
});
return {
@@ -158,6 +170,27 @@ export const POST = withCron(async ({ rawBody }) => {
domainsFailed.map(({ slug }) => slug).join(", "),
);
+ // Dynadot failures are mostly short outages, so retry the invoice later instead of failing now.
+ // The renewed domains are skipped on the next attempt, since their expiresAt is already extended.
+ if (!isLastAttempt) {
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/renewal-succeeded`,
+ delay: RETRY_DELAY_SECONDS,
+ deduplicationId: `domain-renewal-${invoiceId}-attempt-${attempt + 1}`,
+ body: {
+ invoiceId,
+ attempt: attempt + 1,
+ },
+ });
+
+ return logAndRespond(
+ `Failed to renew ${domainsFailed.length} domains for invoice ${invoiceId} (attempt ${attempt} of ${MAX_ATTEMPTS}). Retrying in ${RETRY_DELAY_SECONDS / 60} minutes.`,
+ {
+ logLevel: "warn",
+ },
+ );
+ }
+
await log({
message: `Domain renewal partially failed for invoice ${invoiceId}. See the logs for more details.`,
type: "errors",
diff --git a/apps/web/lib/dynadot/set-renew-option.ts b/apps/web/lib/dynadot/set-renew-option.ts
index 597db4755e..34db5d25fd 100644
--- a/apps/web/lib/dynadot/set-renew-option.ts
+++ b/apps/web/lib/dynadot/set-renew-option.ts
@@ -4,9 +4,12 @@ import { dynadotClient } from "./client";
export const setRenewOption = async ({
domain,
autoRenew,
+ logFailure = true,
}: {
domain: string;
autoRenew: boolean;
+ // Set to false when the caller retries later and alerts on its own
+ logFailure?: boolean;
}): Promise<boolean> => {
try {
const parsedResponse = await dynadotClient.setRenewOption({
@@ -37,11 +40,13 @@ export const setRenewOption = async ({
return true;
} catch (error) {
- await log({
- message: `Failed to set renew option for ${domain}: ${error instanceof Error ? error.message : String(error)}`,
- type: "errors",
- mention: true,
- });
+ if (logFailure) {
+ await log({
+ message: `Failed to set renew option for ${domain}: ${error instanceof Error ? error.message : String(error)}`,
+ type: "errors",
+ mention: true,
+ });
+ }
console.error(error);