You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署在GCP的Next.js中node-cron定时任务无法执行问题

Next.js部署GCP后node-cron定时任务失效问题

本地运行Next.js应用时,使用node-cron库调度后台任务完全正常,但部署到GCP环境(包括Docker镜像部署方式)后,定时任务无法执行。需求是在指定时间调用本地Node API,该接口在localhost:3000下可正常响应,但托管版本无法触发调用。

相关代码

处理任务调度的POST接口代码:

if (req.method === "POST") {
  const currentConfigs = await readConfig(appName, baselineId);
  const date = new Date(frontendConfig.timeOfRun);
  const cronTime = `${date.getMinutes()} ${date.getHours()} * * *`;

  if (frontendConfig.selectedDays && frontendConfig.selectedDays.length) {
    frontendConfig.cronTime = `${date.getMinutes()} ${date.getHours()} * * ${frontendConfig.selectedDays.join(",")}`;
  } else {
    frontendConfig.cronTime = cronTime;
  }
  if (!currentConfigs) {
    console.error("Error reading configuration.");
    res.status(500).send("Error reading configuration.");
    return;
  }

  const configIndex = currentConfigs.findIndex(
    (config) => config.testType === frontendConfig.testType
  );

  if (configIndex !== -1) {
    currentConfigs[configIndex] = frontendConfig;
  } else {
    currentConfigs.push(frontendConfig);
  }

  await writeConfig(currentConfigs, appName, baselineId);

  cron.schedule(
    cronTime,
    async () => {
      const latestConfigs = await readConfig(appName, baselineId);
      const currentConfig = latestConfigs.find(
        (c) => c.testType === frontendConfig.testType
      );

      if (currentConfig && currentConfig.browsers.length) {
        try {
          writeStatus(true);
           const response = await axios.post(
            `${ENV.app_url}/api/schedule/runScript`,
            {
              param: frontendConfig.testType,
              testRunNature: "group",
              groupTestRunId: generateUUIDToPassInPayloadForGroupRun(),
              typeOfTestRun: "scheduled",
            },
            {
              headers: {
                applicationId: appId,
              },
            }
          );
          if (response.status === 200) {
            console.log("Test executed successfully");
          } else {
            console.error(
              "Error executing the test. Server responded with status:",
              response.status
            );
          }

          if (currentConfig && currentConfig?.Repeat === "No Repeat") {
            await deleteConfig(frontendConfig.testType, appName, baselineId);
          }
        } catch (error) {
          console.error("Error executing the test:", error);
        }
        finally {
          writeStatus(false);
        }
      }
    },
    {
      scheduled: true,
      timezone: systemTimeZone,
    }
  );

  res.status(200).send("Test scheduled.");
}

配置文件

[
    {
        "timeOfRun": "",
        "browsers": [],
        "os": null,
        "Repeat": "No Repeat",
        "selectedDays": [],
        "testType": "smoke",
        "headless": true,
        "baselineId": "",
        "appName": "",
        "cronTime": ""
    },
    {
        "timeOfRun": "2024-02-20T11:16",
        "browsers": [
            {
                "value": "chrome",
                "label": "Chrome"
            }
        ],
        "os": {
            "value": "linux",
            "label": "linux"
        },
        "Repeat": "No Repeat",
        "selectedDays": [],
        "testType": "regression",
        "headless": true,
        "baselineId": "eb76caf1-6ac6-4a38-9c5f-20f4662f8096",
        "appName": "sample",
        "cronTime": "16 11 * * *"
    }
]

内容的提问来源于stack exchange,提问作者Divyansh Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:53:09