<commit_before>var child_process = require('child_process');
var fs = require('fs');
var bson = require("bson");

var config = require('./config/gokart-timer');

var BSON = new bson.BSONPure.BSON();

console.log(config.title, 'starting...');

var gps = child_process.fork(__dirname + '/app/gps.js');
var web = child_process.fork(__dirname + '/app/web.js');

var children = [gps, web];

var cache = [];
var wcache = [];

gps.on('message', function(m) {
//  console.log('GPS:', new Date(), m);
  cache.push(m);
  wcache.push({gps: m});

  if (wcache.length > 5){
    web.send({gps_set: wcache});
    wcache=[];
  }

  if (cache.length > 1000) {
    var i = 0;
    var fn = 'gps-'+i+'.out';
    while(fs.existsSync(fn)) {
      i++;
      fn = 'gps-'+i+'.out';
    }
    fs.writeFile(fn, JSON.stringify(cache));
//    fs.writeFile(fn, BSON.serialize(cache, false, true, false));
    cache = [];
  }
});

gps.on('error', function(err) {
  console.log('GPS ERROR', err);
});
gps.on('exit', function() {
  children.pop();
});


web.on('message', function(m) {
});

web.on('error', function(err) {
  console.log('WEB ERROR', err);
});
web.on('exit', function() {
  children.pop();
});


process.on('SIGINT', function() {
  children.forEach(function(p) {
    p.kill('SIGINT');
  });
  var i = setInterval(function() {
    if (children.length == 0) {
      clearInterval(i);
      process.exit(0);
    }<commit_msg>write gps points on exit<commit_after>var child_process = require('child_process');
var fs = require('fs');
var bson = require("bson");

var config = require('./config/gokart-timer');

var BSON = new bson.BSONPure.BSON();

console.log(config.title, 'starting...');

var gps = child_process.fork(__dirname + '/app/gps.js');
var web = child_process.fork(__dirname + '/app/web.js');

var children = [gps, web];

var cache = [];
var wcache = [];

function write_cache() {
  var i = 0;
  var fn = 'gps-'+i+'.out';
  while(fs.existsSync(fn)) {
    i++;
    fn = 'gps-'+i+'.out';
  }
  console.log("Writing GPS to", fn);
  fs.writeFile(fn, JSON.stringify(cache));
//  fs.writeFile(fn, BSON.serialize(cache, false, true, false));
}

gps.on('message', function(m) {
//  console.log('GPS:', new Date(), m);
  cache.push(m);
  wcache.push({gps: m});

  if (wcache.length > 5){
    web.send({gps_set: wcache});
    wcache=[];
  }

  if (cache.length > 1000) {
    write_cache();
    cache = [];
  }
});

gps.on('error', function(err) {
  console.log('GPS ERROR', err);
});
gps.on('exit', function() {
  children.pop();
});


web.on('message', function(m) {
});

web.on('error', function(err) {
  console.log('WEB ERROR', err);
});
web.on('exit', function() {
  children.pop();
});


process.on('SIGINT', function() {
  write_cache();
  children.forEach(function(p) {
    p.kill('SIGINT');
  });
  var i = setInterval(function() {
    if (children.length == 0) {
      clearInterval(i);
      process.exit(0);
    }
{'fraction_non_alphanumeric': 0.22317744154057773, 'fraction_numerical': 0.005502063273727648, 'mean_word_length': 3.5811023622047244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3640913', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1094, 'n_words': 212}