Long running process in NodeJS
More often than not, we don’t come across the situation where, in the context of Javascript based web application, a client request needs to run really long before its able to send response back to the client. While there can be many ways to handle such situation, such as queues, one solution can be, off-loading the processing of long running request to another thread. This method, of course, has its own list of pros and cons.
Pros:
- Does not involves external components such a queue
- NodeJS supports IPC when using the module child_process
- Child process executes independent of parent process
- Parent process can pass argument to the invoking of child process, which are received as CLI arguments.
Cons
- If the child process fails / crashes, there is no simple way to put retry.
- Since the child process runs separately, it will consume some memory of its own because of independent execution context.
- If required, a polling mechanism has to be built to know the status of child process.
Nevertheless I have created a very simple example of the aforementioned concept. Please find it here. The README.md describes how the solution works.
+----------------+ +---------------+ +---------------------+
| | | | | Script to handle |
| Parent Process | <- IPC -> | Child process | <- Execution -> | really long running |
| | | | | task |
+----------------+ +---------------+ +---------------------+