var queries = require('./queries');
var insertPlayer = queries.insertPlayer;
var utility = require('./utility');
var getData = utility.getData;
var async = require('async');
var db = require('./db');
start();

function start()
{
    getSummaries(function(err)
    {
        if (err)
        {
            console.error(err);
        }
        return setTimeout(start, 1000);
    });
}

function getSummaries(cb)
{
    db.raw("select account_id from players offset random() * (select count(*) from players) limit 100").asCallback(function(err, results)
    {
        if (err)
        {
            return cb(err);
        }
        var container = utility.generateJob("api_summaries",
        {
            players: results.rows
        });
        getData(container.url, function(err, body)
        {
            if (err)
            {
                //couldn't get data from api, non-retryable
                return cb(JSON.stringify(err));
            }
            //player summaries response
            async.each(body.response.players, function(player, cb)
            {
                insertPlayer(db, player, cb);
            }, cb);
        });
    });
}
@@ -197,16 +197,362 @@
 ');%0A
-start();
+var count;%0Astart();%0AdoCount();%0A%0Afunction doCount()%0A%7B%0A    db.raw(%22select count(*) from players%22).asCallback(function(err, result)%0A    %7B%0A        if (err)%0A        %7B%0A            throw err;%0A        %7D%0A        count = Number(result.rows%5B0%5D.count);%0A        console.log(%22recomputed count: %25s%22, count);%0A        return setTimeout(doCount, 60 * 10 * 1000);%0A    %7D);%0A%7D
 %0A%0Afu
@@ -648,26 +648,17 @@
     
-console.error(
+throw 
 err
-)
 ;%0A  
@@ -740,24 +740,115 @@
 aries(cb)%0A%7B%0A
+    if (!count)%0A    %7B%0A        console.log('waiting for count');%0A        return cb();%0A    %7D%0A
     db.raw(%22
@@ -900,38 +900,9 @@
 ) * 
-(select count(*) from players)
+?
  lim
@@ -908,16 +908,25 @@
 mit 100%22
+, %5Bcount%5D
 ).asCall

improve profiler performance