Scheduled function shows as deployed but never runs and there are no logs

So I enabled Scheduled Functions through Labs and I’m trying them out for an upcoming project’s site. There are no issues with the deploy and the function works just fine when reachable via HTTP locally, but as soon as I turned it into a scheduled function, nothing happens. There are no function logs (the loading indicator never disappears). The function was deployed to a branch deploy.

Site: https://develop.defimining.fi

Hey there, @niceshadowraze :wave:

Thanks for reaching out about this. Could you please share your function name with us? Additionally, can you share your project repo? This will help us look into the situation further.

I can’t really share the repo as it’s not mine (private org repo). The function simply writes a few values to DynamoDB. I’ll test another function, something like:

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

const handler = async function(event, context) {
    console.log("Received event:", event);

    return {
        statusCode: 200,
    };
};

module.exports.handler = schedule("@hourly", handler);

and report back

Ok so I tried the simplest function possible

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

module.exports.handler = schedule('* * * * *', () => {
  console.log('test');
});

still no logs, doesn’t seem to be executing.

Hey @niceshadowraze

As well as enabling Scheduled functions in Netlify Labs, they also need enabling on a per-site basis. Have you done both?

Yeah, I’ve done both. However, I’m not the team/site owner, do you think it might have something to do?

I used the basic function from the Scheduled Functions documentation as a test, setting it to run every 5 minutes. This has worked for me, logging the event.

On the functions page, it shows the next scheduled run time.

After the second running (at 8am) when I checked the logs (at ~8:01am) nothing was shown when “latest” was selected, I needed to change to “last hour”. So it is possibly something with the view you are using @niceshadowraze ?

(Curiously @hillary at 8:07am I would have expected to see the logs for the 8:05am running of the function under “latest” yet nothing is shown, I have to switch to “last hour” to see the log from 2 minutes ago. Small bug perhaps?)

“Latest” shows no logs for me on all functions. Changing it to “last hour” shows “No results found for query” even for the simple function I posted above. My understanding is that console.log, console.warning, etc should end up here if the function executed? I don’t think they are executing at all if that’s the case

I’ll create a test site I own with some test functions

So I created a test site and can confirm I see the logs for this function https://github.com/gaspardip/remix-scheduled-functions/blob/master/netlify/functions/foo/index.ts

I wonder if me not being the owner of the other site might have something to do with functions not executing? maybe the owner has to enable scheduled functions through labs too? I honestly have no other explanation.

Ok I think I found the issue, functions published to any branch that is not the production branch are not being run, check these functions https://github.com/gaspardip/remix-scheduled-functions/tree/develop/netlify/functions, and their logs

2 Likes

As per below, they only run on the main/production branch (at this point; when they are out of labs this may change.)

Well that wasn’t mentioned in the docs anywhere, would be useful to have it there imo. It’s quite unfortunate because we are using branch subdomains and our default branch doesn’t have the same content as develop (they are even different builds, default branch is the landing, develop is the development branch for our app). I guess the workaround for now is to include the relevant code in the default branch.

howdy @niceshadowraze - sorry about the frustration for this! we’re gonna look into the behaviour you are describing and see if we can at very least make the docs more clear so others don’t have to go through the same journey to figure this out. It takes a village. thanks again!

1 Like

That would be great, thanks!

Hi, thanks a lot for the scheduling feature – that was exactly what we needed <3

Only problem, though: we’re having the same issue (function is deployed, works as expected when manually launched, but doesn’t run when expected and no logs to boot), although it has been deployed to the production branch.

Moreover, on the functions page the “Scheduled” tag & next execution time don’t show up:

Would we be missing something important in the setup?

(tried both ways of setting up the scheduler – in the function itself and in netlify.toml – with the same results)

Hey @voormedia

Just as a double-check on the functions page does the button say “Disable Scheduled Functions” or “Enable Scheduled Functions”?

Hi, @coelmay! Thank you for your reply.

It says “Disable Scheduled Functions”, just like in your screenshot:

Great. But you don’t see a “Scheduled” next to the function?

You say you’ve tried both configuration methods outlined in the docs here and here? The function from my screenshot is exactly the code from the first link (and is working) and have deployed using the second method today without issue too.

Are you able to share the repository, or code?

Correct. Unlike your screenshot, no “Scheduled” for me next to the function.

And I did try both configurations you linked, the inline and the .toml methods.

The code for both attempts:

1. Inline

netlify/functions/rebuild/rebuild.js:

const {schedule} = require("@netlify/functions");
const fetch = require("node-fetch");

const REBUILD_URL =
  "https://api.netlify.com/build_hooks/XXXX";

const handler = async function(event, context) {
  await fetch(REBUILD_URL, {method: "POST"});

  return {
    statusCode: 200,
  };
};

module.exports.handler = schedule("@hourly", handler);

2. .toml

netlify/functions/rebuild/rebuild.js:

const fetch = require("node-fetch");

const REBUILD_URL =
  "https://api.netlify.com/build_hooks/XXX";

const handler = async function(event, context) {
  await fetch(REBUILD_URL, {method: "POST"});

  return {
    statusCode: 200,
  };
};

module.exports.handler = handler;

and netlify.toml:

...
[context.production.environment]
  TZ = "Europe/Amsterdam"

[functions."rebuild"]
  schedule = "@hourly"

Hey @voormedia,

I don’t see anything wrong with the syntax, or anything else that stands out. Would you be able to share the repo so we can test this? Seems like there could be something weird happening with a specific use case.