Setting up a scheduled task on Blitz works the same as on Next.js.
To begin, you need to define your serverless function in the /api
directory (at the root level, not inside the rpc
folder).
Next, create your file like this:
import { api } from "app/blitz-server"
import db from "db"
export default api(async (req, res, ctx) => {
try {
// Perform your tasks here
res.status(200).json({
message: `Success!`,
})
} catch (e) {
res.status(400).json({
message: `Error : ${e.message}`,
})
}
})
Lastly, set up a cron job targeting your API endpoint.
You have various options for this:
- You can utilize cron-job.org
- If your project is hosted on Netlify, you can use Netlify scheduled functions
- Alternatively, you can manually configure a cron job on Vercel
There are also third-party options available, though some may come with a cost.
We hope this information proves helpful 👍