Skip to content

Instantly share code, notes, and snippets.

@smerrill
Created June 8, 2010 03:16
Show Gist options
  • Save smerrill/429553 to your computer and use it in GitHub Desktop.
Save smerrill/429553 to your computer and use it in GitHub Desktop.
var sys = require("sys"),
http = require("http"),
growl = require("./node-growl/lib/growl"),
child_process = require("child_process");
function createClient() {
var i = http.createClient(80, website);
// Handle a connection error.
// Thanks to http://rentzsch.tumblr.com/post/664884799/node-js-handling-refused-http-client-connections.
i.addListener('error', function(connectionException) {
if (connectionException.errno === process.ECONNREFUSED) {
failure('Come on, fhqwhgads.');
// Reset client.
client = createClient();
}
else {
sys.log(connectionException);
}
});
return i;
}
function connect() {
var request = client.request('GET', '/', {'Host': website});
// Handle a request when we got some sort of response from the server.
request.addListener('response', function(response){
// We got a good kind of response!
if (response.statusCode == 200 || response.statusCode == 301 || response.statusCode == 302) {
success('The system is up.');
}
// Something's up.
else {
failure('The system is down.');
}
});
request.end();
}
function success(message) {
growl.notify(message);
sys.puts(message);
clearInterval(requestInterval);
// Use Darwin / OS X's open command to open a web browser to view said site.
child_process.exec('open http://' + website, function(error, stdout, stdin) {
// Quit!
process.exit(error);
});
}
function failure(message) {
growl.notify(message);
sys.puts(message);
}
var website = '127.0.0.1';
var client = createClient();
var requestInterval = setInterval(connect, '5000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment