Next.js Discord

Discord Forum

Scheduled tasks inside nextJS

Answered
Order posted this in #help-forum
Open in Discord
I'm planning a budget tracking app, and I want to have reocurring expenses and incomes that update the total balance of the user every month. But I have no idea how to go about it inside a nextJS app. I don't plan on hosting on Vercel, what's a good JS library to use to schedule those tasks.
Example: The user get's their pay checks on the 5th and the 20th of every month and they're fixed to 1500$ each. I want them to be able to input that information and then in someway the due dates of those incomes should add those 1500$ to their total incomes page.
Answered by Arinji
if you have a way to call a function.. go for it. I just made a hono project for all my cron jobs and run it on my vps with portainer

https://github.com/Arinji2/project-apis/blob/main/projects/vibeify/functions/cron-jobs/resetConverts.ts

Either way its upto you.
View full answer

14 Replies

Cape lion
Build a custom endpoint that does the adding and then use a cron job (on your server) to call that endpoint periodically.

I found this, but never used it, might be good: https://tryslater.com/
Using a custom service is exactly what I'm trying to avoid. I don't like depending on 3rd party providers. I was thinking if there is a way to host another server to execute those cron jobs or something of the sort.
Also it's worth noting that I'm not particularly interested in supporting serverless architecture. I run my nextjs projects on a vps in a long-running environment.
Willow shoot sawfly
@Order Use a scheduler like [bree](https://github.com/breejs/bree) or [agenda](https://github.com/agenda/agenda), and set it up with a database so it persists across restarts of your app.

You can set it up in the [Instrumentation](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation) file so that it runs whenever your Next.js server starts.
most of these feel like overkill lol, OP has a vps so why not just run a basic express nodejs app with node-cron on it?
i use that for my side projects
Or can't i just use the linux built-in task scheduler, "at" or whatever it is
and run a script on it that executes the function?
not a thing with normal nextjs
nextjs has 0 support for any sort of cron job type scheduling
for example I write out a function that checks the dates of all tasks scheduled payments from all users and if the date is today it subtracts/adds to their budget and then I use a bash script to execute that function every day at noon for example
it doesn't directly but I've done it on windows using task scheduler, granted for a much more simple task- deleting inactive auth sessions in my db
if you have a way to call a function.. go for it. I just made a hono project for all my cron jobs and run it on my vps with portainer

https://github.com/Arinji2/project-apis/blob/main/projects/vibeify/functions/cron-jobs/resetConverts.ts

Either way its upto you.
Answer
Okay thanks all those options seem interesting, I'll take a look