server/boot/load-model.js
function loadData(app, apimanager, models, reload, uid) {
  var currdir = getPreviousSnapshotDir();
  var snapdir;
  var snapshotID = getSnapshotID();
  var populatedSnapshot = false;

  async.series([
    function(callback) {
      logger.debug('apimanager before pullFromAPIm: %j', apimanager);
      if (apimanager.host) {
          // && apimanager.handshakeOk << shouldn't call if handshake failed.. not ready #TODO
          // && apimanager.webhooksOk << shouldn't call if handshake failed.. not ready #TODO
          // don't look for successful handshake/webhooks currently because UT depends on this
        // we have an APIm, handshake succeeded, so try to pull data..
        pullFromAPIm(apimanager, currdir, snapshotID, function(err, dir) {
          if (err) {
            if (uid) {
              snapshotID = uid;
            } // in case of error, try the previous snapshot
          }
          snapdir = dir; // even in case of error, we need to try loading from the file system
          callback();
        });
      } else {
        snapdir = '';
        callback();
      }
    },
    // populate snapshot model
    function(callback) {
      populateSnapshot(app, snapshotID, callback);
    },
    // load current config
    function(callback) {
      populatedSnapshot = true;
      loadConfig(app,
                 apimanager,
                 models,
                 currdir,
                 snapdir,
                 snapshotID,
                 callback);
    } ],
    function(err, results) {
      if (!reload) {
        return;
      }
      var interval = apimanager.maxRefresh;
      // if no error and APIs specified, do not agressively reload config
      if (!err && apimanager.host && (http || https)) {
        apimanager.startupRefresh = interval;
        // if the previous snapshot hasn't be loaded, delete it
        if (uid && snapshotID !== uid) {
          try {
            fs.removeSync(currdir);
          } catch (e) {
            logger.error(e);
            //continue
          }
        }
      } else if (apimanager.startupRefresh < apimanager.maxRefresh) {
        // try agressively at first, and slowly back off
        interval = apimanager.startupRefresh;
        apimanager.startupRefresh *= 2;
      }
      if (err) {
        if (populatedSnapshot) {
          releaseSnapshot(app, snapshotID, function(err) {
            if (err) { /* suppress eslint handle-callback-err */ }
            process.send({ LOADED: false });
          });
        } else {
          process.send({ LOADED: false });
        }
      } else if (!apimanager.host || http || https) {
        // neither http nor https would be set if there were no APIs
        // defined; if no APIs defined, let's try again in a while
        process.send({ LOADED: true, https: https });
      }
      setImmediate(scheduleLoadData,
                   app,
                   interval,
                   apimanager,
                   models);
    });
}
function loadData(app, apimanager, models, reload, uid) {
  var currdir = getPreviousSnapshotDir();
  var snapdir;
  var snapshotID = getSnapshotID();
  var populatedSnapshot = false;

  async.series([
    function(callback) {
      logger.debug('apimanager before pullFromAPIm: %j', apimanager);
      if (apimanager.host) {
          // && apimanager.handshakeOk << shouldn't call if handshake failed.. not ready #TODO
          // && apimanager.webhooksOk << shouldn't call if handshake failed.. not ready #TODO
          // don't look for successful handshake/webhooks currently because UT depends on this
        // we have an APIm, handshake succeeded, so try to pull data..
        pullFromAPIm(apimanager, currdir, snapshotID, function(err, dir) {
          if (err) {
            if (uid) {
              snapshotID = uid;
            } // in case of error, try the previous snapshot
          }
          snapdir = dir; // even in case of error, we need to try loading from the file system
          callback();
        });
      } else {
        snapdir = '';
        callback();
      }
    },
    // populate snapshot model
    function(callback) {
      populateSnapshot(app, snapshotID, callback);
    },
    // load current config
    function(callback) {
      populatedSnapshot = true;
      loadConfig(app,
                 apimanager,
                 models,
                 currdir,
                 snapdir,
                 snapshotID,
                 callback);
    } ],
    function(err, results) {
      if (!reload) {
        return;
      }
      var interval = apimanager.maxRefresh;
      // if no error and APIs specified, do not agressively reload config
      if (!err && apimanager.host && (http || https)) {
        apimanager.startupRefresh = interval;
        // if the previous snapshot hasn't be loaded, delete it
        if (uid && snapshotID !== uid) {
          try {
            fs.removeSync(currdir);
          } catch (e) {
            logger.error(e);
            //continue
          }
        }
      } else if (apimanager.startupRefresh < apimanager.maxRefresh) {
        // try agressively at first, and slowly back off
        interval = apimanager.startupRefresh;
        apimanager.startupRefresh *= 2;
      }
      if (err) {
        if (populatedSnapshot) {
          releaseSnapshot(app, snapshotID, function(err) {
            if (err) { /* suppress eslint handle-callback-err */ }
            process.send({ LOADED: false });
          });
        } else {
          process.send({ LOADED: false });
        }
      } else if (!apimanager.host || http || https) {
        // neither http nor https would be set if there were no APIs
        // defined; if no APIs defined, let's try again in a while
        process.send({ LOADED: true, https: https });
      }
      setImmediate(scheduleLoadData,
                   app,
                   interval,
                   apimanager,
                   models);
    });
}
function loadData ( app , apimanager , models , reload , uid ) { var currdir = getPreviousSnapshotDir ( ) ; var snapdir ; var snapshotID = getSnapshotID ( ) ; var populatedSnapshot = false ; async . series ( [ function ( callback ) { logger . debug ( 'apimanager before pullFromAPIm: %j' , apimanager ) ; if ( apimanager . host ) { // && apimanager.handshakeOk << shouldn't call if handshake failed.. not ready #TODO // && apimanager.webhooksOk << shouldn't call if handshake failed.. not ready #TODO // don't look for successful handshake/webhooks currently because UT depends on this // we have an APIm, handshake succeeded, so try to pull data.. pullFromAPIm ( apimanager , currdir , snapshotID , function ( err , dir ) { if ( err ) { if ( uid ) { snapshotID = uid ; } // in case of error, try the previous snapshot } snapdir = dir ; // even in case of error, we need to try loading from the file system callback ( ) ; } ) ; } else { snapdir = '' ; callback ( ) ; } } , // populate snapshot model function ( callback ) { populateSnapshot ( app , snapshotID , callback ) ; } , // load current config function ( callback ) { populatedSnapshot = true ; loadConfig ( app , apimanager , models , currdir , snapdir , snapshotID , callback ) ; } ] , function ( err , results ) { if ( ! reload ) { return ; } var interval = apimanager . maxRefresh ; // if no error and APIs specified, do not agressively reload config if ( ! err && apimanager . host && ( http || https ) ) { apimanager . startupRefresh = interval ; // if the previous snapshot hasn't be loaded, delete it if ( uid && snapshotID !== uid ) { try { fs . removeSync ( currdir ) ; } catch ( e ) { logger . error ( e ) ; //continue } } } else if ( apimanager . startupRefresh < apimanager . maxRefresh ) { // try agressively at first, and slowly back off interval = apimanager . startupRefresh ; apimanager . startupRefresh *= 2 ; } if ( err ) { if ( populatedSnapshot ) { releaseSnapshot ( app , snapshotID , function ( err ) { if ( err ) { /* suppress eslint handle-callback-err */ } process . send ( { LOADED : false } ) ; } ) ; } else { process . send ( { LOADED : false } ) ; } } else if ( ! apimanager . host || http || https ) { // neither http nor https would be set if there were no APIs // defined; if no APIs defined, let's try again in a while process . send ( { LOADED : true , https : https } ) ; } setImmediate ( scheduleLoadData , app , interval , apimanager , models ) ; } ) ; }
Loads the data into models, and periodically refreshes the data
@param {???} app - loopback application
@param {Object} config - configuration pointing to APIm server
@param {Array} models - instances of ModelType to populate with data
@param {bool} reload - set a timer to trigger a future reload
Loads the data into models and periodically refreshes the data