Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update eject example to something more robust #616

Open
2 tasks done
lord007tn opened this issue Apr 17, 2023 · 4 comments
Open
2 tasks done

Update eject example to something more robust #616

lord007tn opened this issue Apr 17, 2023 · 4 comments

Comments

@lord007tn
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

Change the eject file example to validate everything before running the server and make use of the .ready() function.

Motivation

What made me think of this approach are:

  • Fastify encapsulation concept, the server should not run if something goes wrong with plugins and are not loaded inside correctly inside Fastify.
  • The struggle I've been in the past few days trying to use Fastify in a complex real-world project and I didn't find clear examples.
  • easier to handle errors, if something goes wrong Fastify will throw the valid error not some error buried deep inside the router or hooks
  • the unclear error "cannot read properties of undefined (reading 'length')" which was happening for several reasons and I cannot detect them until i throw error on startup

Example

// Read the .env file.
import * as dotenv from "dotenv";
dotenv.config();

// Require the framework
import Fastify from "fastify";

// Require library to exit fastify process, gracefully (if possible)
import closeWithGrace from "close-with-grace";

// Instantiate Fastify with some config
const app = Fastify({
  logger: true,
});
// make use of the `.ready()` to prevent going on race condition
fastify.register(app).ready((err) => {
   // throw error if something happened on registering `plugins` or `routes`
	if (err) throw err;
	// delay is the number of milliseconds for the graceful close to finish
	const closeListeners = closeWithGrace(
		{
			delay:
				parseInt(process.env.FASTIFY_CLOSE_GRACE_DELAY as string) ||
				500,
		},
		async function ({ signal, err, manual }) {
			if (err) {
				fastify.log.error(err);
			}
			await fastify.close();
		} as closeWithGrace.CloseWithGraceAsyncCallback
	);
	fastify.addHook("onClose", async (instance, done) => {
		closeListeners.uninstall();
		done();
	});
	//server listen
	const port = process.env.PORT || 8000;
	// Start listening.
	fastify.listen({ port: parseInt(port as string) }, (err: any) => {
		if (err) {
			fastify.log.error(err);
			process.exit(1);
		}
	});
});
@lord007tn
Copy link
Author

@mcollina what are your thoughts about that?

@mcollina
Copy link
Member

I would move the close-with-grace setup inside the listen callback instead.

@lord007tn
Copy link
Author

could you explain more why?
as i know you cant have addHook inside listen

@mcollina
Copy link
Member

mcollina commented May 6, 2023

Ah sorry, I think I misunderstood your example.

I don't think close-with-grace should be within a plugin or a callback, but rather at the top level, installed after the server has been setup.

It's a property of the global application and not of the server, and you can install only one. Therefore, you should put that into the "main" of your application outside of all plugins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants