Skip to content

Instantly share code, notes, and snippets.

@wedtm
Created December 7, 2011 09:34
Show Gist options
  • Save wedtm/1442170 to your computer and use it in GitHub Desktop.
Save wedtm/1442170 to your computer and use it in GitHub Desktop.
(function() {
var Servers, calculateRank, executeCheck, mongo, net, processCheck, r, redis, remaining, start, startTimer, start_time, util;
net = require('net');
redis = require('redis');
util = require('util');
r = redis.createClient(6379, "<< REDIS CONNECTION STRING >>");
mongo = require('mongoskin');
remaining = 0;
r.on("error", function(err) {
return console.log("Redis Error: " + err);
});
executeCheck = function(server) {
var conn;
conn = net.createConnection(server.port, server.ip);
conn.setTimeout = 1500;
conn.on("connect", function() {
processCheck(server, true);
return conn.end();
});
return conn.on("error", function() {
processCheck(server, false);
return conn.end();
});
};
calculateRank = function(server) {
var key, rank;
rank = 5;
key = "checks:" + server._id;
r.exists(key, function(err, obj) {
var total;
if (obj !== 1) return;
total = 0;
r.llen(key, function(err, obj) {
return total = parseInt(obj);
});
return r.lrange(key, 1, -1, function(err, checks) {
var check, good, _fn, _i, _len;
good = 0;
_fn = function(check) {
var data;
data = JSON.parse(check);
if (data.result === true) return good = good + 1;
};
for (_i = 0, _len = checks.length; _i < _len; _i++) {
check = checks[_i];
_fn(check);
}
server.percentage = (good / total * 100) / 100;
if (new Date(server.created_at).getTime() > new Date().getTime() - 604800) {
rank = rank - 1;
}
rank = rank * server.percentage;
if (server.votes > 0) rank = rank + (server.votes * 0.015);
if (server.premium) rank = rank * 1.1;
return Servers.update({
_id: server._id
}, {
$set: {
rank: rank
}
});
});
});
return remaining -= 1;
};
processCheck = function(server, res) {
var check_time, data, key;
check_time = Math.round((new Date()).getTime() / 1000);
data = {
result: res,
timestamp: check_time
};
key = "checks:" + server._id;
r.lpush(key, JSON.stringify(data));
r.set("checks:" + server._id + ":last", parseInt(check_time));
r.ltrim(key, 0, 168);
Servers.update({
_id: server._id
}, {
$set: {
last_checked_at: check_time
}
});
return calculateRank(server);
};
Servers = mongo.db("<< MONGODB CONNECTION STRING >>").collection('servers');
console.log("Housewife has woken up! Time to get to work.");
start = function(callback) {
var query;
query = {
disabled: {
$ne: true
},
suspended: {
$ne: true
}
};
return Servers.find(query, {
description: 0
}, function(err, cursor) {
cursor.count(function(err, count) {
console.log("" + count + " servers selected...");
return remaining = count;
});
cursor.each(function(err, server) {
if (server !== null) return executeCheck(server);
});
return startTimer();
});
};
startTimer = function() {
return setTimeout(function() {
if (remaining <= 0) {
console.log("CHECKS COMPLETE. FIN. --");
console.log("Total Time: " + ((new Date().getTime() - start_time) / 1000) + " seconds");
return process.exit(0);
} else {
console.log("" + remaining + " servers left...");
return startTimer();
}
}, 1000);
};
start_time = new Date().getTime();
start(function() {
return console.log("Finished");
});
setTimeout(function() {
console.log("Master Timeout Reached, killing process.");
return process.exit(1);
}, 60 * 10 * 1000);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment