Skip to content

Commit ac2214d

Browse files
committed
update cron job
1 parent 32d8fe4 commit ac2214d

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

app/api/cron/route.tsx

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const fetchCache = "force-no-store";
33
import { publicURL } from "@/lib/utils";
44
import type { NextRequest } from "next/server";
55

6-
export function GET(request: NextRequest) {
6+
export async function GET(request: NextRequest) {
77
const authHeader = request.headers.get("authorization");
88
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
99
console.error("[CRON] Unauthorized request: invalid authorization header");
@@ -12,26 +12,34 @@ export function GET(request: NextRequest) {
1212
});
1313
}
1414
try {
15-
const url = `${publicURL()}/api/youtube/views`;
16-
console.log("[CRON] Triggering YouTube views update:", url);
17-
fetch(url, {
18-
method: "POST",
19-
headers: {
20-
authorization: `Bearer ${process.env.CRON_SECRET}`,
21-
"Cache-Control": "no-cache",
22-
},
23-
})
24-
.then((res) => {
25-
if (!res.ok) {
26-
console.error("[CRON] Failed to trigger YouTube views:", res.status);
27-
} else {
28-
console.log("[CRON] Successfully triggered YouTube views update.");
29-
}
30-
})
31-
.catch((err) => {
32-
console.error("[CRON] Error triggering YouTube views:", err);
15+
let totalUpdated = 0;
16+
let keepGoing = true;
17+
let loopCount = 0;
18+
const maxLoops = 200; // Safety: 200*10 = 2000 videos max per day
19+
while (keepGoing && loopCount < maxLoops) {
20+
const url = `${publicURL()}/api/youtube/views`;
21+
const res = await fetch(url, {
22+
method: "POST",
23+
headers: {
24+
authorization: `Bearer ${process.env.CRON_SECRET}`,
25+
"Cache-Control": "no-cache",
26+
},
3327
});
34-
return Response.json({ success: true });
28+
if (!res.ok) {
29+
console.error("[CRON] Failed to trigger YouTube views:", res.status);
30+
break;
31+
}
32+
const json = await res.json();
33+
if (json && json.updatedCount) {
34+
totalUpdated += json.updatedCount;
35+
}
36+
// If no more tasks, stop
37+
if (!json || !json.updatedCount || json.updatedCount === 0) {
38+
keepGoing = false;
39+
}
40+
loopCount++;
41+
}
42+
return Response.json({ success: true, totalUpdated, loops: loopCount });
3543
} catch (err) {
3644
console.error("[CRON] Unexpected error:", err);
3745
return Response.json(

vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"crons": [
44
{
55
"path": "/api/cron",
6-
"schedule": "*/3 * * * *"
6+
"schedule": "0 0 * * *"
77
}
88
]
99
}

0 commit comments

Comments
 (0)