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

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

Vercel Cron Job not firing with Next 13 Route API

问题

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

我有一个route.ts文件:

  1. import { prisma } from '@/lib/prisma';
  2. import { NextResponse } from 'next/server';
  3. export async function GET() {
  4. const users = await prisma.user.findMany();
  5. for (const user of users) {
  6. const goals = await prisma.goal.findMany({
  7. where: { userId: user.id },
  8. });
  9. for (const goal of goals) {
  10. if (goal?.percentage ?? 0 >= 100) {
  11. await prisma.$transaction([
  12. prisma.user.update({
  13. where: { id: user.id },
  14. data: {
  15. completedGoals: [...user.completedGoals, goal.id],
  16. },
  17. }),
  18. prisma.goal.update({
  19. where: { id: goal.id },
  20. data: { percentage: 0 },
  21. }),
  22. ]);
  23. }
  24. }
  25. }
  26. return NextResponse.json({ message: '已更新已完成的目标' });
  27. }

和一个vercel.json:

  1. {
  2. "crons": [
  3. {
  4. "path": "/api/cron/set-completed-goals",
  5. "schedule": "0 0 * * *"
  6. }
  7. ]
  8. }

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

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

  1. 200
  2. [GET] /api/cron/set-completed-goals

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

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

非常感谢任何帮助。

英文:

I am using Next 13 app dir.

I have a route.ts file:

  1. app/api/cron/set-completed-goals/route.ts
  1. import { prisma } from '@/lib/prisma';
  2. import { NextResponse } from 'next/server';
  3. export async function GET() {
  4. const users = await prisma.user.findMany();
  5. for (const user of users) {
  6. const goals = await prisma.goal.findMany({
  7. where: { userId: user.id },
  8. });
  9. for (const goal of goals) {
  10. if (goal?.percentage ?? 0 >= 100) {
  11. await prisma.$transaction([
  12. prisma.user.update({
  13. where: { id: user.id },
  14. data: {
  15. completedGoals: [...user.completedGoals, goal.id],
  16. },
  17. }),
  18. prisma.goal.update({
  19. where: { id: goal.id },
  20. data: { percentage: 0 },
  21. }),
  22. ]);
  23. }
  24. }
  25. }
  26. return NextResponse.json({ message: 'Completed goals updated' });
  27. }

And a vercel.json:

  1. {
  2. "crons": [
  3. {
  4. "path": "/api/cron/set-completed-goals",
  5. "schedule": "0 0 * * *"
  6. }
  7. ]
  8. }

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:

  1. 200
  2. [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

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

  1. export const revalidate = 0;
  2. export async function GET(req: Request) {
  3. // your db logic
  4. return NextResponse.json({
  5. result: result
  6. });
  7. }

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

英文:

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 -->

  1. export const revalidate = 0
  2. export async function GET(req: Request) {
  3. // your db logic
  4. return NextResponse.json({
  5. result:result
  6. });
  7. }

<!-- 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:

确定