1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/static/js/poll.js
catborise 35c794c50c feat(instances): start, shut down or force off the VMs chosen on the VM list
Powering many VMs took a click each. On the VM list each VM its user may
power (theirs, or every one for a superuser) has a box; "Choose the VMs
shown" chooses those on screen. Start, Shut down and Force off queue an
instance.power job for each chosen VM: each holds its VM and shares its host,
checks its user and the VM again when it runs, and has the checks of the
page's buttons (a template is not started, a host in maintenance starts none,
a VM an administrator paused is not an owner's to power), its own recovery
and its own log row. A VM the user may not power, or that another job holds,
is named and the others are queued; the Jobs page follows them. A VM a filter
or a collapsed host hides is neither counted nor sent, and the list does not
reload while VMs on screen are chosen.
2026-11-20 11:39:45 +03:00

61 lines
2.2 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). The first request
// goes out after firstDelay ms, the interval if it is not given.
function pollWhileVisible(interval, request, onData, firstDelay) {
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, firstDelay === undefined ? interval : firstDelay);
return {
stop: function () {
stopped = true;
clearTimeout(timer);
timer = null;
},
start: function () {
stopped = false;
resume();
},
};
}
// The user is in the page: a dialog open, a field in use, an XML edit not
// saved or VMs chosen on the VM list. A poll that would reload the page waits for its next answer.
function pageInUse() {
var active = document.activeElement;
return !!document.querySelector(".modal.show")
|| !!(active && active.matches && active.matches("input, select, textarea"))
|| !!(window.xmlEditUnsaved && window.xmlEditUnsaved())
|| (typeof bulkPowerChosen === "function" && bulkPowerChosen()); // VMs chosen on the VM list (bulk_power.js)
}