Vercel定时任务在Next 13路由API中未触发。

huangapple go评论66阅读模式
英文:

Vercel Cron Job not firing with Next 13 Route API

问题

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

我有一个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: '已更新已完成的目标' });
}

和一个vercel.json:

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

当我在本地手动触发函数时,它按预期工作。

然而,当我在vercel上手动触发cron作业时,在日志中看到:

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

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

我是cron作业的新手,不清楚问题出在哪里。

非常感谢任何帮助。

英文:

I am using Next 13 app dir.

I have a route.ts file:

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:

{
  "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:

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

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

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

Any help is greatly appreciated.

答案1

得分: 0

It started to work now.

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

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.

英文:

It started to work now.

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

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.

答案2

得分: 0

我遇到了相同的问题,但我使用以下代码解决了它。

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

希望这段代码对你有帮助。

英文:

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

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

I hove this code will help you...

huangapple
  • 本文由 发表于 2023年6月12日 13:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定