Please note that this question is not about keeping a process running if it crashes, or restarting it when a new deploy is done. This question is about how to restart without killing pending operations.
I have a very busy node application that receives a lot of hits every second. My application runs functions that take quite a while before they are returned (see: youtube upload via API).
The problem I am having is that when I deploy a new version of the app, the process gets restarted -- and as a result anything that was "pending" gets basically killed as well. This means that 10 youtube uploads are potentially killed and need to be retarted. Now:
- Empty the event queue is basically impossible, since I could be waiting for quite a while
- Killing the process as is is proving to be problematic
The ideal solution would be to make sure that any existing ongoing request is satisfied, but serve any new request with the newly deployed code.
A possible idea:
- Have a master process that takes connections. This process never changes
- When there is an update, send a signal to this process which will reload the "runner"
- At this point, any new request will go through the updated runner
The only time when you have to restart the process itself for real is when you want to update the master one taking the connections.
Is this approach something that is "done"? Is there a module that does it? Or is it a total overkill?
UPDATE
Interesting answer: https://stackoverflow.com/a/10711410/829771 However, it's unrealistic to wait for the event loop to be empty to restart a process.
BUT there is another level of complication here: if the server has timers and for example it runs a task every 5 minutes, following what I wrote above you would end up with two of them running. So, the "obsoleted" processes must be notified with a signal and must listen for it and stop any "background" operation when they receive it. Please keep in mind that this is not theory -- I do have setInterval()
s in my application