Add-on: check connection with Flamenco on URL change
Whenever the URL to Flamenco Manager is updated, check it and report. This required moving the 'ping manager' code into a separate function, as calling an operator from an RNA update callback is not a good idea.
This commit is contained in:
parent
b1f13c47f7
commit
866513e06a
@ -5,13 +5,18 @@
|
|||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from urllib3.exceptions import HTTPError, MaxRetryError
|
||||||
|
import bpy
|
||||||
|
|
||||||
_flamenco_client = None
|
_flamenco_client = None
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from flamenco.manager import ApiClient as _ApiClient
|
from flamenco.manager import ApiClient as _ApiClient
|
||||||
|
from .preferences import FlamencoPreferences as _FlamencoPreferences
|
||||||
else:
|
else:
|
||||||
_ApiClient = object
|
_ApiClient = object
|
||||||
|
_FlamencoPreferences = object
|
||||||
|
|
||||||
|
|
||||||
def flamenco_api_client(manager_url: str) -> _ApiClient:
|
def flamenco_api_client(manager_url: str) -> _ApiClient:
|
||||||
@ -43,3 +48,47 @@ def discard_flamenco_data():
|
|||||||
_log.info("closing Flamenco client")
|
_log.info("closing Flamenco client")
|
||||||
_flamenco_client.close()
|
_flamenco_client.close()
|
||||||
_flamenco_client = None
|
_flamenco_client = None
|
||||||
|
|
||||||
|
|
||||||
|
def ping_manager(context: bpy.types.Context, api_client: _ApiClient, prefs: _FlamencoPreferences) -> tuple[str, str]:
|
||||||
|
"""Ping the Manager, update preferences, and return a report as string.
|
||||||
|
|
||||||
|
:returns: tuple (report, level). The report will be something like "<name>
|
||||||
|
version <version> found", or an error message. The level will be
|
||||||
|
'ERROR', 'WARNING', or 'INFO', suitable for reporting via
|
||||||
|
`Operator.report()`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Do a late import, so that the API is only imported when actually used.
|
||||||
|
from flamenco.manager import ApiException
|
||||||
|
from flamenco.manager.apis import MetaApi
|
||||||
|
from flamenco.manager.models import FlamencoVersion, ManagerConfiguration
|
||||||
|
|
||||||
|
context.window_manager.flamenco_status_ping = "..."
|
||||||
|
|
||||||
|
meta_api = MetaApi(api_client)
|
||||||
|
try:
|
||||||
|
version: FlamencoVersion = meta_api.get_version()
|
||||||
|
config: ManagerConfiguration = meta_api.get_configuration()
|
||||||
|
except ApiException as ex:
|
||||||
|
report = "Manager cannot be reached: %s" % ex
|
||||||
|
level = "ERROR"
|
||||||
|
except MaxRetryError as ex:
|
||||||
|
# This is the common error, when for example the port number is
|
||||||
|
# incorrect and nothing is listening.
|
||||||
|
report = "Manager cannot be reached"
|
||||||
|
level = "WARNING"
|
||||||
|
except HTTPError as ex:
|
||||||
|
report = "Manager cannot be reached: %s" % ex
|
||||||
|
level = "ERROR"
|
||||||
|
else:
|
||||||
|
report = "%s version %s found" % (version.name, version.version)
|
||||||
|
level = "INFO"
|
||||||
|
|
||||||
|
# Store whether this Manager supports the Shaman API.
|
||||||
|
prefs.is_shaman_enabled = config.shaman_enabled
|
||||||
|
prefs.job_storage = config.storage_location
|
||||||
|
|
||||||
|
context.window_manager.flamenco_status_ping = report
|
||||||
|
|
||||||
|
return report, level
|
||||||
|
@ -5,7 +5,7 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
from pathlib import Path, PurePosixPath
|
from pathlib import Path, PurePosixPath
|
||||||
from typing import Optional, TYPE_CHECKING
|
from typing import Optional, TYPE_CHECKING
|
||||||
from urllib3.exceptions import HTTPError, MaxRetryError
|
from urllib3.exceptions import MaxRetryError
|
||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
|
|
||||||
@ -81,40 +81,14 @@ class FLAMENCO_OT_ping_manager(FlamencoOpMixin, bpy.types.Operator):
|
|||||||
bl_options = {"REGISTER"} # No UNDO.
|
bl_options = {"REGISTER"} # No UNDO.
|
||||||
|
|
||||||
def execute(self, context: bpy.types.Context) -> set[str]:
|
def execute(self, context: bpy.types.Context) -> set[str]:
|
||||||
|
from . import comms, preferences
|
||||||
|
|
||||||
api_client = self.get_api_client(context)
|
api_client = self.get_api_client(context)
|
||||||
|
prefs = preferences.get(context)
|
||||||
|
|
||||||
from flamenco.manager import ApiException
|
report, level = comms.ping_manager(context, api_client, prefs)
|
||||||
from flamenco.manager.apis import MetaApi
|
|
||||||
from flamenco.manager.models import FlamencoVersion, ManagerConfiguration
|
|
||||||
|
|
||||||
context.window_manager.flamenco_status_ping = "..."
|
|
||||||
|
|
||||||
meta_api = MetaApi(api_client)
|
|
||||||
try:
|
|
||||||
version: FlamencoVersion = meta_api.get_version()
|
|
||||||
config: ManagerConfiguration = meta_api.get_configuration()
|
|
||||||
except ApiException as ex:
|
|
||||||
report = "Manager cannot be reached: %s" % ex
|
|
||||||
level = "ERROR"
|
|
||||||
except MaxRetryError as ex:
|
|
||||||
# This is the common error, when for example the port number is
|
|
||||||
# incorrect and nothing is listening.
|
|
||||||
report = "Manager cannot be reached"
|
|
||||||
level = "WARNING"
|
|
||||||
except HTTPError as ex:
|
|
||||||
report = "Manager cannot be reached: %s" % ex
|
|
||||||
level = "ERROR"
|
|
||||||
else:
|
|
||||||
report = "%s version %s found" % (version.name, version.version)
|
|
||||||
level = "INFO"
|
|
||||||
|
|
||||||
# Store whether this Manager supports the Shaman API.
|
|
||||||
prefs = preferences.get(context)
|
|
||||||
prefs.is_shaman_enabled = config.shaman_enabled
|
|
||||||
prefs.job_storage = config.storage_location
|
|
||||||
|
|
||||||
self.report({level}, report)
|
self.report({level}, report)
|
||||||
context.window_manager.flamenco_status_ping = report
|
|
||||||
return {"FINISHED"}
|
return {"FINISHED"}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
import bpy
|
import bpy
|
||||||
|
|
||||||
|
|
||||||
def discard_flamenco_client(prefs, context):
|
def discard_flamenco_client(context):
|
||||||
"""Discard any cached Flamenco client after the Manager URL changes."""
|
"""Discard any cached Flamenco client after the Manager URL changes."""
|
||||||
from . import comms
|
from . import comms
|
||||||
|
|
||||||
@ -22,6 +22,18 @@ def _refresh_the_planet(
|
|||||||
region.tag_redraw()
|
region.tag_redraw()
|
||||||
|
|
||||||
|
|
||||||
|
def _manager_url_updated(prefs, context):
|
||||||
|
discard_flamenco_client(context)
|
||||||
|
|
||||||
|
from . import comms
|
||||||
|
|
||||||
|
api_client = comms.flamenco_api_client(prefs.manager_url)
|
||||||
|
|
||||||
|
# Warning, be careful what of the context to access here. Accessing /
|
||||||
|
# changing too much can cause crashes, infinite loops, etc.
|
||||||
|
comms.ping_manager(context, api_client, prefs)
|
||||||
|
|
||||||
|
|
||||||
class FlamencoPreferences(bpy.types.AddonPreferences):
|
class FlamencoPreferences(bpy.types.AddonPreferences):
|
||||||
bl_idname = "flamenco"
|
bl_idname = "flamenco"
|
||||||
|
|
||||||
@ -29,7 +41,7 @@ class FlamencoPreferences(bpy.types.AddonPreferences):
|
|||||||
name="Manager URL",
|
name="Manager URL",
|
||||||
description="Location of the Manager",
|
description="Location of the Manager",
|
||||||
default="http://localhost:8080/",
|
default="http://localhost:8080/",
|
||||||
update=discard_flamenco_client,
|
update=_manager_url_updated,
|
||||||
)
|
)
|
||||||
|
|
||||||
is_shaman_enabled: bpy.props.BoolProperty( # type: ignore
|
is_shaman_enabled: bpy.props.BoolProperty( # type: ignore
|
||||||
@ -68,16 +80,25 @@ class FlamencoPreferences(bpy.types.AddonPreferences):
|
|||||||
|
|
||||||
row = col.row(align=True)
|
row = col.row(align=True)
|
||||||
row.prop(self, "manager_url")
|
row.prop(self, "manager_url")
|
||||||
row.operator("flamenco.ping_manager", text="", icon="CHECKMARK")
|
row.operator("flamenco.ping_manager", text="", icon="FILE_REFRESH")
|
||||||
if context.window_manager.flamenco_status_ping:
|
|
||||||
split = col.split(factor=0.4)
|
def text_row(parent, label):
|
||||||
|
split = parent.split(factor=0.4)
|
||||||
split.label(text="")
|
split.label(text="")
|
||||||
split.label(text=context.window_manager.flamenco_status_ping)
|
split.label(text=label)
|
||||||
|
|
||||||
|
if not self.job_storage:
|
||||||
|
text_row(col, "Press the refresh button before using Flamenco")
|
||||||
|
|
||||||
|
if context.window_manager.flamenco_status_ping:
|
||||||
|
text_row(col, context.window_manager.flamenco_status_ping)
|
||||||
|
else:
|
||||||
|
aligned = col.column(align=True)
|
||||||
|
text_row(aligned, "Press the refresh button to check the connection")
|
||||||
|
text_row(aligned, "and update the job storage location")
|
||||||
|
|
||||||
if self.is_shaman_enabled:
|
if self.is_shaman_enabled:
|
||||||
split = col.split(factor=0.4)
|
text_row(col, "Shaman enabled")
|
||||||
split.label(text="")
|
|
||||||
split.label(text="Shaman enabled")
|
|
||||||
col.prop(self, "job_storage_for_gui", text="Job Storage")
|
col.prop(self, "job_storage_for_gui", text="Job Storage")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user