KurtzL / nestjs-temporal

Temporal plugin for nestjs framework
MIT License
102 stars 21 forks source link

Worker restarts: address already in use #54

Open tiberiu89 opened 3 months ago

tiberiu89 commented 3 months ago

Everything starts fine on the first run, but when changing code and hot reloading is triggered, the worker seems to gracefully shutdown successfully, but upon restarting, the process complains the default 3000 port is already in use(I assume the one nestjs is listening. Now, I have a feeling this is because the worker is spawned as a child process, and it tries to bind to the same address. Maybe I'm wrong, but I can't seem to find a workaround. Any ideas would be appreciated.

gaizeror commented 3 months ago

it seems like you are missing app.enableShutdownHooks()

async function bootstrap() {
    const port = 3000

    const app = await NestFactory.create(AppModule, {
        bufferLogs: true,
        autoFlushLogs: true,
    })
    app.enableShutdownHooks()

    await app.listen(port, () => {
        /* eslint-disable no-console */
        console.log(`[server]: Server is running at http://localhost:${port}`)
    })
}

bootstrap().catch(err => {
    /* eslint-disable no-console */
    console.error(err)
    process.exit(1)
})
tiberiu89 commented 3 months ago

Apart from a warn saying worker was not cleanly shutdown, this seems to be doing the job. An alternative I used so far is what I described here https://github.com/temporalio/sdk-typescript/issues/1437 basically, running the codebase in 2 processes, one regular http app and the worker running in an application context only, which might adhere better to docker best practices