node.js - node js gettting error of activity timeout or database connection lost -


i using node js script sync data(for multiple users) geotab server apis local db , using below code

     var syncusers = function () {      (var = 0; < geotabusers.length; i++) {            if (apiinstanse[geotabusers[i].userid] == undefined)          {               apiinstanse[geotabusers[i].userid] = new api(geotabusers[i].apiusername, geotabusers[i].apipassword, geotabusers[i].apidatabase, js_lang.geotab_server);           }            syncstatusdata(apiinstanse[geotabusers[i].userid], i, userinfo[geotabusers[i].userid].statusdatafromversion, geotabusers[i].userid, userinfo[geotabusers[i].userid].currentcompany, geotabusers[i].apiusername, geotabusers[i].apipassword, geotabusers[i].apidatabase);  }    var syncstatusdata = function (api, i, fromversion, userid, currentcompany, apiusername, apipassword, apidatabase){  try {     api.call(js_lang.geotab_getfeed_method, {         typename: js_lang.geotab_status_data,         resultslimit: js_lang.geotab_api_limit,         fromversion: fromversion     }, function (err, data) {          if (err) {             console.log('api call error:', userid);             console.log('apiusername:', apiusername);             console.log('apipassword:', apipassword);             console.log('apidatabase:', apidatabase);             console.log('error', err);             apiinstanse[userid] = new api(apiusername, apipassword, apidatabase, js_lang.geotab_server);             return;         }          var insertstatus = [];          var sql = "insert " + js_lang.table_status_data + " (companyid,datetime,deviceid ,diagnosticid,value,version,uniqueid,userid,unitofmeasure ) values ?";           //iterate data         if (data.data != undefined)         {             (var key in data.data) {                  if (diagnosticlist[data.data[key].diagnostic.id] == undefined)                 {                     continue;                 }                  var thisdate = moment(new date(data.data[key].datetime));                  var thisdaydate = thisdate.format("yyyy-mm-dd");                  //prepare data insert                 var insertrow = [                     currentcompany,                     thisdate.format("yyyy-mm-dd hh:mm:ss"),                     data.data[key].device.id,                     data.data[key].diagnostic.id,                     data.data[key].data,                     data.data[key].version,                     data.data[key].id,                     userid,                     diagnosticlist[data.data[key].diagnostic.id].unitofmeasure                 ];                 insertstatus.push(insertrow);                  var todaydate = moment(new date());                 var todaydaydate = todaydate.format("yyyy-mm-dd");                  //send alert in case of current day data                 if (todaydaydate == thisdaydate)                 {                     //send mails in case of high pressure                     if (diagnosticlist[data.data[key].diagnostic.id].unitofmeasure == js_lang.geotab_pressure_unit_str &&                             data.data[key].data > js_lang.max_pressure &&                             alerttemplates[userid] != undefined)                     {                         console.log('alert time');                           if (alerttemplates[userid] != undefined)                         {                              (var templateindex = 0; templateindex < alerttemplates[userid].length; templateindex++) {                                 var template = alerttemplates[userid][templateindex];                                 var res = template.devices.split(",");                                  var index = function_class.contains.call(res, data.data[key].device.id)                                  if (index)                                 {                                     var emailsubject = 'high pressure alert';                                      if (userdevices[userid][data.data[key].device.id].name != undefined)                                     {                                         var emailtext = 'vehicle:' + userdevices[userid][data.data[key].device.id].name;                                     }                                     else                                     {                                         var emailtext = '';                                     }                                      var toemails = template.emails;                                      var emailhtml = 'vehicle:' + userdevices[userid][data.data[key].device.id].name + '<br>' + diagnosticlist[data.data[key].diagnostic.id].name + ' <br> value:' + data.data[key].data + ' ' + js_lang.geotab_pressure_unit_pa + '<br>\ ' + js_lang.date_time + ':' + thisdate.format("yyyy-mm-dd hh:mm:ss");                                      //call mail function                                     sendemail(toemails, emailsubject, emailtext, emailhtml);                                  }                               }                         }                        }                 }              }         }          if (insertstatus.length > 0)         {             connection.query(sql, [insertstatus], function (err) {                 if (err)                     throw err;                                   connection.query('update ' + js_lang.table_user + ' set statusdatafromversion = ? id = ?',                         [data.toversion, userid]);              });         }         else {             console.log('update user:', userid);              connection.query('update ' + js_lang.table_user + ' set statusdatafromversion = ? id = ?',                     [data.toversion, userid]);            }          if ((geotabusers.length - 1) == i)         {             console.log('loop ended');             continuesync();         }     }); } catch (e) {     continuesync(); }  }  var continuesync = function () { settimeout(function () {      connection.end();      geotabusers = [];     userdevices = [];     diagnosticlist = [];     userinfo = [];     alerttemplates = {};      eventemitter.emit('getusers'); }, 60000); 

}

its work fine few iteration getting random errors like

    events.js:85           throw er; // unhandled 'error' event             ^ error: quit inactivity timeout 

or

events.js:85       throw er; // unhandled 'error' event             ^ error: connection lost: server closed connection. 

i getting error while executing app on local, remove problem have used pm2 node module process management.now working fine.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -