Vercel Cron作业未使用Next 13 Route API触发

rlcwz9us  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(109)

我正在使用Next 13 App
我有一个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' });
}

和一个vercel.json:

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

当我在我的localhost中手动触发该函数时,它会按预期工作。
然而,当我在vercel上手动启动cron作业时,我在日志中看到:

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

它似乎返回了200,但实际上我的数据库中没有任何变化。
我是cron jobs的新手,不知道哪里出了问题。
任何帮助都是非常感谢的。

dpiehjr4

dpiehjr41#

它现在开始工作了。
我相信它没有正常工作,因为Vercel的限制爱好计划,并不知何故去了我的使用。
https://vercel.com/blog/cron-jobs#limits-of-cron-jobs-and-vercel-functions
希望他们有更好的日志或某种限制使用的视图,以反映发生了什么,但它是什么。

相关问题