asaraf (3) [Avatar] Offline
#1
I am actually trying to port the example to the latest version of Express.

Looking at the source for CRUD in chapter 8.5.2

we are calling clearIsOnline() duing module initialization:

 ------------- BEGIN MODULE INITIALIZATION --------------
dbHandle.open( function () {
  console.log( '** Connected to MongoDB **' );
  clearIsOnline();
});


This will in turn go to this code -
clearIsOnline = function () {
  updateObj(
    'user',
    { is_online : true  },
    { is_online : false },
    function ( response_map ) {
      console.log( 'All users set to offline', response_map );
    }
  );
};


What would happen if someone logs in as soon as the server starts up and their online status is set to true, only to be set to false by the asynchronous execution of updateObj in clearIsOnline?

Should all server initialization be isolated in some form of synchronous block before the server starts?
michael.mikowski (247) [Avatar] Offline
#2
The short answer is "maybe". The long answer is JavaScript runs a single event queue, so this will probably effectively be synchronous. However if we decided to scale this out with a cluster, we would probably invoke a cluster startup script that would perform this action and only then start the node processes. I would look at Promises to ensure completion of this action before starting the node processes.