-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime hook for server startup/init event #1987
Comments
A special hook running once at start up would be great to e.g. log some debugging information into the log stream of a Nuxt container. Imagine something like this: export default defineNitroPlugin((nitroApp) => {
console.log('Nitro plugin', nitroApp);
// Variant a) make config accessible via nitroApp
console.log(`Using Backend API Version: ${nitroApp.runtimeConfig.apiVersion}`);
console.log(`Feature Flag A: ${nitroApp. runtimeConfig.public.featureA}`);
// Variant b) start hook
nitroApp.hooks.hook("start", async (config) => {
console.log(`Using Backend API Version: ${runtimeConfig.apiVersion}`);
console.log(`Feature Flag A: ${runtimeConfig.public.featureA}`);
});
}); This would make it much easier to write out hints e.g. for a helpdesk team. They could just start with the log and look for the important configuration options. Btw. you already use the config in |
@botic nitro plugins actually run once during server startup. what is current issue with Varaint a? (feel free to share feedback in a new issue if have issues or questions about it not working) Example: export default defineNitroPlugin(() => {
const runtimeConfig = useRuntimeConfig();
console.log(
[
`Using Backend API Version: ${runtimeConfig.apiVersion}`,
`Feature Flag A: ${runtimeConfig.public.featureA}`,
].join('\n')
);
}); https://stackblitz.com/edit/github-67l6eo?file=plugins%2Fbanner.ts,nitro.config.ts |
Yes, this is the behavior. Nitro plugins run once during server startup. It is documented but PR welcome to improve and reword
You can track support for async plugins in #915 however generally I wouldn't recommend blocking (entire) server for tasks such as DB migrations. Instead you could create a utility like Nitro Tasks API is also a relevant topic you might like to track. |
I must have missed the "on the first" in the docs (- why does it even say "first", are there cases in which multiple initializations are possible?).
Thanks for the pointer, async plugins would be perfect for me than.
While performing migrations on startup might not sound ideal, it can often be the most practical solution and is widely used due to this. While it might not work for distributed stuff with all its complications, for single node services it is perfect. There is always a small downtime when restarting to the newer version, a few more seconds won't hurt. It is a lot easier to apply, instead of e.g. having to run commands inside a docker container. |
Async plugins, if/when supported will likely show a warning if their evaluation takes too long which can happen for dbs. Also for Nitro's built-in database API, lazy init will be used because you need the database once you need it(at least IMO). This kind of lazy startup is one of the core principles in Nitro's server design that ensures it is fast and scalable. However, I respect your opinion about the convenience of blocking the whole server startup also it is nothing strange indeed many (traditional) approaches of DB migration block server startup. I made an alternative discussion to support |
Hello @pi0, colleague of @botic here. Thanks for the information that Nitro Plugins only run once, we somehow missed this in the docs which you linked. We use Nitro in the context of Nuxt, and tried our usecase there using all kind of hooks, both defined in the Not defining a hook and logging directly in the |
Yes, Variant A is perfect. I cannot reproduce my issue not having access to I personally was looking for an |
How would such a lazy init work? The current nitro docs do not mention anything about database migrations and the usage example always calls My assumption is that this is not implemented, yet, however I am curious how you intend to integrate it. |
Testing your example, I do not get any output to console. Furthermore, Nuxt warns me to use defineNuxtPlugin instead. Is this something specific to my use case? |
Describe the feature
It would be great if there were a possibility to run some code once during server startup. My potential use case would be running migrations programmatically. Currently there does not exist a hook for this.
It is unclear if running such code inside the
defineNitroPlugin
satisfies these requirements (run only once, run before any request is handled) and if yes, that should likely be documented.PS: However, even if they do meet these requirements it does not seem like they support async calls.
See also nuxt/nuxt#22486
Additional information
The text was updated successfully, but these errors were encountered: