Hello,
I’m trying to deploy a function and utilize Netlify Schedule to have it run at specific intervals.
I have the syntax correct and deployed, however I’m missing the piece where you enable Schedule Functions at the site level. I read the documentation and it appears to be in the labs section and or on the site level. Is this a deprecated feature, or behind a subscription?
const { schedule } = require("@netlify/functions");
require('dotenv').config()
export const handler = schedule('* * * * *', async (event, context) => {
console.log('Calling function')
return {
statusCode: 200,
body: 'Hello from Netlify Functions!'
}
})
Thanks,
To enable a scheduled function, you need only deploy with a project.
Part of the issue is the code sample you are using as it doesn’t conform to that shown as examples in the Scheduled Functions documentation.
You can see this by invoking the function locally using Netlify CLI where the following error is shown:
There was an error during execution of your scheduled function:
SyntaxError: Unexpected token 'export'
Hmm, so netlify CLI works fine. Production, nothing appears to happen.
My new code is:
const axios = require('axios').default;
const { schedule } = require("@netlify/functions");
require('dotenv').config()
process.env.SILENCE_EMPTY_LAMBDA_WARNING = true
const handler = async function () {
console.log('calling func!');
axios(process.env.API_REQUEST_URL)
.then((response) => {
return response;
})
.catch((err) => console.log(err));
return {
statusCode: 200,
};
};
exports.handler = schedule(process.env.TIMEOUT, handler);
I don’t see any scheduled tag next to the functions either
I can visit the url to my scraper function, and it fires off the job route within my api function just fine, but has to be done manually. Appreciate all of your help.
The function isn’t scheduled. This is evident by the fact the function does not display “scheduled” beside it.
Another thing to note is you are trying to return a response from axios
, but that doesn’t in turn get returned by the functions.
Additionally, a scheduled function isn’t going to return a response to a user.
I understand it’s not scheduled, do you know how to schedule it? Yea I know it’s not going to be consumed anywhere, I have to adjust the logic there.
Netlify will automatically detect it and implement a scheduled function when in the correct format. There are syntactically correct examples in the previously linked documentation.
Ok so I even copied the example and it still didn’t work. Moving the cron information to the .toml file worked. This forum post helped me out:
Hey there folks, thanks so much for your patience here. Our support team is still looking into this and will follow up in this thread when we have more information.
Thanks again.
For reference, I was able to deploy a scheduled function with the cron information in the function based on this example from the documentation.
const { schedule } = require("@netlify/functions");
const handler = async function () {
console.log('Function invoked.');
return {
statusCode: 200,
};
};
exports.handler = schedule("0/10 * * * *", handler);
SamO
April 4, 2023, 12:14pm
10
Awesome! I’m glad you found your solution!