<commit_before>var express = require('express');

var _ = require('./util');
var app = express();

var builder = require('./server-builder');

var db = require('./server-db');

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.cookieParser());
});

function errorCallback(res) {
  return function(err) {
    res.send(500, err);
  }
}

function sendResponse(res, promise) {
  promise.then(function(a) {
    res.send(a);
  }, errorCallback(res));
}


app.get('/', function(req, res) {
  res.send('hello world');
});

app.post('/builds', function(req, res) {
  var d = req.body;
  var buildId;
  db.builds.add('building', 'Starting Build', d.repoUrl, d.deployKey, d.refspec).then(function(build) {
    buildId = build.id;
    res.send(build);
    builder.build(build).then(function() {
      db.builds.setStatus('passed', 'Build Passed').then(function() {
        console.log('saved success status');
      }, function() {
        console.log('error saving success status');
      });
    }, function(err) {
      db.builds.setStatus('failed', err).then(function() {
        console.log('saved err status ', err);
      }, function() {
        console.log('error saving err status');
      });
    }, function(status) {
      db.builds.setStatus(buildId, status.status, status.message).then(function() {
        console.log('saved status ', status.message);
      }, function() {
        console.log('error saving status');
      });
    });
  }, errorCallback(res));
});

app.get('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.get(req.params.id));
});

app.del('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.remove(req.params.id));
});

app.get('/builds', function(req, res) {<commit_msg>fix race condition with notification<commit_after>var express = require('express');

var _ = require('./util');
var app = express();

var builder = require('./server-builder');

var db = require('./server-db');

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.cookieParser());
});

function errorCallback(res) {
  return function(err) {
    res.send(500, err);
  }
}

function sendResponse(res, promise) {
  promise.then(function(a) {
    res.send(a);
  }, errorCallback(res));
}


app.get('/', function(req, res) {
  res.send('hello world');
});

app.post('/builds', function(req, res) {
  var d = req.body;
  var hasErrorHappened = false;
  var hasPassHappened = false;
  var isNotifying = false;
  var buildId;
  db.builds.add('building', 'Starting Build', d.repoUrl, d.deployKey, d.refspec).then(function(build) {
    buildId = build.id;
    res.send(build);
    builder.build(build).then(function() {
      hasPassHappened = true;
      function reportPass() {
        db.builds.setStatus('passed', 'Build Passed').then(function() {
          console.log('saved success status');
        }, function() {
          console.log('error saving success status');
        });
      });
      if (isNotifying) afterNotify = reportPass;
      else reportPass();
    }, function(err) {
      hasErrorHappened = true;
      function reportFailure() {      
        db.builds.setStatus('failed', err).then(function() {
          console.log('saved err status ', err);
        }, function() {
          console.log('error saving err status');
        });
      }
      if (isNotifying) afterNotify = reportFailure;
      else reportFailure();
    }, function(status) {
      isNotifying = true;
      if(!hasErrorHappened && !hasPassHappened) {      
        db.builds.setStatus(buildId, status.status, status.message).then(function() {
          isNotifying = false;
          if(afterNotify) afterNotify();
          console.log('saved status ', status.message);
        }, function() {
          isNotifying = false;
          if(afterNotify) afterNotify();
          console.log('error saving status');
        });
      }
    });
  }, errorCallback(res));
});

app.get('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.get(req.params.id));
});

app.del('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.remove(req.params.id));
});

app.get('/builds', function(req, res) {
{'fraction_non_alphanumeric': 0.1793829227457546, 'fraction_numerical': 0.0014350633819660368, 'mean_word_length': 3.186186186186186, 'pattern_counts': {'":': 0, '<': 3, '<?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': '565590', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1318, 'n_words': 245}