From b53bcceb6dfae055f409cc349b96f5121b3ef41b Mon Sep 17 00:00:00 2001 From: javi Date: Wed, 7 Dec 2011 13:06:23 +0100 Subject: [PATCH] lovely fixes --- lib/cartodb/varnish.js | 22 ++++++++++++++++------ test/acceptance/varnish.js | 5 +++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/cartodb/varnish.js b/lib/cartodb/varnish.js index 3e5f0c6f..a60d119f 100644 --- a/lib/cartodb/varnish.js +++ b/lib/cartodb/varnish.js @@ -21,7 +21,7 @@ function VarnishClient(host, port, ready_callback) { } function connect() { - if(connecting) return; + if(connecting || connected ) return; connecting = true; log("VARNISH: connection"); ready = false; @@ -30,8 +30,8 @@ function VarnishClient(host, port, ready_callback) { client.on('connect', function () { log("VARNISH: connected"); connected = true; - connecting = false; self.emit('connect'); + connecting = false; }); } else { client.connect(port, host); @@ -155,7 +155,7 @@ function VarnishQueue(host, port) { var ready = false; var reconnectTimer = null; var reconnectTries = 0; - var MAX_RECONNECT_TRIES = 50; + var MAX_RECONNECT_TRIES = 120; // 2 minutes var client = new VarnishClient(host, port); @@ -163,15 +163,24 @@ function VarnishQueue(host, port) { console.log.apply(console, arguments); } + // attach a dummy callback to error event to avoid nodejs throws an exception and closes the process + self.on('error', function(e) { + log("error", e); + }); + + client.on('connect', function() { + clearInterval(reconnectTimer); + reconnectTries = 0; + }); + client.on('ready', function() { ready = true; log('sending pending'); _send_pending(); - reconnectTries = 0; - clearInterval(reconnectTimer); }); function reconnect() { + ready = false; clearInterval(reconnectTimer); reconnectTimer = setInterval(function() { client.connect(); @@ -183,12 +192,13 @@ function VarnishQueue(host, port) { }); clearInterval(reconnectTimer); } - }, 500); + }, 1000); } client.on('close', reconnect); client.on('error', reconnect); function _send_pending(empty_callback) { + if(!ready) return; var c = queue.pop(); if(!c) return; client.run_cmd(c, function() { diff --git a/test/acceptance/varnish.js b/test/acceptance/varnish.js index 3a34534b..5a4dbd17 100644 --- a/test/acceptance/varnish.js +++ b/test/acceptance/varnish.js @@ -131,11 +131,12 @@ tests['should send command'] = function() { tests['should send commands on connect'] = function() { // first create queue var queue = new varnish.VarnishQueue('127.0.0.1', 1234) - for(var i = 0; i < 5; ++i) { + for(var i = 0; i < 10; ++i) { queue.run_cmd('purge simon_is == gay'); } // then server var server = VarnishEmu(null, 1234) - setTimeout(function() { assert.equal(5, server.commands); }, 1000); + //wait 2 seconds because the client tries every second the reconnection + setTimeout(function() { assert.equal(10, server.commands); }, 2000); }