mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 11:16:47 +00:00
The VM page polls the VM state every 5 s and, while its Stats tab is open, the statistics every 10 s; the compute overview polls the host graph every 5 s. All used setInterval: - A new request went out on schedule while the previous one was still open. When a host goes silent, a stats request waits up to 30-45 s, so each open page held several sync workers at once. - Each time the Stats tab was shown, the page made new charts and a new interval without stopping the old ones, and polling went on on the other tabs of the page. - Hidden pages kept polling at full rate. Measured in a browser against a test server (old code / new): Stats tab shown 4 times, 31 s on it: 12 / 2 requests; 31 s on another tab of the page: 12 / 0; page hidden for 31 s: 15 / 0, and the new code asks once right after the page is shown again; with 25 s answers, at most 15 / 1 requests open at once. static/js/poll.js repeats a request while the page is visible: the next one 5 or 10 s after the previous answer, none while hidden, one at once when shown again. The Stats tab makes its charts once and polls only while shown. The answer handler runs after the poller's own bookkeeping, so an error in it (a chart for a disk added meanwhile) does not stop the polling. A Node test of poll.js runs with the test suite when node is installed.
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
// Repeats request() while the page is visible and passes each answer to
|
|
// onData. request() returns a jQuery promise (e.g. $.getJSON(url)); onData is
|
|
// called after the poller's own bookkeeping, so an error in it does not stop
|
|
// the polling. One request at a time: the next one starts
|
|
// interval ms after the previous one has finished, so a slow host does not
|
|
// pile up requests, each holding a web worker. A hidden page asks nothing;
|
|
// when it is shown again it asks at once. stop() pauses, start() resumes
|
|
// at once (for example while a tab of the page is shown).
|
|
function pollWhileVisible(interval, request, onData) {
|
|
var timer = null;
|
|
var running = false;
|
|
var stopped = false;
|
|
|
|
function run() {
|
|
timer = null;
|
|
if (stopped || document.hidden) {
|
|
return;
|
|
}
|
|
running = true;
|
|
request()
|
|
.always(function () {
|
|
running = false;
|
|
if (!stopped && !document.hidden) {
|
|
timer = setTimeout(run, interval);
|
|
}
|
|
})
|
|
.done(onData);
|
|
}
|
|
|
|
function resume() {
|
|
if (!stopped && !document.hidden && !running && timer === null) {
|
|
run();
|
|
}
|
|
}
|
|
|
|
document.addEventListener("visibilitychange", resume);
|
|
timer = setTimeout(run, interval);
|
|
|
|
return {
|
|
stop: function () {
|
|
stopped = true;
|
|
clearTimeout(timer);
|
|
timer = null;
|
|
},
|
|
start: function () {
|
|
stopped = false;
|
|
resume();
|
|
},
|
|
};
|
|
}
|