Why does my netlify scheduled function run 3 times in a row

Hi everyone,

I have a problem with my scheluded netlify function for my project fortnite-plus.netlify.app As you see in the screenshot, it gets executed 3 times and well, this is not what I expect.

Here is a dummy version of my code:

const { schedule } = require("@netlify/functions");
const axios = require("axios");

const handler = async function () {
  console.log("updating shop image :)");
  try {
    const { data } = await axios.get("some-endpoint");
    console.log(data);
  } catch (err) {
    console.error("i failed");
  }
};

module.exports.handler = schedule("5 0 * * *", handler);

If anybody knows the trick to only have the function being run only once, I would be happy to hear about it :wink:

Have a nice day!

Hey @Shanbic

The function runs three times because the function does not return a status code of 200 to indicate it completed without error *(or any other status code). As it didn’t complete, it gets run again, and then a third time, before the system stops trying (until the next day.)

See this documentation for examples.

2 Likes

Thank you so much ! I went through the documentation again but couldnt read anything about the statusCode (I saw the example but it was not mentionned it HAD to be this way). But it totally makes sense and I thank you a lot for your really really fast answer :wink:

Scheduled Functions are an extension of Netlify Functions meaning they are still functions thus need to return a status like any other.

They are new, so documentation will most certainly improve over time.

I was surprised by this too. Before scheduled functions I used background functions which do not return a response.

Still not mentioned in the docs anywhere :disappointed_face: I switched to using Response() thinking I was smart rather than bare returns. nope.