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.

Sync the index when partner links change in bulk

Rafi67bf07903234
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index e5faf38ef3..4cd465065a 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -1,3 +1,4 @@
+import { queuePartnerSearchSyncForLinks } from "@/lib/api/partners/queue-partner-search-sync";
import { prisma } from "@/lib/prisma";
import { ProcessedLinkProps } from "@/lib/types";
import { publishWorkspaceLinksUsageEvent } from "@/lib/upstash/redis-streams/workspace-links-usage";
@@ -235,6 +236,9 @@ export async function bulkCreateLinks({
linksCount: links.length,
timestamp: new Date().toISOString(),
}),
+ // Default delay rather than the link delay: a new short link is how
+ // people go looking for that partner, so it is worth indexing promptly.
+ queuePartnerSearchSyncForLinks(createdLinksData),
]),
);
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index e23b75a8a8..357cffd470 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -1,6 +1,6 @@
import {
PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
- queuePartnerSearchSync,
+ queuePartnerSearchSyncForLinks,
} from "@/lib/api/partners/queue-partner-search-sync";
import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";
import { prisma } from "@/lib/prisma";
@@ -65,26 +65,11 @@ export async function bulkDeleteLinks(
storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),
),
- // Grouped by program so each queued payload carries the scope the job
- // needs to resolve enrollments, rather than one payload per link.
- ...Array.from(
- links.reduce((byProgram, { programId, partnerId }) => {
- if (!programId || !partnerId) {
- return byProgram;
- }
-
- const partnerIds = byProgram.get(programId) ?? new Set<string>();
- partnerIds.add(partnerId);
-
- return byProgram.set(programId, partnerIds);
- }, new Map<string, Set<string>>()),
- ([programId, partnerIds]) =>
- queuePartnerSearchSync({
- partnerIds: [...partnerIds],
- programId,
- delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
- }),
- ),
+ // The enrollment outlives its links, so this re-serializes the document
+ // without them rather than removing it.
+ queuePartnerSearchSyncForLinks(links, {
+ delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
+ }),
]),
);
}
diff --git a/apps/web/lib/api/links/bulk-update-links.ts b/apps/web/lib/api/links/bulk-update-links.ts
index 9225636760..b618f33ef5 100644
--- a/apps/web/lib/api/links/bulk-update-links.ts
+++ b/apps/web/lib/api/links/bulk-update-links.ts
@@ -1,3 +1,7 @@
+import {
+ PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
+ queuePartnerSearchSyncForLinks,
+} from "@/lib/api/partners/queue-partner-search-sync";
import { prisma } from "@/lib/prisma";
import { isNotHostedImage, storage } from "@/lib/storage";
import { bulkUpdateLinksBodySchema } from "@/lib/zod/schemas/links";
@@ -115,6 +119,12 @@ export async function bulkUpdateLinks(
propagateBulkLinkChanges({
links: updatedLinks,
}),
+ // A bulk edit can move the destination URL, and the key or domain behind
+ // the short link. Queued on the link delay: the job re-reads the whole
+ // document, so nothing else goes stale by waiting.
+ queuePartnerSearchSyncForLinks(updatedLinks, {
+ delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
+ }),
// if proxy is true and image is not stored in R2, upload image to R2
proxy &&
image &&
diff --git a/apps/web/lib/api/partners/queue-partner-search-sync.ts b/apps/web/lib/api/partners/queue-partner-search-sync.ts
index 710e7d1f2e..e4f99b97db 100644
--- a/apps/web/lib/api/partners/queue-partner-search-sync.ts
+++ b/apps/web/lib/api/partners/queue-partner-search-sync.ts
@@ -157,3 +157,45 @@ export async function queuePartnerSearchSync({
);
}
}
+
+interface PartnerSearchSyncLink {
+ programId: string | null;
+ partnerId: string | null;
+}
+
+/**
+ * Queues a sync for every partner whose links changed.
+ *
+ * A link reaches the document through its (programId, partnerId) pair, so this
+ * groups by program and queues one payload per program rather than one per
+ * link. Links carrying neither are ordinary workspace links and hold nothing
+ * the document indexes, which is why the bulk link helpers can call this
+ * unconditionally: an import of a hundred thousand workspace links queues
+ * nothing at all.
+ */
+export async function queuePartnerSearchSyncForLinks(
+ links: PartnerSearchSyncLink[],
+ { delay }: { delay?: number } = {},
+) {
+ const partnersByProgram = new Map<string, Set<string>>();
+
+ for (const { programId, partnerId } of links) {
+ if (!programId || !partnerId) {
+ continue;
+ }
+
+ const partners = partnersByProgram.get(programId) ?? new Set<string>();
+ partners.add(partnerId);
+ partnersByProgram.set(programId, partners);
+ }
+
+ await Promise.all(
+ Array.from(partnersByProgram, ([programId, partnerIds]) =>
+ queuePartnerSearchSync({
+ partnerIds: [...partnerIds],
+ programId,
+ ...(delay !== undefined && { delay }),
+ }),
+ ),
+ );
+}
diff --git a/apps/web/tests/partners/queue-partner-search-sync.test.ts b/apps/web/tests/partners/queue-partner-search-sync.test.ts
index 59d047b51c..4d0676a8b6 100644
--- a/apps/web/tests/partners/queue-partner-search-sync.test.ts
+++ b/apps/web/tests/partners/queue-partner-search-sync.test.ts
@@ -3,6 +3,7 @@ import {
PARTNER_SEARCH_SYNC_DELAY_SECONDS,
QSTASH_DEDUPLICATION_WINDOW_SECONDS,
queuePartnerSearchSync,
+ queuePartnerSearchSyncForLinks,
} from "@/lib/api/partners/queue-partner-search-sync";
import { beforeEach, describe, expect, it, vi } from "vitest";
@@ -189,3 +190,79 @@ describe("queuePartnerSearchSync", () => {
}
});
});
+
+describe("queuePartnerSearchSyncForLinks", () => {
+ beforeEach(() => {
+ mocks.getPartnerSearchProvider
+ .mockReset()
+ .mockReturnValue({ name: "turbopuffer" });
+ mocks.dispatchBatch.mockReset().mockResolvedValue({
+ published: 1,
+ deferred: 0,
+ failed: 0,
+ results: [],
+ });
+ });
+
+ // The bulk link helpers call this on every write, including workspace-link
+ // imports of a hundred thousand rows. Those carry no partner, so this must
+ // cost nothing rather than being guarded at each call site.
+ it("queues nothing for links with no program or partner", async () => {
+ await queuePartnerSearchSyncForLinks([
+ { programId: null, partnerId: null },
+ { programId: "prog_1", partnerId: null },
+ { programId: null, partnerId: "pn_1" },
+ ]);
+
+ expect(mocks.dispatchBatch).not.toHaveBeenCalled();
+ });
+
+ it("queues one payload per program, not per link", async () => {
+ await queuePartnerSearchSyncForLinks([
+ { programId: "prog_1", partnerId: "pn_1" },
+ { programId: "prog_1", partnerId: "pn_2" },
+ { programId: "prog_1", partnerId: "pn_1" },
+ { programId: "prog_2", partnerId: "pn_3" },
+ ]);
+
+ expect(mocks.dispatchBatch).toHaveBeenCalledTimes(2);
+
+ const queued = mocks.dispatchBatch.mock.calls.map(([payloads]) => payloads[0]);
+
+ expect(queued).toEqual([
+ { type: "partners", partnerIds: ["pn_1", "pn_2"], programId: "prog_1" },
+ { type: "partners", partnerIds: ["pn_3"], programId: "prog_2" },
+ ]);
+ });
+
+ it("passes the delay through so link edits can batch harder than creations", async () => {
+ await queuePartnerSearchSyncForLinks(
+ [{ programId: "prog_1", partnerId: "pn_1" }],
+ { delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS },
+ );
+
+ const [, getOptions] = mocks.dispatchBatch.mock.calls[0];
+
+ expect(getOptions({ type: "partners", partnerIds: ["pn_1"] }, 0).delay).toBe(
+ PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,
+ );
+ });
+
+ it("defaults to the interactive delay when none is given", async () => {
+ await queuePartnerSearchSyncForLinks([
+ { programId: "prog_1", partnerId: "pn_1" },
+ ]);
+
+ const [, getOptions] = mocks.dispatchBatch.mock.calls[0];
+
+ expect(getOptions({ type: "partners", partnerIds: ["pn_1"] }, 0).delay).toBe(
+ PARTNER_SEARCH_SYNC_DELAY_SECONDS,
+ );
+ });
+
+ it("queues nothing for an empty link set", async () => {
+ await queuePartnerSearchSyncForLinks([]);
+
+ expect(mocks.dispatchBatch).not.toHaveBeenCalled();
+ });
+});