Scheduling jobs using node-cron in Next.js deployed on GCP not working

64 Views Asked by At

I've been trying to schedule background jobs in a Next.js application deployed on GCP using the node-cron library, but I'm encountering an issue where the scheduled jobs are not being executed. I've followed the documentation and some online guides, but I'm still facing this problem.

It's working on my local, but not on the hosted environment.

Any ideas, what could be the issue?

I tried making an image in Docker, but there also it's not working. I just want to call an local node API at a specific time/date, and that works on my localhost:3000, but not in the hosted version

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.");
}

This is the config I am using:

[
    {
        "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 * * *"
    }
]
0

There are 0 best solutions below