GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update eject example to something more robust · Issue #616 · fastify/fastify-cli

Update eject example to something more robust #616

New issue
Copy link
New issue
Copy link
Open
Open
Update eject example to something more robust#616
Copy link

Description

@lord007tn
lord007tn
opened on Apr 17, 2023
Issue body actions

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);
		}
	});
});

Activity

  1. lord007tn commented on Apr 20, 2023

    @lord007tn
    lord007tn
    on Apr 20, 2023
    Author
    More actions

    @mcollina what are your thoughts about that?

  2. mcollina commented on Apr 25, 2023

    @mcollina
    mcollina
    on Apr 25, 2023
    Member
    More actions

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

  3. lord007tn commented on May 5, 2023

    @lord007tn
    lord007tn
    on May 5, 2023
    Author
    More actions

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

  4. mcollina commented on May 6, 2023

    @mcollina
    mcollina
    on May 6, 2023
    Member
    More actions

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.