Hello!
In order to split up a time-consuming task into smaller bits that can safely execute within the time limit, I’m trying to invoke a Netlify function from within a Netlify function like so:
export const handler = async (event, context) => {
let { thingIds } = context
// If there aren't things, get them from the DB
if (thingIds === undefined) {
const things = await db.thing.findMany({
where: { isExcellent: true },
})
thingIds = things.map((thing) => thing.id)
}
// Pick a random index
const randomIndex = Math.floor(Math.random() * thingIds.length)
const chosenThingId = thingIds[randomIndex]
// Get the thing
const thing = await db.thing.findOne({
where: { id: chosenThingId },
})
// Do stuff
await doStuffToThings(thing)
// Remove the chosen thing
thingIds.splice(randomIndex, 1)
// If there are still things
if (thingIds.length) {
// Recursively kick off a new function to do more stuff
handler({}, { thingIds: thingIds })
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: thingIds,
}
}
This works just fine in development, but I can’t seem to get it to work when deploying to Netlify. Does anyone know how I might invoke a Netlify function from within a Netlify function? Or perhaps have a recommendation for which docs I should take a closer look at? I unfortunately didn’t have much luck with initial google searches.
Thank you!