Add-on: use job check endpoint before sending files to the farm

This commit is contained in:
Sybren A. Stüvel 2022-10-20 13:14:01 +02:00
parent 7a60bb70e0
commit 32a3e48e24
2 changed files with 64 additions and 11 deletions

View File

@ -95,6 +95,19 @@ def submit_job(job: _SubmittedJob, api_client: _ApiClient) -> _Job:
return response
def submit_job_check(job: _SubmittedJob, api_client: _ApiClient) -> None:
"""Check the given job at Flamenco Manager to see if it is acceptable."""
from flamenco.manager import ApiClient
from flamenco.manager.api import jobs_api
from flamenco.manager.models import SubmittedJob, Job
assert isinstance(job, SubmittedJob), "got %s" % type(job)
assert isinstance(api_client, ApiClient), "got %s" % type(api_client)
job_api_instance = jobs_api.JobsApi(api_client)
job_api_instance.submit_job_check(job)
def is_file_inside_job_storage(context: bpy.types.Context, blendfile: Path) -> bool:
"""Check whether current blend file is inside the storage path.

View File

@ -163,10 +163,8 @@ class FLAMENCO_OT_submit_job(FlamencoOpMixin, bpy.types.Operator):
filepath = self._save_blendfile(context)
# Construct the Job locally before trying to pack. If any validations fail, better fail early.
self.job = job_submission.job_for_scene(context.scene)
if self.job is None:
self.report({"ERROR"}, "Unable to create job")
# Check the job with the Manager, to see if it would be accepted.
if not self._check_job(context):
return {"CANCELLED"}
return self._submit_files(context, filepath)
@ -427,14 +425,13 @@ class FLAMENCO_OT_submit_job(FlamencoOpMixin, bpy.types.Operator):
self.blendfile_on_farm = bpathlib.make_absolute(blendfile)
self._submit_job(context)
def _submit_job(self, context: bpy.types.Context) -> None:
"""Use the Flamenco API to submit the new Job."""
assert self.job is not None
assert self.blendfile_on_farm is not None
def _prepare_job_for_submission(self, context: bpy.types.Context) -> None:
"""Prepare self.job for sending to Flamenco."""
from flamenco.manager import ApiException
api_client = self.get_api_client(context)
self.job = job_submission.job_for_scene(context.scene)
if self.job is None:
self.report({"ERROR"}, "Unable to create job")
return {"CANCELLED"}
propgroup = getattr(context.scene, "flamenco_job_settings", None)
assert isinstance(propgroup, JobTypePropertyGroup), "did not expect %s" % (
@ -446,6 +443,16 @@ class FLAMENCO_OT_submit_job(FlamencoOpMixin, bpy.types.Operator):
propgroup.job_type, self.job, self.blendfile_on_farm
)
def _submit_job(self, context: bpy.types.Context) -> None:
"""Use the Flamenco API to submit the new Job."""
assert self.job is not None
assert self.blendfile_on_farm is not None
from flamenco.manager import ApiException
self._prepare_job_for_submission(context)
api_client = self.get_api_client(context)
try:
submitted_job = job_submission.submit_job(self.job, api_client)
except MaxRetryError as ex:
@ -470,6 +477,39 @@ class FLAMENCO_OT_submit_job(FlamencoOpMixin, bpy.types.Operator):
self.report({"INFO"}, "Job %s submitted" % submitted_job.name)
def _check_job(self, context: bpy.types.Context) -> bool:
"""Use the Flamenco API to check the Job before submitting files.
:return: "OK" flag, so True = ok, False = not ok.
"""
from flamenco.manager import ApiException
self._prepare_job_for_submission(context)
api_client = self.get_api_client(context)
try:
job_submission.submit_job_check(self.job, api_client)
except MaxRetryError as ex:
self.report({"ERROR"}, "Unable to reach Flamenco Manager")
return False
except HTTPError as ex:
self.report({"ERROR"}, "Error communicating with Flamenco Manager: %s" % ex)
return False
except ApiException as ex:
if ex.status == 412:
self.report(
{"ERROR"},
"Cached job type is old. Refresh the job types and submit again, please",
)
return False
if ex.status == 400:
error = parse_api_error(api_client, ex)
self.report({"ERROR"}, error.message)
return False
self.report({"ERROR"}, f"Could not check job: {ex.reason}")
return False
return True
def _quit(self, context: bpy.types.Context) -> set[str]:
"""Stop any timer and return a 'FINISHED' status.