1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/operations/api/serializers.py
catborise f2138e54d8 feat(operations): jobs a request runs
Request jobs (an upload's chunks) run in gunicorn: their kind is admitted
running with the request as its worker, never claimed or recovered by the
worker, cancelled in the request, and asked by every housekeeping pass to
end its jobs that went idle. The panel can follow a job the page just
started. A job a request runs does not wait for the worker, so it does not
raise the worker-unavailable warning on the Jobs page or in the panel.
2026-10-16 11:53:59 +03:00

48 lines
1.8 KiB
Python

from django.utils.translation import gettext as _
from rest_framework import serializers
from operations.jobs import KINDS, label_of
from operations.models import Operation
from operations.utils import shown_label
class OperationSerializer(serializers.ModelSerializer):
label = serializers.SerializerMethodField()
resource_label = serializers.SerializerMethodField()
cancellable = serializers.SerializerMethodField()
in_request = serializers.SerializerMethodField()
class Meta:
model = Operation
fields = ["id", "kind", "label", "username", "resource", "resource_label", "state", "phase",
"progress_done", "progress_total", "result", "error", "created_at", "started_at",
"finished_at", "cancel_requested", "cancellable", "in_request", "resolution", "resolution_note"]
read_only_fields = fields
def get_label(self, op) -> str:
return str(_(label_of(op.kind)))
def get_resource_label(self, op) -> str:
return shown_label(op, self.context["request"].user)
def get_cancellable(self, op) -> bool:
kind = KINDS.get(op.kind)
return op.state == Operation.QUEUED or bool(kind and kind.cancellable and op.state == Operation.RUNNING)
def get_in_request(self, op) -> bool:
"""A request runs it: it does not wait for the worker."""
return op.in_request
class OperationSummarySerializer(serializers.Serializer):
kind = serializers.CharField()
label = serializers.CharField()
state = serializers.CharField()
since = serializers.DateTimeField()
class ResourceJobsSerializer(serializers.Serializer):
jobs = OperationSerializer(many=True)
others = OperationSummarySerializer(many=True)
short_operation = serializers.BooleanField()
worker_last_seen = serializers.DateTimeField(allow_null=True)