Next.js Discord

Discord Forum

Implementing cron job in nextjs 14 without any external service (deployed on vps)

Answered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Is it possible to implement cron job in nextjs 14 using something like node-cron . Project is being deployed on vps. I dont want to use any external service or linux cron to hit api on schedule. I want to implement it within framework if possible
Answered by riský
like to import and run off your layout file, this should be good example:
// tools.ts
export function cron() {
  // ...
}

// layout.tsx
import {cron} from "./tools"
cron()

export default function Layout() { ... }
View full answer

21 Replies

you should be able to add the cron to a js file and then import to root layout per say (as vps isnt serverless, the code should always run)
Asian black bearOP
but layout.js will run multiple times would'nt it will initlize multiple cron jobs
@riský you should be able to add the cron to a js file and then import to root layout per say (as vps isnt serverless, the code should always run)
Asian black bearOP
ig i should probablye use simple check to run it only once
@Asian black bear but layout.js will run multiple times would'nt it will initlize multiple cron jobs
if you import to the file (so its used), it wont run every time as its not serverless - if you run the function frominside the Layout() functio then it would also run many times
@riský if you import to the file (so its used), it wont run every time as its not serverless - if you run the function frominside the Layout() functio then it would also run many times
Asian black bearOP
Didnt really understood how it would work just by importing, wouldnt i have to invoke the function in layout. sorry if it is silly i am newbie
like to import and run off your layout file, this should be good example:
// tools.ts
export function cron() {
  // ...
}

// layout.tsx
import {cron} from "./tools"
cron()

export default function Layout() { ... }
Answer
it was really helpful i searched through everything, didnt though about this approach.
thanks
no problem, let me know if it works well... and if there are any issues from nextjs being weird
(if worst comes, its not the end of world making seperate node app for it)
@riský (if worst comes, its not the end of world making seperate node app for it)
Asian black bearOP
yeah i was considering that, if this works that be great
actually another madness of an option could be to... use standalone build and then append your code (but it wont get bundled so - so wont share imports as well, so prob not bother
This is interesting to me… @riský have you done this personally? I would make sure you disable the from job if it’s not in production build lol because well it would get called in each rebuild
well true, id personally do a check in the function or before running the cron()
and i have done similar things running always during server, but i just chose to make seperate server as too annoying (like trying to update prod build)
@riský well true, id personally do a check in the function or before running the cron()
Asian black bearOP
It is working as expected thanks again
yoooo
up to you, but id think something like what was suggested: node-cron