N
Nuxt7mo ago
Yaiko

Nitro plugin while loop stucks server

I have a simple plugin that reads a file and runs some code periodically.
const task = () => {
while (1){
// Reads file and runs code if data on it
}
}

export default defineNitroPlugin(() => {
task();
});
const task = () => {
while (1){
// Reads file and runs code if data on it
}
}

export default defineNitroPlugin(() => {
task();
});
The while makes the server go stuck, is there an alternative to run a code like that in the server background without breaking the rest of the server? Thanks. https://github.com/nuxt/nuxt/issues/26809
GitHub
Nitro plugin while loop stucks server · Issue #26809 · nuxt/nuxt
Environment Operating System: Darwin Node Version: v18.15.0 Nuxt Version: 3.11.2 CLI Version: 3.11.1 Nitro Version: 2.9.6 Package Manager: [email protected] Builder: - User Config: devtools, app, runtimeCo...
8 Replies
Sandvich
Sandvich7mo ago
Maybe you want something like this? https://nodejs.org/api/child_process.html#child_processspawncommand-args-options You'll probably want to add a hook for the "close" event to kill the child process. Or maybe you don't need to, I honestly don't know as I've never used child_process before I just know it exists Also why did you make an issue on this when it's not a bug? you should probably close the issue, and save the maintainers some time
Yaiko
YaikoOP7mo ago
child_process is for running shell commands. I opened the case because Nuxt plugins should allow running this kind of background plugins...
Sandvich
Sandvich7mo ago
Sorry I meant child_process fork https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options That's for running other node processes and it won't block Nitro Without spawning a child process you'll have to do some complicated promise setup where you give up control back to the main thread occasionally. Or maybe you just want a simple cron job?
Sandvich
Sandvich7mo ago
https://nitro.unjs.io/guide/tasks#scheduled-tasks Could use this to run whatever code you want every minute
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
Yaiko
YaikoOP7mo ago
That method looks a bit complex and it opens a new Node instance, not exactly what I'm trying to do... Crons won't work since I want this while loop to always run. The only solution I see is to Nitro to not wait for the plugin to finish running, somehow.
Sandvich
Sandvich7mo ago
Since this plugin is just a sync function there's no possible way for Nitro to not wait for the function to finish. That's just how javascript works If you were able to listen to certain events and run the code you need without a while loop that would work setInterval could work, but you might run into problems
Yaiko
YaikoOP7mo ago
Yes, already tried using async but still stucks. Yes, I don't have other option than using while since I also run async code on it.
Sandvich
Sandvich7mo ago
There's just no way in javascript to do what you want to do, I don't know what to tell you and that's not Nitro's problem
Want results from more Discord servers?
Add your server