The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
updated script
Steven Teydf10069c14d2
diff --git a/apps/web/scripts/sync-expired-links.ts b/apps/web/scripts/sync-expired-links.tsindex 5570a45eb5..f521d577e6 100644--- a/apps/web/scripts/sync-expired-links.ts+++ b/apps/web/scripts/sync-expired-links.ts@@ -1,15 +1,13 @@import "dotenv-flow/config";import prisma from "@/lib/prisma";-import { redis } from "./utils";-import { qstash } from "@/lib/cron";--const anchor = 2250;+import { redis, chunk } from "./utils";+import { RedisLinkProps } from "@/lib/types";async function main() {- const expiredLinks = await prisma.link.findMany({+ const allExpiredLinks = await prisma.link.findMany({where: {- expiresAt: {- gt: new Date("2023-12-20T02:10:00.000Z"),+ NOT: {+ expiresAt: null,},},select: {@@ -21,39 +19,34 @@ async function main() {orderBy: {expiresAt: "asc",},- skip: anchor,+ skip: 2000,});- const pipeline = redis.pipeline();- // redisLinks.forEach((link, idx) => {- // const { domain, key } = expiredLinks[idx];- // console.log({ domain, key });- // pipeline.set(`${domain}:${key}`, {- // ...link,- // expired: true,- // });- // });-- // await Promise.all(- // expiredLinks.map((link) => {- // const { domain, key } = link;- // pipeline.persist(`${domain}:${key}`);- // qstash.publishJSON({- // url: "https://app.dub.co/api/callback/expire",- // delay:- // (new Date(link.expiresAt!).getTime() - new Date().getTime()) / 1000,- // body: {- // linkId: link.id,- // },- // });- // }),- // );-- // await pipeline.exec();-- console.log(`Synced ${expiredLinks.length} links`);- // table log the last 10 links- console.table(expiredLinks.slice(-10));+ let count = 0;+ const chunks = chunk(allExpiredLinks, 100);++ for (const chunk of chunks) {+ const redisLinks = await redis.mget<RedisLinkProps[]>(+ chunk.map((link) => `${link.domain}:${link.key}`),+ );++ const pipeline = redis.pipeline();+ redisLinks.forEach((link, idx) => {+ const { domain, key, expiresAt } = chunk[idx];+ // @ts-ignore (old version)+ const { expired, ...rest } = link || {};+ pipeline.set(`${domain}:${key}`, {+ ...rest,+ expiresAt,+ });+ });++ await pipeline.exec();++ count += chunk.length;++ console.log(`Synced ${count} expired links...`);+ }}main();diff --git a/apps/web/scripts/utils.ts b/apps/web/scripts/utils.tsindex dd424a46e4..0f48fcfd04 100644--- a/apps/web/scripts/utils.ts+++ b/apps/web/scripts/utils.ts@@ -29,3 +29,17 @@ export function linkConstructor({if (noDomain) return `/${key}`;return pretty ? link.replace(/^https?:\/\//, "") : link;}++export const chunk = <T>(array: T[], chunk_size: number): T[][] => {+ return array.reduce((resultArray, item, index) => {+ const chunkIndex = Math.floor(index / chunk_size);++ if (!resultArray[chunkIndex]) {+ resultArray[chunkIndex] = []; // start a new chunk+ }++ resultArray[chunkIndex].push(item);++ return resultArray;+ }, [] as T[][]);+};