1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
function Poller(config)
{
// CONFIGURABLE
var endpoints = {
up: "/status/poll", // url to poll when the server is up
down: "/status" // url to poll at regular intervals when the server is down
};
var timeout = 60000 * 2; // how many ms between polling attempts
var intervalMs = 1000; // ms between polls when the server is down
// INTERNAL STATE
var up = true; // whether or not we can connect to the server
var req; // the pending ajax request
var downPoller; // the setInterval for polling when the server is down
var self = this;
if (typeof config === 'object')
{
if (typeof config.endpoints === 'object')
{
endpoints.up = config.endpoints.up;
endpoints.down = config.endpoints.down;
}
if (config.timeout)
timeout = config.timeout;
if (config.interval)
intervalMs = config.interval;
}
$(self).on('pollstart', function(event, data) {
log("Started poller");
}).on('pollstop', function(event, data) {
log("Stopped poller");
});
this.start = function()
{
if (req)
return false;
doPoll();
$(self).trigger('pollstart', {url: endpoints.up, timeout: timeout});
return true;
};
this.stop = function()
{
if (!req)
return false;
req.abort();
req = undefined;
stopped = true;
stopDownPoller();
$(self).trigger('pollstop', {});
return true;
};
this.setTimeout = function(tmout)
{
timeout = tmout; // takes effect at next poll
};
this.isUp = function()
{
return up;
};
function doPoll()
{
req = $.ajax({
url: endpoints.up + "?timeout=" + timeout,
timeout: timeout
}).done(pollSuccess).fail(pollFailed);
}
function pollSuccess(data, message, jqxhr)
{
stopDownPoller();
doPoll();
var wasUp = up;
up = true;
status = data;
var arg = {
status: status,
data: data,
jqxhr: jqxhr
};
if (!wasUp)
$(convey.poller).trigger('serverstarting', arg);
else
$(self).trigger('pollsuccess', arg);
}
function pollFailed(jqxhr, message, exception)
{
if (message === "timeout")
{
log("Poller timeout; re-polling...", req);
doPoll(); // in our case, timeout actually means no activity; poll again
return;
}
up = false;
downPoller = setInterval(function()
{
// If the server is still down, do a ping to see
// if it's up; pollSuccess() will do the rest.
if (!up)
$.get(endpoints.down).done(pollSuccess);
}, intervalMs);
$(self).trigger('pollfail', {
exception: exception,
message: message,
jqxhr: jqxhr
});
}
function stopDownPoller()
{
if (!downPoller)
return;
clearInterval(downPoller);
downPoller = undefined;
}
}
|