Vercel Cron 作业在 nextjs 中使用 nodemailer 给出 504 错误

问题描述 投票:0回答:1

所以问题是我正在使用nodemailer向用户发送电子邮件。 所以nodemailer.js 文件位于根目录。

import nodemailer from "nodemailer";

export const transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "[email protected]",
    pass: "mypasswordishere",
  },
});

export const mailOptions = {
  from: "[email protected]",
};

文件路径:api/setupReminder.js

import { mongooseConnect } from "@/lib/mongoose";
import { Task } from "@/models/Task";
import { mailOptions, transporter } from "@/nodemailer";

export default async function handle(req, res) {
  const { method } = req;

  await mongooseConnect();

  if (method === "GET") {
    const today = new Date();
    const todayStart = new Date(
      today.getFullYear(),
      today.getMonth(),
      today.getDate(),
      0,
      0,
      0
    );
    const todayEnd = new Date(
      today.getFullYear(),
      today.getMonth(),
      today.getDate(),
      23,
      59,
      59
    );
    const tasksWithReminders = await Task.find({
      "reminder.date": {
        $gte: todayStart,
        $lte: todayEnd,
      },
    }).populate("user");

    try {
      // Send email reminders for tasks with matching reminder dates
      tasksWithReminders.forEach(async (task) => {
        const newMailOptions = {
          ...mailOptions,
          to: task?.user?.email,
          subject: `Task Reminder: ${task.task}`,
          html: `<h1>Task Reminder</h1><p>Reminder for task "${
            task.task
          }" on ${task.reminder.date.toLocaleString()}</p>`,
        };

        transporter.sendMail(newMailOptions, (error, info) => {
          if (error) {
            console.log(error);
          } else {
            console.log("Email sent: " + info.response);
            res.send("All ok");
          }
        });
        // }
      });
    } catch (error) {
      console.error(error);
    }
  }
}

这是我的文件 vercel.json

{
    "crons": [
        {
            "path": "/api/setupReminder",
            "schedule": "30 16 * * *"
        }
    ]
}

我已经尝试过上面的代码。也许问题出在爱好计划上,而我的请求花费了超过 10 秒的时间。任何人都可以帮助我或告诉我一种实现此功能的方法。 this is the error i am facing

next.js cron vercel nodemailer
1个回答
0
投票

http 状态代码表示您遇到的是网关超时。当函数执行时间超过计划中包含的限制时,Vercel 会返回此状态代码。

根据 Vercel cron 作业持续时间限制与 Serverless 和 Edge Functions 限制相同:

  • 爱好:10 秒(Serverless),30 秒(Edge)
  • 专业版:60 秒(无服务器)30 秒(边缘)
  • 企业:900 秒(无服务器)、30 秒(边缘)
© www.soinside.com 2019 - 2024. All rights reserved.