66import uuid
77from collections import defaultdict
88from functools import partial
9+ import atexit
910
1011from six import string_types
1112
2021
2122from ftrack_utils .framework .config .tool import get_tool_config_by_name
2223
23- from ftrack_framework_core .event import _EventHubThread
24+ from ftrack_framework_core .event import EventManager
2425
2526
2627class Client (object ):
@@ -185,21 +186,18 @@ def tool_config_options(self):
185186 return self ._tool_config_options
186187
187188 @property
188- def remote_session (self ):
189- # TODO: temporary hack for remote session
190- if self ._remote_session :
191- return self ._remote_session
189+ def remote_event_manager (self ):
190+ # TODO: this is a temporal solution, 1 session should be able to act as local and remote at the same time
191+ if self ._remote_event_manager :
192+ return self ._remote_event_manager
192193 else :
193- self ._remote_session = ftrack_api .Session (
194- auto_connect_event_hub = True
194+ _remote_session = ftrack_api .Session (auto_connect_event_hub = False )
195+ self ._remote_event_manager = EventManager (
196+ session = _remote_session , mode = constants .event .REMOTE_EVENT_MODE
195197 )
196- # TODO: temporary solution to start the wait thread
197- _event_hub_thread = _EventHubThread (self ._remote_session )
198-
199- if not _event_hub_thread .is_alive ():
200- # self.logger.debug('Starting new hub thread for {}'.format(self))
201- _event_hub_thread .start ()
202- return self ._remote_session
198+ # Make sure it is shutdown
199+ atexit .register (self .close )
200+ return self ._remote_event_manager
203201
204202 def __init__ (
205203 self , event_manager , registry , run_in_main_thread_wrapper = None
@@ -226,7 +224,7 @@ def __init__(
226224 self .__instanced_dialogs = {}
227225 self ._dialog = None
228226 self ._tool_config_options = defaultdict (defaultdict )
229- self ._remote_session = None
227+ self ._remote_event_manager = None
230228
231229 # Set up the run_in_main_thread decorator
232230 self .run_in_main_thread_wrapper = run_in_main_thread_wrapper
@@ -421,8 +419,9 @@ def subscribe_action_tool(
421419 '''
422420 if not options :
423421 options = dict ()
424- # TODO: we don't support dock_fn in here because is not serializable
425- self .remote_session .event_hub .subscribe (
422+ # TODO: The event should be added to the event manager to be accesible
423+ # through subscribe and publish classes
424+ self .remote_event_manager .session .event_hub .subscribe (
426425 u'topic=ftrack.action.discover and '
427426 u'source.user.username="{0}"' .format (self .session .api_user ),
428427 partial (
@@ -435,7 +434,7 @@ def subscribe_action_tool(
435434 ),
436435 )
437436
438- self .remote_session .event_hub .subscribe (
437+ self .remote_event_manager . session .event_hub .subscribe (
439438 u'topic=ftrack.action.launch and '
440439 u'data.name={0} and '
441440 u'source.user.username="{1}" and '
@@ -665,3 +664,10 @@ def verify_plugins(self, plugin_names):
665664 self .host_id , plugin_names
666665 )[0 ]
667666 return unregistered_plugins
667+
668+ def close (self ):
669+ self .logger .debug ('Shutting down client' )
670+ if self ._remote_event_manager :
671+ self .logger .debug ('Stopping remote_event_manager' )
672+ self .remote_event_manager .close ()
673+ self ._remote_event_manager = None
0 commit comments