title: Why nodejs process lost data?
body: <p>I'm really confused at this point with losing data and can't figure out why. So I write a service to send GPS info from a device to an endpoint. </p>
  
  <p>I'm using pm2 to launch my processes but the problem is this service sometimes don't send the info to the endpoint, and the device is sending data. The solution until now was restarting the instance in pm2. But this sometimes is not viable because I create a crontab to restart the GPS instance in pm2 every 45 minutes but it happens to lose information in a time window &lt; 45 min this will not work...</p>
  
  <p>I can't figure this out. Why I lose the data and restarting everything is okay?  I saw a post in stack overflow about lost data in node.js process when the child sends the data to the parent and I read about 2 possible causes:</p>
  
  <ol>
  <li>The child is not reading the GPS data info quickly enough in time to make a post and send info.</li>
  <li>The child needs to make a JSON.stringify of the content sent to parent and parent needs to make a JSON.parse of the info received.</li>
  </ol>
  
  <p>Here's my code:</p>
  
  <pre><code>var child_process = require("child_process");
  var argv = require('minimist')(process.argv.slice(2));
  
  //ex: nsGPSService.js -d 1111-11-11-111
  var deviceId = argv.d;
  var processDevices = [];
  
  function runParent() {
    setTimeout(function() {
      return Database.Devices.getDevices().then(function(devices) {
        return new Promise(function(resolve, reject) {
          async.each(devices, function(device, callback) {
            var result = _.filter(processDevices, { "id": device.id });
  
            if (result.length == 0) {
              var process = child_process.fork(__dirname + '/nsGPSService.js', ["-d", device.id]);
  
              processDevices.push({ "process": process, "id": device.id });
              process.on('message', function(data) {
                //receber mensagens do filho
                if (data.reason == "deleted") {
                  //child end process and alerts parent to remove from the list
                  var index = _.findIndex(processDevices, { "id": data.deviceId });
                  processDevices.splice(index, 1);
                }
              });
              process.on('exit', function(code) {});
              process.on("uncaughtException", function(error) {
                process.exit(1);
              });
            }
  
            callback();
          }, function(error) {
            error ? reject(error) : resolve();
          });
        }).then(function() {
          runParent()
        }).catch(function(error) {
          runParent()
        });
      });
    }, 5000);
  }
  
  if (!deviceId) {
    return runParent();
  }
  
  
  function runChild(id) {
    setTimeout(function() {
      return Database.Devices.getDeviceById(id).then(function(device) {
        if (!device) {
          proccess.send({
            "deviceId": id,
            "reason": "deleted"
          });
          process.exit();
          return;
        }
        return Controllers.Gps.getRadioInfo('gps', 'info', {}, device).then(function(data) {
          return Controllers.Gps.sendDeviceInfo(data, device);
        }).then(function() {
          return runChild(id);
        }).catch(function(e) {
          return runChild(id);
        });
      });
    }, 5000);
  }
  </code></pre>
  
  <p>I really need to figure this out because I never know when I need to restart the service because I'm not getting info when in reality I'm receiving...</p>
  
  <p>Which solution is really viable in my scenario and anyone can figure this problem?</p>