mbhall88 / masters_project

0 stars 0 forks source link

Kill child process when socket disconnects #5

Closed mbhall88 closed 7 years ago

mbhall88 commented 7 years ago

When the websocket is closed, i.e if the client navigates away from the page during analysis, kill the child process.
Should be able to just add a 'disconnect' listener to the socket.

mbhall88 commented 7 years ago

Functionality is there but now there is a problem where if the client clicks 'Stop Analysis' button and then closes the browser there is an error because the 'disconnect' event on the socket is trying to kill the child process and writeStream when they were already stopped by the 'kill' event emitted from the client-side.

mbhall88 commented 7 years ago

This has now been fixed. On socket disconnect event there is a series of logic steps which will close the child process and/or writable streams if they're still open:

socket.on('disconnect', function(){
        console.log("Socket disconnected");
        // TODO - run test to see if CP and stream are already closed
        if (writeAnalysisFile.closed && !speciesTyping.connected){
            // write stream and CP are both closed.
        } else if (writeAnalysisFile.closed && speciesTyping.connected) {
            // write stream closed but CP open still.
            onCloseOrKill(speciesTyping.pid, null);
        } else if (!writeAnalysisFile.closed && speciesTyping.connected) {
            // write stream and CP both open.
            onCloseOrKill(speciesTyping.pid, writeAnalysisFile);
        } else {
            // write stream open but CP is closed.
            onCloseOrKill(null, writeAnalysisFile);
        }
    });