Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow to discard or not data on endpoitn failure

  • Loading branch information...
commit e90db2da1a1a3386ac4b52749506bdeed80bfa64 1 parent a1c4294
@cyberdelia cyberdelia authored
View
3  config.example.json
@@ -2,5 +2,6 @@
"port": "9187",
"host": "localhost",
"interval": 60,
- "endpoint": "http://www.example.com/internal/save_hits"
+ "endpoint": "http://www.example.com/internal/save_hits",
+ "discard": true
}
View
3  index.html
@@ -198,7 +198,8 @@ <h2 id="usage">Usage</h2>
"host": "127.0.0.1",
"port": "9187",
"interval": 600,
- "endpoint": "http://www.example.com/internal/save_hits"
+ "endpoint": "http://www.example.com/internal/save_hits",
+ "discard": false
}</pre>
<h2 id="changes">Change Log</h2>
View
9 lib/pixel-ping.js
@@ -1,5 +1,5 @@
(function() {
- var VERSION, config, configPath, emptyHeaders, endHeaders, endParams, endpoint, flush, fs, http, log, pixel, pixelHeaders, querystring, record, serialize, server, store, url;
+ var VERSION, config, configPath, emptyHeaders, endHeaders, endParams, endpoint, flush, fs, http, log, pixel, pixelHeaders, querystring, record, reset, serialize, server, store, url;
fs = require('fs');
@@ -25,11 +25,14 @@
data = {
json: JSON.stringify(store)
};
- store = {};
if (config.secret) data.secret = config.secret;
return querystring.stringify(data);
};
+ reset = function() {
+ return store = {};
+ };
+
flush = function() {
var data, request;
log(store);
@@ -39,6 +42,7 @@
request = endpoint.request('POST', endParams.pathname, endHeaders);
request.write(data);
request.on('response', function(response) {
+ reset();
return console.info('--- flushed ---');
});
return request.end();
@@ -101,6 +105,7 @@
endParams = url.parse(config.endpoint);
endpoint = http.createClient(endParams.port || 80, endParams.hostname);
endpoint.on('error', function(e) {
+ if (config.discard) reset();
return console.log("--- cannot connect to endpoint : " + e.message);
});
endHeaders = {
View
7 src/pixel-ping.coffee
@@ -24,10 +24,13 @@ record = (params) ->
# `secret` token to the request object, if configured.
serialize = ->
data = json: JSON.stringify(store)
- store = {}
data.secret = config.secret if config.secret
querystring.stringify data
+# Reset the `store`.
+reset = ->
+ store = {}
+
# Flushes the `store` to be saved by an external API. The contents of the store
# are sent to the configured `endpoint` URL via HTTP POST. If no `endpoint` is
# configured, this is a no-op.
@@ -39,6 +42,7 @@ flush = ->
request = endpoint.request 'POST', endParams.pathname, endHeaders
request.write data
request.on 'response', (response) ->
+ reset()
console.info '--- flushed ---'
request.end()
@@ -94,6 +98,7 @@ if config.endpoint
endParams = url.parse config.endpoint
endpoint = http.createClient endParams.port or 80, endParams.hostname
endpoint.on 'error', (e) ->
+ reset() if config.discard
console.log "--- cannot connect to endpoint : #{e.message}"
endHeaders =
'host': endParams.host
Please sign in to comment.
Something went wrong with that request. Please try again.