Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active December 10, 2015 21:39
Show Gist options
  • Save thbar/4496917 to your computer and use it in GitHub Desktop.
Save thbar/4496917 to your computer and use it in GitHub Desktop.
A quick node.js script to do many pings to a load-balanced app until a given instance behind the load-balancer replies. Please note that I'm barely starting to use node.js, any improvement is most welcome.
express = require 'express'
rest = require 'restler'
app = express()
# a little url expected to return a x-commit header with the hostname
url = process.env.MONITORED_URL
attempts = 4
app.get '/', (req, res) =>
res.send("OK")
app.get '/check/service/:id', (req, res) ->
wanted_host = "my-app-default-#{req.params.id.replace ".", "-"}"
remaining = attempts
scan = (complete) =>
rest.get(url).on 'complete', complete
complete = (data, response) =>
commit = response.headers['x-commit']
host = commit && commit.split(',')[0]
if host == wanted_host
res.send(200, "#{host} is up!")
else
remaining -= 1
if remaining < 1
res.send(500, "#{wanted_host} cannot be reached!")
else
scan(complete)
scan(complete)
port = process.env.PORT || 5000
app.listen port
console.log "Server started on port #{port}"
{
"name": "node-example",
"version": "0.0.1",
"dependencies": {
"express": "3.x",
"coffee-script": "1.4.x",
"restler": "2.0.1"
},
"devDependencies": {
"supervisor": "0.5.0"
},
"engines": {
"node": "0.8.x",
"npm": "1.1.x"
}
}
web: node run.js
require("coffee-script");
require("./app.coffee");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment