-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathroute.ts
More file actions
79 lines (68 loc) · 2.25 KB
/
Copy pathroute.ts
File metadata and controls
79 lines (68 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { ahrefsClient } from "@/lib/ahrefs/client";
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { chunk, getDomainWithoutWWW } from "@dub/utils";
import { PlatformType } from "@prisma/client";
import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
const BATCH_SIZE = 60;
const CONCURRENCY = 10;
/**
* This route is used to update domain rating (DR) for verified website partners using the Ahrefs free API
* Runs once a minute (cron expression: * * * * *), processing up to 60 platforms per run (Ahrefs rate limit)
* GET /api/cron/partner-platforms/website
*/
export const GET = withCron(async () => {
const websites = await prisma.partnerPlatform.findMany({
where: {
type: PlatformType.website,
verifiedAt: {
not: null,
},
},
take: BATCH_SIZE,
orderBy: {
lastCheckedAt: "asc",
},
});
if (websites.length === 0) {
return logAndRespond("No verified website platforms found.");
}
const websiteChunks = chunk(websites, CONCURRENCY);
for (const websiteChunk of websiteChunks) {
await Promise.allSettled(
websiteChunk.map(async (website) => {
const target =
getDomainWithoutWWW(website.identifier) ?? website.identifier;
if (!target) {
console.error(
`Invalid website identifier for partner platform ${website.id}: ${website.identifier}`,
);
return;
}
try {
const domainRating = await ahrefsClient.getDomainRating(target);
await prisma.partnerPlatform.update({
where: {
id: website.id,
},
data: {
subscribers: domainRating,
lastCheckedAt: new Date(),
},
});
console.log(`Updated domain rating for ${target}`, {
domainRating,
previousDomainRating: Number(website.subscribers),
domainRatingChanged: Number(website.subscribers) !== domainRating,
});
} catch (error) {
console.error(`Error updating domain rating for ${target}:`, error);
}
}),
);
}
return logAndRespond(
`Processed domain ratings for ${websites.length} websites.`,
);
});