From daee320863c51d899d74f68dfbb80a9b0879d9a9 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 16:07:36 +0100 Subject: [PATCH 1/8] check_ftrack_url has moved from event_server_cli to lib to be importable --- pype/ftrack/__init__.py | 2 +- pype/ftrack/ftrack_server/__init__.py | 1 + pype/ftrack/ftrack_server/event_server_cli.py | 35 ++----------------- pype/ftrack/ftrack_server/lib.py | 31 ++++++++++++++++ 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/pype/ftrack/__init__.py b/pype/ftrack/__init__.py index 45ca8384b5..aa8f04bffb 100644 --- a/pype/ftrack/__init__.py +++ b/pype/ftrack/__init__.py @@ -1,2 +1,2 @@ from .lib import * -from .ftrack_server import FtrackServer +from .ftrack_server import FtrackServer, check_ftrack_url diff --git a/pype/ftrack/ftrack_server/__init__.py b/pype/ftrack/ftrack_server/__init__.py index 0861a1bc08..fcae4e0690 100644 --- a/pype/ftrack/ftrack_server/__init__.py +++ b/pype/ftrack/ftrack_server/__init__.py @@ -1 +1,2 @@ from .ftrack_server import FtrackServer +from .lib import check_ftrack_url diff --git a/pype/ftrack/ftrack_server/event_server_cli.py b/pype/ftrack/ftrack_server/event_server_cli.py index 8dd503f845..e14fd705f1 100644 --- a/pype/ftrack/ftrack_server/event_server_cli.py +++ b/pype/ftrack/ftrack_server/event_server_cli.py @@ -9,11 +9,12 @@ import atexit import time from urllib.parse import urlparse -import requests from pype.vendor import ftrack_api from pype.ftrack.lib import credentials from pype.ftrack.ftrack_server import FtrackServer -from pype.ftrack.ftrack_server.lib import ftrack_events_mongo_settings +from pype.ftrack.ftrack_server.lib import ( + ftrack_events_mongo_settings, check_ftrack_url +) import socket_thread @@ -25,36 +26,6 @@ class MongoPermissionsError(Exception): super().__init__(message) -def check_ftrack_url(url, log_errors=True): - """Checks if Ftrack server is responding""" - if not url: - print('ERROR: Ftrack URL is not set!') - return None - - url = url.strip('/ ') - - if 'http' not in url: - if url.endswith('ftrackapp.com'): - url = 'https://' + url - else: - url = 'https://{0}.ftrackapp.com'.format(url) - try: - result = requests.get(url, allow_redirects=False) - except requests.exceptions.RequestException: - if log_errors: - print('ERROR: Entered Ftrack URL is not accesible!') - return False - - if (result.status_code != 200 or 'FTRACK_VERSION' not in result.headers): - if log_errors: - print('ERROR: Entered Ftrack URL is not accesible!') - return False - - print('DEBUG: Ftrack server {} is accessible.'.format(url)) - - return url - - def check_mongo_url(host, port, log_error=False): """Checks if mongo server is responding""" sock = None diff --git a/pype/ftrack/ftrack_server/lib.py b/pype/ftrack/ftrack_server/lib.py index 12159693fe..748937c7bd 100644 --- a/pype/ftrack/ftrack_server/lib.py +++ b/pype/ftrack/ftrack_server/lib.py @@ -1,4 +1,5 @@ import os +import requests try: from urllib.parse import urlparse, parse_qs except ImportError: @@ -66,3 +67,33 @@ def get_ftrack_event_mongo_info(): url = "mongodb://{}{}{}{}".format(user_pass, socket_path, dab, auth) return url, database, collection + + +def check_ftrack_url(url, log_errors=True): + """Checks if Ftrack server is responding""" + if not url: + print('ERROR: Ftrack URL is not set!') + return None + + url = url.strip('/ ') + + if 'http' not in url: + if url.endswith('ftrackapp.com'): + url = 'https://' + url + else: + url = 'https://{0}.ftrackapp.com'.format(url) + try: + result = requests.get(url, allow_redirects=False) + except requests.exceptions.RequestException: + if log_errors: + print('ERROR: Entered Ftrack URL is not accesible!') + return False + + if (result.status_code != 200 or 'FTRACK_VERSION' not in result.headers): + if log_errors: + print('ERROR: Entered Ftrack URL is not accesible!') + return False + + print('DEBUG: Ftrack server {} is accessible.'.format(url)) + + return url From 2c3ebb0c1377d56616c9ebef59756a3d1cbcb2f0 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 16:08:06 +0100 Subject: [PATCH 2/8] added exception message when there are no events to register --- pype/ftrack/ftrack_server/ftrack_server.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pype/ftrack/ftrack_server/ftrack_server.py b/pype/ftrack/ftrack_server/ftrack_server.py index 12b046c510..dd4f9a9c69 100644 --- a/pype/ftrack/ftrack_server/ftrack_server.py +++ b/pype/ftrack/ftrack_server/ftrack_server.py @@ -100,7 +100,10 @@ class FtrackServer: log.warning(msg, exc_info=e) if len(register_functions_dict) < 1: - raise Exception + raise Exception(( + "There are no events with register function." + " Registered paths: \"{}\"" + ).format("| ".join(paths))) # Load presets for setting plugins key = "user" From 2f8f0c5bcab82d7f1e5e030c713051e695f9365a Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 16:11:11 +0100 Subject: [PATCH 3/8] changed logic of action server processing in ftrack module, now is checking if is possible to connect to ftrack in while loop --- pype/ftrack/tray/ftrack_module.py | 70 +++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index ce2754c25d..16b593eea2 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -6,7 +6,7 @@ from Qt import QtCore, QtGui, QtWidgets from pype.vendor import ftrack_api from pypeapp import style -from pype.ftrack import FtrackServer, credentials +from pype.ftrack import FtrackServer, check_ftrack_url, credentials from . import login_dialog from pype import api as pype @@ -24,7 +24,8 @@ class FtrackModule: self.thread_timer = None self.bool_logged = False - self.bool_action_server = False + self.bool_action_server_running = False + self.bool_action_thread_running = False self.bool_timer_event = False def show_login_widget(self): @@ -74,6 +75,8 @@ class FtrackModule: # Actions part def start_action_server(self): + self.bool_action_thread_running = True + self.set_menu_visibility() if self.thread_action_server is None: self.thread_action_server = threading.Thread( target=self.set_action_server @@ -81,21 +84,36 @@ class FtrackModule: self.thread_action_server.daemon = True self.thread_action_server.start() - log.info("Ftrack action server launched") - self.bool_action_server = True - self.set_menu_visibility() - def set_action_server(self): - try: - self.action_server.run_server() - except Exception as exc: - log.error( - "Ftrack Action server crashed! Please try to start again.", - exc_info=True + first_check = True + while self.bool_action_thread_running is True: + if not check_ftrack_url(os.environ['FTRACK_SERVER']): + if first_check: + log.warning( + "Could not connect to Ftrack server" + ) + first_check = False + time.sleep(1) + continue + log.info( + "Connected to Ftrack server. Running actions session" ) - # TODO show message to user - self.bool_action_server = False + try: + self.bool_action_server_running = True + self.set_menu_visibility() + self.action_server.run_server() + if self.bool_action_thread_running: + self.log.debug("Ftrack action server has stopped") + except Exception: + log.warning( + "Ftrack Action server crashed. Trying to connect again", + exc_info=True + ) + self.bool_action_server_running = False self.set_menu_visibility() + first_check = True + + self.bool_action_thread_running = False def reset_action_server(self): self.stop_action_server() @@ -103,16 +121,21 @@ class FtrackModule: def stop_action_server(self): try: + self.bool_action_thread_running = False self.action_server.stop_session() if self.thread_action_server is not None: self.thread_action_server.join() self.thread_action_server = None - log.info("Ftrack action server stopped") - self.bool_action_server = False + log.info("Ftrack action server was forced to stop") + + self.bool_action_server_running = False self.set_menu_visibility() - except Exception as e: - log.error("During Killing action server: {0}".format(e)) + except Exception: + log.warning( + "Error has happened during Killing action server", + exc_info=True + ) # Definition of Tray menu def tray_menu(self, parent_menu): @@ -158,6 +181,11 @@ class FtrackModule: def tray_start(self): self.validate() + def tray_exit(self): + self.bool_action_thread_running = False + self.bool_action_server_running = False + self.stop_action_server() + # Definition of visibility of each menu actions def set_menu_visibility(self): @@ -170,9 +198,9 @@ class FtrackModule: self.stop_timer_thread() return - self.aRunActionS.setVisible(not self.bool_action_server) - self.aResetActionS.setVisible(self.bool_action_server) - self.aStopActionS.setVisible(self.bool_action_server) + self.aRunActionS.setVisible(not self.bool_action_thread_running) + self.aResetActionS.setVisible(self.bool_action_server_running) + self.aStopActionS.setVisible(self.bool_action_thread_running) if self.bool_timer_event is False: self.start_timer_thread() From 0f7c320ad4ceaffac6cf760db9ef7549150ef32f Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 16:13:29 +0100 Subject: [PATCH 4/8] changed bool for visibility of reset action server to not confuse users that reset is not there but stop is --- pype/ftrack/tray/ftrack_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index 16b593eea2..d117d0c21c 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -199,7 +199,7 @@ class FtrackModule: return self.aRunActionS.setVisible(not self.bool_action_thread_running) - self.aResetActionS.setVisible(self.bool_action_server_running) + self.aResetActionS.setVisible(self.bool_action_thread_running) self.aStopActionS.setVisible(self.bool_action_thread_running) if self.bool_timer_event is False: From db77c0655511d41a3de94555f7c60f27fd60fbed Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 18:15:25 +0100 Subject: [PATCH 5/8] log fix --- pype/ftrack/tray/ftrack_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index d117d0c21c..1239e3c76c 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -103,7 +103,7 @@ class FtrackModule: self.set_menu_visibility() self.action_server.run_server() if self.bool_action_thread_running: - self.log.debug("Ftrack action server has stopped") + log.debug("Ftrack action server has stopped") except Exception: log.warning( "Ftrack Action server crashed. Trying to connect again", From 0e5cb8652c8cfce6f7fd3bad38e1faaad3b4e425 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 18:15:41 +0100 Subject: [PATCH 6/8] added one more check on start action server --- pype/ftrack/tray/ftrack_module.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index 1239e3c76c..563b11c388 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -77,6 +77,12 @@ class FtrackModule: def start_action_server(self): self.bool_action_thread_running = True self.set_menu_visibility() + if ( + self.thread_action_server is not None and + self.bool_action_thread_running is False + ): + self.stop_action_server() + if self.thread_action_server is None: self.thread_action_server = threading.Thread( target=self.set_action_server From edd3ad2e57577d717e9a6225bfe893937a295472 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 18:35:08 +0100 Subject: [PATCH 7/8] action server is not in daemon thread anymore --- pype/ftrack/tray/ftrack_module.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index 563b11c388..56324be39e 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -87,7 +87,6 @@ class FtrackModule: self.thread_action_server = threading.Thread( target=self.set_action_server ) - self.thread_action_server.daemon = True self.thread_action_server.start() def set_action_server(self): From f25aac63b24df95319585563022a3b8779cb47b6 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 15 Nov 2019 18:38:36 +0100 Subject: [PATCH 8/8] removed unnecessary bool changes --- pype/ftrack/tray/ftrack_module.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pype/ftrack/tray/ftrack_module.py b/pype/ftrack/tray/ftrack_module.py index 56324be39e..9a11a47a3a 100644 --- a/pype/ftrack/tray/ftrack_module.py +++ b/pype/ftrack/tray/ftrack_module.py @@ -187,8 +187,6 @@ class FtrackModule: self.validate() def tray_exit(self): - self.bool_action_thread_running = False - self.bool_action_server_running = False self.stop_action_server() # Definition of visibility of each menu actions