mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
A failed backup, a scheduled backup not taken, backups the daily check finds missing and a job that ends uncertain could only be seen on the pages. They are now queued as alerts in the transaction of the change they tell of (never stopping it) and sent by the worker's periodic pass to the Settings page's alert e-mail addresses (the server's SMTP settings) and alert webhook (a JSON POST), each tried again for ten passes on its own. A skip for a host in maintenance is no alert. Setting values take 255 characters. /healthz answers 200 while the database answers and the job worker runs, 503 otherwise, without a login.
102 lines
4.2 KiB
Python
102 lines
4.2 KiB
Python
from urllib.parse import urlencode
|
|
|
|
from django.contrib import messages
|
|
from django.contrib.auth.decorators import login_not_required
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.core.paginator import Paginator
|
|
from django.db import Error as DatabaseError
|
|
from django.http import Http404, JsonResponse
|
|
from django.shortcuts import redirect, render
|
|
from django.utils.translation import gettext as _
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import require_POST
|
|
from vrtManager.util import OperationError
|
|
|
|
from . import jobs as jobs_module
|
|
from .api.serializers import OperationSerializer
|
|
from .models import Operation, last_seen, worker_gone
|
|
from .utils import said, words
|
|
|
|
|
|
def _visible(user):
|
|
return Operation.objects.all() if user.is_superuser else Operation.objects.filter(user=user)
|
|
|
|
|
|
def _get(user, pk):
|
|
try:
|
|
return _visible(user).get(pk=pk)
|
|
except Operation.DoesNotExist:
|
|
raise Http404 from None
|
|
|
|
|
|
def jobs(request):
|
|
qs = _visible(request.user)
|
|
if request.user.is_superuser:
|
|
# the host and the user as typed: the start of the host, any part of the name
|
|
if compute := request.GET.get("compute"):
|
|
qs = qs.filter(resource_label__istartswith=compute)
|
|
if username := request.GET.get("username"):
|
|
qs = qs.filter(username__icontains=username)
|
|
for field in ("state", "kind"):
|
|
if value := request.GET.get(field):
|
|
qs = qs.filter(**{field: value})
|
|
page = Paginator(qs, 50).get_page(request.GET.get("page"))
|
|
# the model for its datetimes, the serialized job for its labels
|
|
rows = [(op, OperationSerializer(op, context={"request": request}).data) for op in page.object_list]
|
|
seen = last_seen()
|
|
# a job the worker has to start, run or stop, not one a request runs (job_panel.js uses the same rule)
|
|
unavailable = (worker_gone(seen)
|
|
and _visible(request.user).filter(state__in=Operation.ACTIVE)
|
|
.exclude(worker=Operation.REQUEST).exists())
|
|
return render(request, "operations/jobs.html",
|
|
{"page": page, "rows": rows, "worker_last_seen": seen, "worker_unavailable": unavailable,
|
|
"filters": request.GET, "active_states": Operation.ACTIVE,
|
|
"follow": any(op.state in Operation.ACTIVE for op in page.object_list),
|
|
"states": Operation.ACTIVE + Operation.TERMINAL,
|
|
"kinds": sorted((name, _(kind.label)) for name, kind in jobs_module.KINDS.items()),
|
|
"bytes_kinds": {name for name, kind in jobs_module.KINDS.items() if kind.progress_bytes},
|
|
"page_query": urlencode({k: v for k, v in request.GET.items() if k != "page"})})
|
|
|
|
|
|
def job(request, pk):
|
|
op = _get(request.user, pk)
|
|
kind = jobs_module.KINDS.get(op.kind)
|
|
return render(request, "operations/job.html",
|
|
{"job": OperationSerializer(op, context={"request": request}).data, "op": op,
|
|
"progress_bytes": kind is not None and kind.progress_bytes,
|
|
"follow": op.state in Operation.ACTIVE})
|
|
|
|
|
|
@require_POST
|
|
def cancel(request, pk):
|
|
op = _get(request.user, pk)
|
|
try:
|
|
jobs_module.cancel(op.pk, request.user)
|
|
except OperationError as err:
|
|
messages.error(request, words(said(err)))
|
|
return redirect("operations:job", pk=op.pk)
|
|
|
|
|
|
@require_POST
|
|
def resolve(request, pk):
|
|
if not request.user.is_superuser:
|
|
raise PermissionDenied
|
|
op = _get(request.user, pk)
|
|
try:
|
|
jobs_module.resolve(op.pk, request.user, request.POST.get("outcome", ""), request.POST.get("note", ""))
|
|
except OperationError as err:
|
|
messages.error(request, words(said(err)))
|
|
return redirect("operations:job", pk=op.pk)
|
|
|
|
|
|
@login_not_required
|
|
@never_cache
|
|
def healthz(request):
|
|
"""For a monitor, without a login: 200 while the database answers and the job worker runs, 503 otherwise."""
|
|
try:
|
|
seen = last_seen()
|
|
except DatabaseError:
|
|
return JsonResponse({"database": False, "worker": False}, status=503)
|
|
running = not worker_gone(seen)
|
|
return JsonResponse({"database": True, "worker": running, "worker_seen": seen and seen.isoformat()},
|
|
status=200 if running else 503)
|