fastify / restartable

Restart Fastify without losing a request
MIT License
104 stars 8 forks source link

@Fastify/redis plugin connection close but can't connect again #47

Closed lord007tn closed 1 year ago

lord007tn commented 1 year ago

Prerequisites

Fastify version

4.18.0

Plugin version

2.1.0

Node.js version

18.x

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Description

redis connect but say "Connection is closed" after the app restart. and I can't make it connect again

err: { "type": "Error", "message": "Connection is closed.", .... }

I'm using @fastify/autoload to load my plugins and they work just fine an other problem after app.restart() is that @fastify/redis seems to have an error that says connection is closed

and in the app.addOnRestartHook() the new app dosen't have the Redis plugin loaded in it or any other plugin so i assume that @fastify/restartable is making a new fastify instance without using my factory function

Steps to Reproduce

https://github.com/lord007tn/fastify-reproducible-example this is my code that works fine before the restart and even with the first start:

//register fastify app

async function createServerApp(fastify: Fastify, opts: FastifyServerOptions) {
    const app: FastifyInstance = await fastify(opts);

    app.register(import("./app.js")).ready((err) => {
            if (err) throw err;
        });
    // delay is the number of milliseconds for the graceful close to finish

    return app;
}
//@ts-ignore
const app = await restartable(createServerApp, {
    logger: {
        transport: {
            target: "pino-pretty",
            options: {
                translateTime: "HH:MM:ss Z",
                ignore: "pid,hostname",
            },
        },
    },
    pluginTimeout: 20000,
    ajv: {
        customOptions: {
            allowUnionTypes: true,
        },
        // the ajvFilePlugin is my custom plugin to add new types to ajv validator and it's not working after restart
        plugins: [ajvFormat, ajvFilePlugin],
    },
});
    const closeListeners = closeWithGrace(
                {
                    delay:
                        parseInt(
                            process.env.FASTIFY_CLOSE_GRACE_DELAY as string
                        ) || 500,
                },
                async function ({ signal, err, manual }) {
                    if (err) {
                        app.log.error(err);
                    }
                    await app.close();
                } as closeWithGrace.CloseWithGraceAsyncCallback
            );
            app.addHook("onClose", async (_instance, done) => {
                closeListeners.uninstall();
                done();
            });

const port = process.env.PORT || 8000;
// Start listening.

app.listen(
    { host: "127.0.0.1", port: parseInt(port as string) },
    (err: any) => {
        if (err) {
            app.log.error(err);
            process.exit(1);
        }
        setTimeout(() => {
            app.restart();
        }, 2000);
    }
);

Expected Behavior

i expect the app to reload again with no problem

lord007tn commented 1 year ago

I'm using @fastify/autoload to load my plugins and they work just fine an other problem after app.restart()is that @fastify/redis seems to have an error that says connection is closed

and in the app.addOnRestartHook() the newapp dosen't have the redis plugin loaded in it or any other plugin so i assume that @fastify/restartable is making a new fastify instance without using my factory function

ivan-tymoshenko commented 1 year ago

@lord007tn Can you put the minimal reproducible example to the repo and share the link?

lord007tn commented 1 year ago

@ivan-tymoshenko this is where ia have a problem with redis https://github.com/lord007tn/fastify-reproducible-example

lord007tn commented 1 year ago

@ivan-tymoshenko i can't reproduce the Ajv problem but still have the main problem in the redis plugin

lord007tn commented 1 year ago

related to https://github.com/fastify/fastify-redis/issues/181