mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-01 16:36:06 +00:00
fix(instances): a VM list notices a change made while the page was read
The VM lists took the poll's first answer as what the page showed: a VM started or stopped between the page's read and that answer kept its old state and buttons until the next change. The page now carries the states read before its rows, and the poll compares its first answer with them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
7dd68aaca4
commit
effe2a33f0
8 changed files with 29 additions and 18 deletions
|
|
@ -11,7 +11,7 @@ from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.decorators.debug import sensitive_post_parameters
|
from django.views.decorators.debug import sensitive_post_parameters
|
||||||
from instances.models import Instance
|
from instances.models import Instance
|
||||||
from instances.utils import read_list_rows
|
from instances.utils import read_list_rows, vm_states
|
||||||
from libvirt import libvirtError
|
from libvirt import libvirtError
|
||||||
from logs.audit import audit, changed_fields, snapshot
|
from logs.audit import audit, changed_fields, snapshot
|
||||||
from operations import claims
|
from operations import claims
|
||||||
|
|
@ -136,7 +136,8 @@ def overview(request, compute_id):
|
||||||
@superuser_only
|
@superuser_only
|
||||||
def instances(request, compute_id):
|
def instances(request, compute_id):
|
||||||
compute = get_object_or_404(Compute, pk=compute_id)
|
compute = get_object_or_404(Compute, pk=compute_id)
|
||||||
|
# read first: the poll's first answer then tells a change made while the page was read
|
||||||
|
states = vm_states(request.user, [compute])
|
||||||
utils.refresh_instance_database(compute)
|
utils.refresh_instance_database(compute)
|
||||||
instances = list(Instance.objects.filter(compute=compute).order_by("name").prefetch_related("userinstance_set"))
|
instances = list(Instance.objects.filter(compute=compute).order_by("name").prefetch_related("userinstance_set"))
|
||||||
read_list_rows(instances)
|
read_list_rows(instances)
|
||||||
|
|
@ -144,7 +145,7 @@ def instances(request, compute_id):
|
||||||
return render(
|
return render(
|
||||||
request, "computes/instances.html",
|
request, "computes/instances.html",
|
||||||
{"compute": compute, "instances": instances, "job_resource": claims.compute_key(compute.id),
|
{"compute": compute, "instances": instances, "job_resource": claims.compute_key(compute.id),
|
||||||
"states_url": reverse("instances:states") + f"?compute={compute.id}"},
|
"states_url": reverse("instances:states") + f"?compute={compute.id}", "vm_states": states},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
{% load static %}
|
{% load static %}
|
||||||
{# the VM list reloads when its VMs change: vm_states.js #}
|
{# the VM list reloads when its VMs change: vm_states.js #}
|
||||||
{{ states_url|json_script:"vm-states-url" }}
|
{{ states_url|json_script:"vm-states-url" }}
|
||||||
|
{{ vm_states|json_script:"vm-states" }}
|
||||||
<script src="{% static 'js/vm_states.js' %}"></script>
|
<script src="{% static 'js/vm_states.js' %}"></script>
|
||||||
<script src="{% static 'js/vm_states_start.js' %}"></script>
|
<script src="{% static 'js/vm_states_start.js' %}"></script>
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ class UnreachableListTests(TestCase):
|
||||||
patch("instances.views.refresh_instance_database"),
|
patch("instances.views.refresh_instance_database"),
|
||||||
patch("computes.views.utils.refresh_instance_database"),
|
patch("computes.views.utils.refresh_instance_database"),
|
||||||
patch.object(Compute, "proxy", new_callable=PropertyMock,
|
patch.object(Compute, "proxy", new_callable=PropertyMock,
|
||||||
return_value=SimpleNamespace(get_domain_rows=domain_rows)),
|
return_value=SimpleNamespace(get_domain_rows=domain_rows, get_domain_states=dict)),
|
||||||
]
|
]
|
||||||
for p in patches:
|
for p in patches:
|
||||||
p.start()
|
p.start()
|
||||||
|
|
@ -85,7 +85,7 @@ class UnreachableListTests(TestCase):
|
||||||
patch.object(Compute, "connection_error", new_callable=PropertyMock, return_value=None), \
|
patch.object(Compute, "connection_error", new_callable=PropertyMock, return_value=None), \
|
||||||
patch("instances.views.refresh_instance_database"), \
|
patch("instances.views.refresh_instance_database"), \
|
||||||
patch.object(Compute, "proxy", new_callable=PropertyMock, return_value=type("P", (), {
|
patch.object(Compute, "proxy", new_callable=PropertyMock, return_value=type("P", (), {
|
||||||
"get_domain_rows": lambda self, uuids=None: {}, "get_memory_usage": lambda self: gone(),
|
"get_domain_rows": lambda self, uuids=None: {}, "get_domain_states": lambda self: {}, "get_memory_usage": lambda self: gone(),
|
||||||
"host_info": property(lambda self: gone())})()):
|
"host_info": property(lambda self: gone())})()):
|
||||||
response = self.client.get(reverse("instances:index"))
|
response = self.client.get(reverse("instances:index"))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ class StateTests(TestCase):
|
||||||
|
|
||||||
self.client.force_login(user)
|
self.client.force_login(user)
|
||||||
props = {"status": True, "connection_error": None, "cpu_count": 1, "ram_size": 1, "ram_usage": 0,
|
props = {"status": True, "connection_error": None, "cpu_count": 1, "ram_size": 1, "ram_usage": 0,
|
||||||
"proxy": SimpleNamespace(get_domain_rows=lambda uuids=None: rows)}
|
"proxy": SimpleNamespace(get_domain_rows=lambda uuids=None: rows, get_domain_states=dict)}
|
||||||
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in props.items()]
|
patches = [patch.object(Compute, k, new_callable=PropertyMock, return_value=v) for k, v in props.items()]
|
||||||
patches += [patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database")]
|
patches += [patch("instances.views.refresh_instance_database"), patch("computes.views.utils.refresh_instance_database")]
|
||||||
for p in patches:
|
for p in patches:
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,8 @@ def index(request):
|
||||||
|
|
||||||
# a user who sees no hosts makes the panel read only those of their own VMs
|
# a user who sees no hosts makes the panel read only those of their own VMs
|
||||||
computes = _listed_computes(request.user, list(Compute.objects.all().order_by("name")))
|
computes = _listed_computes(request.user, list(Compute.objects.all().order_by("name")))
|
||||||
|
# read first: the poll's first answer then tells a change made while the page was read
|
||||||
|
vm_states = utils.vm_states(request.user, computes)
|
||||||
for compute in computes:
|
for compute in computes:
|
||||||
refresh_instance_database(compute)
|
refresh_instance_database(compute)
|
||||||
|
|
||||||
|
|
@ -109,7 +111,7 @@ def index(request):
|
||||||
"reachable_computes": [c for c in computes if c.status is True and c.ram_usage is not None],
|
"reachable_computes": [c for c in computes if c.status is True and c.ram_usage is not None],
|
||||||
"unreachable_computes": [c for c in computes if c.status is not True],
|
"unreachable_computes": [c for c in computes if c.status is not True],
|
||||||
"states": [label for label, _color in STATES.values()] + [UNKNOWN[0]],
|
"states": [label for label, _color in STATES.values()] + [UNKNOWN[0]],
|
||||||
"states_url": reverse("instances:states")},
|
"states_url": reverse("instances:states"), "vm_states": vm_states},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
// A VM list follows libvirt: it polls the states of its VMs (instances:states)
|
// A VM list follows libvirt: it polls the states of its VMs (instances:states)
|
||||||
// at once, then every 5 s, as the VM page, and reloads when an answer differs
|
// at once, then every 5 s, as the VM page, and reloads when an answer differs
|
||||||
// from the one before: a VM started or stopped, made, removed or renamed
|
// from the states the page was rendered with, then from the answer before: a
|
||||||
// outside the page included. The page in use or the job panel showing
|
// VM started or stopped, made, removed or renamed outside the page included.
|
||||||
// something: it waits for the next answer. poll.js gives the polling.
|
// The page in use or the job panel showing something: it waits for the next
|
||||||
|
// answer. poll.js gives the polling.
|
||||||
function vmStatesKey(states) {
|
function vmStatesKey(states) {
|
||||||
return Object.keys(states).sort().map(function (id) {
|
return Object.keys(states).sort().map(function (id) {
|
||||||
var host = states[id];
|
var host = states[id];
|
||||||
|
|
@ -12,12 +13,11 @@ function vmStatesKey(states) {
|
||||||
}).join(";");
|
}).join(";");
|
||||||
}
|
}
|
||||||
|
|
||||||
function followVmStates(url) {
|
function followVmStates(url, rendered) {
|
||||||
var last = null;
|
var last = vmStatesKey(rendered);
|
||||||
// the first answer, at once, is what the page was rendered with
|
|
||||||
return pollWhileVisible(5000, function () { return $.getJSON(url); }, function (data) {
|
return pollWhileVisible(5000, function () { return $.getJSON(url); }, function (data) {
|
||||||
var key = vmStatesKey(data.computes);
|
var key = vmStatesKey(data.computes);
|
||||||
if (last !== null && key !== last) {
|
if (key !== last) {
|
||||||
if (!pageInUse() && !window.backgroundJobRunning) {
|
if (!pageInUse() && !window.backgroundJobRunning) {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
// Starts following the VM list's states (vm_states_poll.html).
|
// Starts following the VM list's states (vm_states_poll.html).
|
||||||
followVmStates(JSON.parse(document.getElementById("vm-states-url").textContent));
|
followVmStates(JSON.parse(document.getElementById("vm-states-url").textContent),
|
||||||
|
JSON.parse(document.getElementById("vm-states").textContent));
|
||||||
|
|
|
||||||
|
|
@ -32,12 +32,18 @@ eval(fs.readFileSync(process.argv[2], "utf8"));
|
||||||
eval(fs.readFileSync(process.argv[3], "utf8"));
|
eval(fs.readFileSync(process.argv[3], "utf8"));
|
||||||
|
|
||||||
const base = { "1": { ok: true, vms: { "u-a": ["web", 1], "u-b": ["db", 5] } }, "2": { ok: false } };
|
const base = { "1": { ok: true, vms: { "u-a": ["web", 1], "u-b": ["db", 5] } }, "2": { ok: false } };
|
||||||
followVmStates("/instances/states/?compute=1");
|
// a change made while the page was read: the first answer, asked at once, reloads
|
||||||
|
followVmStates("/instances/states/?compute=1", { "1": { ok: true, vms: { "u-a": ["web", 1] } }, "2": { ok: false } });
|
||||||
// the first answer, asked at once, is where it starts; the same VMs in another order change nothing
|
|
||||||
answer = { computes: base };
|
answer = { computes: base };
|
||||||
advance(0);
|
advance(0);
|
||||||
|
assert.strictEqual(window.reloads, 1, "the first answer differs from the page");
|
||||||
|
timers = []; window.reloads = 0; asked = [];
|
||||||
|
|
||||||
|
// the page's states are where it starts; the same VMs in another order change nothing
|
||||||
|
followVmStates("/instances/states/?compute=1", base);
|
||||||
|
advance(0);
|
||||||
assert.strictEqual(asked.length, 1, "asks at once");
|
assert.strictEqual(asked.length, 1, "asks at once");
|
||||||
|
assert.strictEqual(window.reloads, 0, "the first answer is the page");
|
||||||
answer = { computes: { "2": { ok: false }, "1": { ok: true, vms: { "u-b": ["db", 5], "u-a": ["web", 1] } } } };
|
answer = { computes: { "2": { ok: false }, "1": { ok: true, vms: { "u-b": ["db", 5], "u-a": ["web", 1] } } } };
|
||||||
advance(5000);
|
advance(5000);
|
||||||
assert.deepStrictEqual(asked, ["/instances/states/?compute=1", "/instances/states/?compute=1"], "asks every 5 s");
|
assert.deepStrictEqual(asked, ["/instances/states/?compute=1", "/instances/states/?compute=1"], "asks every 5 s");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue