<commit_before>#!/usr/bin/env node

var cluster = require('cluster');

function stdout(str){
  console.log("[" + process.pid + "] " + new Date().toISOString() + ": " + str)
}

function stderr(str){
  console.error("ERROR: [" + process.pid + "] " + new Date().toISOString() + ": " + str)
}

if(cluster.isMaster){

  var N = 1 || require('os').cpus().length;
  for(var i = 0; i < N; i++){
    stdout('forking ... ' + (i + 1) + ' of ' + N)
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    stdout('worker ' + worker.process.pid + ' died');
  });

} // /cluster.isMaster

if(cluster.isWorker){

  /* General */
  var fs = require('fs');
  var async       = require('async');
  var io          = require('socket.io');
  var moment      = require('moment');
  var assert      = require('assert');

  /* Express.io */
  var http        = require('http');
  var https       = require('https');
  var express     = require('express');
  var helmet      = require('helmet');
  var bodyParser  = require('body-parser');
  var compression = require('compression');

  /* Socket.io helper (communication between R and app via MongoDB) */
  var mubsub      = require('mubsub');

  /* Load config.json file */
  var config;
  var loadConfig = function(callback){

    stdout('Loading config.json file...');
    assert(fs.existsSync(process.cwd() + '/config.json'), 'Configuration file config.json should exist.');

    fs.readFile(process.cwd() + '/config.json', { enconding: 'utf8'}, function(err, data){
      if(err) callback(err);
      config = JSON.parse(data);
      callback();
    });

  }

  var checkConfig = function(callback){
    assert(config.db, 'Mongo should be configured in config.json.');
    assert(config.db.database, 'A Mongo database should be configured in config.json.');
    assert(config.db && config.db.database, 'Mongo should be configured in config.json for Mubsub.');
    assert(config.admin && config.collections, 'Default users and collections should be in config.json.');    
  }

  // Shared database client binding
  var db = null;
  var dbConnect = function(callback){

    stdout('Connecting to MongoDB...');
    var mongodb = require('mongodb');
    var driver  = mongodb.MongoClient;

    driver.connect('mongodb://'+ config.db.host +':'+ config.db.port +'/' + config.db.database, function(err, client) {
      if(err){
        stderr('MongoDB connection error: ', err);
        callback(err);
      }
      stdout('Connected to MongoDB!');
      db = client;
      callback();

      // Insert admin if it doesn't exist.
      db.collection('settings').find({ _id: 'admin' }, function(err, data){
        if(err) {
          stderr('Could not find admin user...');
          return;
        }
        if(!data){
        db.collection('settings').insert({ event: channel, "message": message }, function(err, data){
          if(err) stderr('trigger: ' + err);
        })

        }<commit_msg>Default admin user fix<commit_after>#!/usr/bin/env node

var cluster = require('cluster');

function stdout(str){
  console.log("[" + process.pid + "] " + new Date().toISOString() + ": " + str)
}

function stderr(str){
  console.error("ERROR: [" + process.pid + "] " + new Date().toISOString() + ": " + str)
}

if(cluster.isMaster){

  var N = 1 || require('os').cpus().length;
  for(var i = 0; i < N; i++){
    stdout('forking ... ' + (i + 1) + ' of ' + N)
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    stdout('worker ' + worker.process.pid + ' died');
  });

} // /cluster.isMaster

if(cluster.isWorker){

  /* General */
  var fs = require('fs');
  var async       = require('async');
  var io          = require('socket.io');
  var moment      = require('moment');
  var assert      = require('assert');

  /* Express.io */
  var http        = require('http');
  var https       = require('https');
  var express     = require('express');
  var helmet      = require('helmet');
  var bodyParser  = require('body-parser');
  var compression = require('compression');

  /* Socket.io helper (communication between R and app via MongoDB) */
  var mubsub      = require('mubsub');

  /* Load config.json file */
  var config;
  var loadConfig = function(callback){

    stdout('Loading config.json file...');
    assert(fs.existsSync(process.cwd() + '/config.json'), 'Configuration file config.json should exist.');

    fs.readFile(process.cwd() + '/config.json', { enconding: 'utf8'}, function(err, data){
      if(err) callback(err);
      config = JSON.parse(data);
      callback();
    });

  }

  var checkConfig = function(callback){
    assert(config.db, 'Mongo should be configured in config.json.');
    assert(config.db.database, 'A Mongo database should be configured in config.json.');
    assert(config.db && config.db.database, 'Mongo should be configured in config.json for Mubsub.');
    assert(config.admin && config.collections, 'Default users and collections should be in config.json.');    
  }

  // Shared database client binding
  var db = null;
  var dbConnect = function(callback){

    stdout('Connecting to MongoDB...');
    var mongodb = require('mongodb');
    var driver  = mongodb.MongoClient;

    driver.connect('mongodb://'+ config.db.host +':'+ config.db.port +'/' + config.db.database, function(err, client) {
      if(err){
        stderr('MongoDB connection error: ', err);
        callback(err);
      }
      stdout('Connected to MongoDB!');
      db = client;
      callback();

      // Insert admin if it doesn't exist.
      db.collection('settings').find({ _id: 'admin' }).toArray(function(err, data){
        if(err) {
          stderr('Error trying to find admin user...');
          return;
        }
        if(!data.length){
          db.collection('settings').insert(config.admin, function(err, data){
            if(err) stderr('Could not add default admin user: ' + err);
            else stdout('Added default admin user.')
          })

        }
{'fraction_non_alphanumeric': 0.1705607476635514, 'fraction_numerical': 0.0013351134846461949, 'mean_word_length': 2.8196303377947736, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 3, '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': '1288913', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1798, 'n_words': 462}