Reputation: 2499
Server.js
file i launch from the CMD: node server
Server.js
file creates an new instance of class A
Class A
creates a new instance of class B
(web socket) and C(REST API)class B
then entire NodeJS
app. simply stops/shuts down. Even though i have tried to "start the entire process" again...by trying to place different code in in close event - like:The close event is called - but no matter - what and how i try to "re-launch" my app. it doesn't execute my code - it just shuts down.
I like to understand why and what is going on and not just to re-launch the entire app - by using NPM forever etc...
UPDATE:
Sorry for not making myself clear enough. But actually the details you are looking for is my bullet point no. 4... literally no - i don't execute the entire node app again... thats why i used "" (quotes) ... as i described i try to start the process up again in the on.('close') event handler...i can debug and see when the code reach the point in the eventhandler...but it doesn't work to execute / call A.start() function again or via firering an event...so its some thing about scopes, instances, function etc. i properly dont do correct - thats why i tried to describe the app, class, functions...now - i hope its more clear what i mean :-)
UPDATE 2:
Server.js
var dlManager = new DownloadManager();
dlManager.initDownload('BTC-USD');
Download-manager.js
function DownloadManager() {
this.initDownload = function(productList) {
var wl = new WinLog();
var restClient = new RestApiClient();
var webSocketClient = new WsClient();
wl.info('Running...');
try {
// Removed a lot of not relevant code from here...
function startWS(productList) {
// 2. Make the WS begin to queuing all the newest trading data...
webSocketClient.initws(productList);
}
} catch (err) {
wl.info('initDownload: ' + err);
}
}
}
function create() {
return new DownloadManager();
}
module.exports = create;
Wsclient.js
function WsClient() {
this.initws = function(productList) {
var wl = new WinLog();
try {
var firstTime = true;
var WebSocketEmit = new WSemitter();
var activeQueue = [];
var insertToDB = [];
var cursorId = 0;
var websocket = new Gdax.WebsocketClient(productList);
websocket.on('message', function(data) {
// Removed a lot of not relevant code from here...
});
websocket.on('close', function close() {
wl.info('Disconnected');
//WebSocketEmit.connectionClosed(productList);
// Starts from the beginning once again ;-)
var new_dlManager = new DownloadManager();
new_dlManager.initDownload(productList);
});
} catch (err) {
wl.error('initws: ' + err);
}
}
this.SetHistoricalDataOK = function(value) {
HistoricalDataOK = value;
}
}
function create() {
return new WsClient();
}
module.exports = create;
Upvotes: 0
Views: 223
Reputation: 707328
A node app will stay alive as long as there are active elements that can still receive events. This can include a TCP connection, a listening server, a timer, etc...
As soon as none of those exist any more and the event queue is empty and the flow of control returns back to the system (e.g. no JS is executing), then the node app will shut itself down. The idea is that if nothing is running that could possible create more events in the future, then there's nothing else that can happen in this node app so it must be done.
If you're saying that when you try to run your app again, it won't start any more, then that is a different issue. You will need to do some logging of errors and debugging to figure out why that may be. If you just log every possible place you could get an error in your app start-up code, you will probably discover where the error is that is keeping it from starting. Since you've provided not details that we could go on to know what issue might be preventing that, I can list some common things:
Some process is still running that has control of some resource (such as a port) and when you try to restart your app, it shuts down when it can't access that resource.
Some process is still running and has open file resources, restricting your ability to access those files.
To start with, I'd check your OS process manager to verify that your original app instance is not still running. The symptoms you describe make it sound like it is probably still running and holding onto some resources that keep your app from running again.
This part has me confused when you say "start the entire process again" when your wed socket closes. Are you literally trying to exec node server.js
again from within your app? Or, what are you actually trying to do there? We probably need to see that code to help you with that particular action. FYI, it should be no big deal to just re-establish a webSocket connection after it closed. You shouldn't have to restart your whole app just to do that.
Upvotes: 1