From 35c366095829413a6cfcfbc2c0eb3712258e9e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 12 Jan 2021 16:33:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20pmgr:=20make=20upload=20install?= =?UTF-8?q?=20async=20&=20fix=20conflict=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out that a) we were not yet doing plugin installation after an archive/file upload asynchronously, so that any delays there could still cause timeouts, and that b) we were also not handling the conflict situation of trying to start another plugin install while one is already running correctly on the frontend. This fixes both issues. --- .../plugins/pluginmanager/__init__.py | 83 +++++++++++++------ .../pluginmanager/static/js/pluginmanager.js | 56 ++++++++----- 2 files changed, 93 insertions(+), 46 deletions(-) diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 7425d253d..15cb8e013 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -380,28 +380,43 @@ class PluginManagerPlugin( ) ext = exts[0] + archive = tempfile.NamedTemporaryFile(delete=False, suffix=ext) + archive.close() + shutil.copy(upload_path, archive.name) - archive = tempfile.NamedTemporaryFile( - delete=False, suffix="{ext}".format(**locals()) - ) - try: - archive.close() - shutil.copy(upload_path, archive.name) - return self.command_install( - path=archive.name, - name=upload_name, - force="force" in flask.request.values - and flask.request.values["force"] in valid_boolean_trues, - ) - finally: + def perform_install(source, name, force=False): try: - os.remove(archive.name) - except Exception as e: - self._logger.warning( - "Could not remove temporary file {path} again: {message}".format( - path=archive.name, message=str(e) - ) + self.command_install( + path=source, + name=name, + force=force, ) + finally: + try: + os.remove(archive.name) + except Exception as e: + self._logger.warning( + "Could not remove temporary file {path} again: {message}".format( + path=archive.name, message=str(e) + ) + ) + + with self._install_lock: + if self._install_task is not None: + return make_response("There's already a plugin being installed", 409) + + self._install_task = threading.Thread( + target=perform_install, + args=(archive.name, upload_name), + kwargs={ + "force": "force" in flask.request.values + and flask.request.values["force"] in valid_boolean_trues + }, + ) + self._install_task.daemon = True + self._install_task.start() + + return jsonify(in_progress=True) @octoprint.plugin.BlueprintPlugin.route("/export", methods=["GET"]) @no_firstrun_access @@ -538,7 +553,7 @@ class PluginManagerPlugin( with self._install_lock: if self._install_task is not None: - return make_response("There's always a plugin being installed", 409) + return make_response("There's already a plugin being installed", 409) self._install_task = threading.Thread( target=self.command_install, @@ -636,7 +651,7 @@ class PluginManagerPlugin( # determine type of path if self._is_archive(path): - return self._command_install_archive( + result = self._command_install_archive( path, source=source, source_type=source_type, @@ -646,12 +661,13 @@ class PluginManagerPlugin( ) elif self._is_pythonfile(path): - return self._command_install_pythonfile( + result = self._command_install_pythonfile( path, source=source, source_type=source_type, name=name ) else: raise exceptions.InvalidPackageFormat() + except requests.exceptions.HTTPError as e: self._logger.error("Could not fetch plugin from server, got {}".format(e)) result = { @@ -661,7 +677,7 @@ class PluginManagerPlugin( "reason": "Could not fetch plugin from server, got {}".format(e), } self._send_result_notification("install", result) - return result + except exceptions.InvalidPackageFormat: self._logger.error( "{} is neither an archive nor a python file, can't install that.".format( @@ -676,12 +692,29 @@ class PluginManagerPlugin( "a plugin archive nor a single file plugin".format(source), } self._send_result_notification("install", result) - return result + + except Exception: + error_msg = ( + "Unexpected error while trying to install plugin from {}".format( + source + ) + ) + self._logger.exception(error_msg) + result = { + "result": False, + "source": source, + "source_type": source_type, + "reason": error_msg, + } + self._send_result_notification("install", result) + finally: if folder is not None: folder.cleanup() self._install_task = None + return result + # noinspection DuplicatedCode def _command_install_archive( self, @@ -702,7 +735,7 @@ class PluginManagerPlugin( # currently throttled, we refuse to run error_msg = ( "System is currently throttled, refusing to install anything" - " due to possible stability isssues" + " due to possible stability issues" ) self._logger.error(error_msg) result = { diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 5b5bd9552..b0a72ccfe 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -435,23 +435,30 @@ $(function () { }, done: function (e, data) { var response = data.result; - if (response.result) { - self._markDone(); - } else { - self._markDone(response.reason); + if (!response.in_progress) { + if (response.result) { + self._markDone(); + } else { + self._markDone(response.reason); + } } self.uploadButton.unbind("click"); self.uploadFilename(undefined); }, fail: function (e, data) { - new PNotify({ - title: gettext("Something went wrong"), - text: gettext("Please consult octoprint.log for details"), - type: "error", - hide: false - }); - self._markDone("Could not install plugin, unknown error."); + if (data && data.errorThrown === "CONFLICT") { + // there's already a plugin being installed + self._markDone("There's already another plugin install in progress."); + } else { + new PNotify({ + title: gettext("Something went wrong"), + text: gettext("Please consult octoprint.log for details"), + type: "error", + hide: false + }); + self._markDone("Could not install plugin, unknown error."); + } self.uploadButton.unbind("click"); self.uploadFilename(undefined); } @@ -823,16 +830,23 @@ $(function () { var onSuccess = function (response) { self.installUrl(""); }, - onError = function () { - self._markDone( - "Could not install plugin, unknown error, please consult octoprint.log for details" - ); - new PNotify({ - title: gettext("Something went wrong"), - text: gettext("Please consult octoprint.log for details"), - type: "error", - hide: false - }); + onError = function (jqXHR) { + if (jqXHR.status === 409) { + // there's already a plugin being installed + self._markDone( + "There's already another plugin install in progress." + ); + } else { + self._markDone( + "Could not install plugin, unknown error, please consult octoprint.log for details" + ); + new PNotify({ + title: gettext("Something went wrong"), + text: gettext("Please consult octoprint.log for details"), + type: "error", + hide: false + }); + } }; if (reinstall) {