gpt4 book ai didi

Vercel Cron Job not firing with Next 13 Route API(Vercel Cron作业未使用下13路由API触发)

转载 作者:bug小助手 更新时间:2023-10-24 20:20:23 33 4
gpt4 key购买 nike



I am using Next 13 app dir.

我正在使用Next 13应用程序目录。


I have a route.ts file:

我有一个route.ts文件:


app/api/cron/set-completed-goals/route.ts

import { prisma } from '@/lib/prisma';
import { NextResponse } from 'next/server';
export async function GET() {
const users = await prisma.user.findMany();

for (const user of users) {
const goals = await prisma.goal.findMany({
where: { userId: user.id },
});

for (const goal of goals) {
if (goal?.percentage ?? 0 >= 100) {
await prisma.$transaction([
prisma.user.update({
where: { id: user.id },
data: {
completedGoals: [...user.completedGoals, goal.id],
},
}),
prisma.goal.update({
where: { id: goal.id },
data: { percentage: 0 },
}),
]);
}
}
}
return NextResponse.json({ message: 'Completed goals updated' });
}


And a vercel.json:

和一个vercel.json:


{
"crons": [
{
"path": "/api/cron/set-completed-goals",
"schedule": "0 0 * * *"
}
]
}


When I fire the function in my localhost manually it works as intended.

当我手动启动本地主机中的函数时,它会按预期工作。


However when I manually fire the cron job on vercel, I see in the logs:

但是,当我在Vercel上手动启动cron作业时,我在日志中看到:


200
[GET] /api/cron/set-completed-goals

It seems to be returning a 200, but nothing is actually changing in my database.

它似乎返回了200,但在我的数据库中实际上没有任何变化。


I am new to cron jobs, and it's not obvious what is wrong.

我还是个新手,现在还不清楚到底出了什么问题。


Any help is greatly appreciated.

如有任何帮助,我们不胜感激。


更多回答
优秀答案推荐

It started to work now.

它现在开始起作用了。


I believe it wasnt working properly because of Vercel limitations for Hobby plans, and somehow went over my usage.

我相信它不能正常工作是因为Vercel对Hobby Plans的限制,并且不知何故超过了我的使用。


https://vercel.com/blog/cron-jobs#limits-of-cron-jobs-and-vercel-functions

Https://vercel.com/blog/cron-jobs#limits-of-cron-jobs-and-vercel-functions


Wish they had better logs or some sort of limit usage view to reflect what goes on, but it is what it is.

希望他们有更好的日志或某种限制使用情况视图来反映正在发生的事情,但事实就是这样。



I faced the same issue but i did solve it with this code.

我也遇到过同样的问题,但我确实用下面的代码解决了这个问题。




export const revalidate = 0
export async function GET(req: Request) {
// your db logic
return NextResponse.json({
result:result
});
}




I hove this code will help you...

我希望这个代码能帮助你..。


更多回答

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com