mstephen19 / nanolith

Multithreading in Node.js made simple with seamless TypeScript support.

Home Page:https://www.npmjs.com/package/nanolith

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catching exceptions from `__initializeService`

mstephen19 opened this issue · comments

Currently, the following code is in the runner for services:

            // ! temp - enable catching errors in the __initializeService
            // ! hook. This is only temporary because the "terminate()" function
            // ! doesn't actually do anything. Eventually, closing a service should
            // ! be done with message passing for more flexibility.
            const initErrHandler = async (body: WorkerBaseMessageBody) => {
                if (body.type !== WorkerMessageType.WorkerException) return;
                await exceptionHandler?.({
                    error: (body as WorkerExceptionMessageBody).data,
                    terminate: () => {
                        //
                    },
                });
            };

The terminate() functionality is completely empty, but when this function is called, the worker should be terminated. The Service instance isn't created yet though, so service.close() cannot be called (Service is only initialized one the worker has notified that it is ready). The solution is to use message-passing in order to command the worker at a lower level (lower than Service level).