Skip to content

Commit

Permalink
allow endpoint to crash, without compromising future flush
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia committed Feb 8, 2012
1 parent 79e4a4e commit a1c4294
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
73 changes: 46 additions & 27 deletions lib/pixel-ping.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
(function() {
var VERSION, config, configPath, emptyHeaders, endHeaders, endParams, endpoint, flush, fs, http, log, pixel, pixelHeaders, querystring, record, serialize, server, store, url;
var __hasProp = Object.prototype.hasOwnProperty;

fs = require('fs');

url = require('url');

http = require('http');

querystring = require('querystring');

VERSION = '0.1.2';

store = {};

record = function(params) {
var key;
if (!(key = params.query == null ? undefined : params.query.key)) {
return null;
}
var key, _ref;
if (!(key = (_ref = params.query) != null ? _ref.key : void 0)) return;
store[key] || (store[key] = 0);
return store[key] += 1;
};

serialize = function() {
var data;
data = {
json: JSON.stringify(store)
};
store = {};
if (config.secret) {
data.secret = config.secret;
}
if (config.secret) data.secret = config.secret;
return querystring.stringify(data);
};

flush = function() {
var data, request;
log(store);
if (!(config.endpoint)) {
return null;
}
if (!config.endpoint) return;
data = serialize();
endHeaders['Content-Length'] = data.length;
request = endpoint.request('POST', endParams.pathname, endHeaders);
request.write(data);
request.end();
return request.on('response', function(response) {
request.on('response', function(response) {
return console.info('--- flushed ---');
});
return request.end();
};

log = function(hash) {
var _a, _b, hits, key;
_a = []; _b = hash;
for (key in _b) {
if (!__hasProp.call(_b, key)) continue;
hits = _b[key];
_a.push(console.info("" + (hits) + ":\t" + (key)));
var hits, key, _results;
_results = [];
for (key in hash) {
hits = hash[key];
_results.push(console.info("" + hits + ":\t" + key));
}
return _a;
return _results;
};

server = http.createServer(function(req, res) {
var params;
params = url.parse(req.url, true);
Expand All @@ -64,45 +67,61 @@
}
return null;
});

configPath = process.argv[2];
if (('-v' === configPath || '-version' === configPath || '--version' === configPath)) {
console.log("Pixel Ping version " + (VERSION));

if (configPath === '-v' || configPath === '-version' || configPath === '--version') {
console.log("Pixel Ping version " + VERSION);
process.exit(0);
}
if (!configPath || (('-h' === configPath || '-help' === configPath || '--help' === configPath))) {

if (!configPath || (configPath === '-h' || configPath === '-help' || configPath === '--help')) {
console.error("Usage: pixel-ping path/to/config.json");
process.exit(0);
}

config = JSON.parse(fs.readFileSync(configPath).toString());

pixel = fs.readFileSync(__dirname + '/pixel.gif');

pixelHeaders = {
'Cache-Control': 'private, no-cache, proxy-revalidate',
'Content-Type': 'image/gif',
'Content-Disposition': 'inline',
'Content-Length': pixel.length
};

emptyHeaders = {
'Content-Type': 'text/html',
'Content-Length': '0'
};

if (config.endpoint) {
console.info("Flushing hits to " + (config.endpoint));
console.info("Flushing hits to " + config.endpoint);
endParams = url.parse(config.endpoint);
endpoint = http.createClient(endParams.port || 80, endParams.hostname);
endpoint.on('error', function(e) {
return console.log("--- cannot connect to endpoint : " + e.message);
});
endHeaders = {
'host': endParams.host,
'Content-Type': 'application/x-www-form-urlencoded'
};
} else {
console.warn("No endpoint set. Hits won't be flushed, add \"endpoint\" to " + (configPath) + ".");
console.warn("No endpoint set. Hits won't be flushed, add \"endpoint\" to " + configPath + ".");
}

process.on('SIGUSR1', function() {
console.log('Got SIGUSR1. Forcing a flush:');
return flush();
});

process.on('uncaughtException', function(err) {
return console.error("Uncaught Exception: " + (err));
return console.error("Uncaught Exception: " + err);
});

server.listen(config.port, config.host);

setInterval(flush, config.interval * 1000);
})();

}).call(this);
4 changes: 3 additions & 1 deletion src/pixel-ping.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ flush = ->
endHeaders['Content-Length'] = data.length
request = endpoint.request 'POST', endParams.pathname, endHeaders
request.write data
request.end()
request.on 'response', (response) ->
console.info '--- flushed ---'
request.end()

# Log the contents of the `store` to **stdout**. Happens on every flush, so that
# there's a record of hits if something goes awry.
Expand Down Expand Up @@ -93,6 +93,8 @@ if config.endpoint
console.info "Flushing hits to #{config.endpoint}"
endParams = url.parse config.endpoint
endpoint = http.createClient endParams.port or 80, endParams.hostname
endpoint.on 'error', (e) ->
console.log "--- cannot connect to endpoint : #{e.message}"
endHeaders =
'host': endParams.host
'Content-Type': 'application/x-www-form-urlencoded'
Expand Down

0 comments on commit a1c4294

Please sign in to comment.