def Run(params): """ Note: this action always runs in parallel. """ git = params.config.git repos = params.config.repos # Note: to time, just pass --timeit in the command line! import sys if sys.platform == "win32": from mu_repo.system_mutex import create_system_mutex_for_current_dir # On mu st, we first check if we have a running server system_mutex = create_system_mutex_for_current_dir() if not system_mutex.get_mutex_aquired(): with open(system_mutex.filename, "r") as stream: port = int(stream.read().strip()) # Ok, we have a running server, go on and connect to it! from mu_repo.umsgpack_s_conn import ConnectionHandler, UMsgPacker, Client import threading event = threading.Event() class ClientHandler(ConnectionHandler, UMsgPacker): def _handle_decoded(self, msgs): if not isinstance(msgs, (tuple, list)): msgs = [msgs] from mu_repo.print_ import Print for msg in msgs: Print(msg) event.set() client = Client("127.0.0.1", port, ClientHandler) client.send(("stat", git, repos)) event.wait(5) return ExecuteStCommand(params, repos, git)
def stop_server(): from mu_repo.system_mutex import create_system_mutex_for_current_dir system_mutex = create_system_mutex_for_current_dir() if not system_mutex.get_mutex_aquired(): with open(system_mutex.filename, 'r') as stream: port = int(stream.read().strip()) from mu_repo.umsgpack_s_conn import ConnectionHandler, UMsgPacker, Client class ClientHandler(ConnectionHandler, UMsgPacker): def _handle_decoded(self, decoded): print('Warning: not expecting message. Received: %s' % (decoded,)) client = Client('127.0.0.1', port, ClientHandler) if _DEBUG: print('debug: client: Killing server') client.send('shutdown')
def Run(params): ''' Note: this action always runs in parallel. ''' git = params.config.git repos = params.config.repos # Note: to time, just pass --timeit in the command line! import sys if sys.platform == 'win32': from mu_repo.system_mutex import create_system_mutex_for_current_dir # On mu st, we first check if we have a running server system_mutex = create_system_mutex_for_current_dir() if not system_mutex.get_mutex_aquired(): with open(system_mutex.filename, 'r') as stream: port = int(stream.read().strip()) # Ok, we have a running server, go on and connect to it! from mu_repo.umsgpack_s_conn import ConnectionHandler, UMsgPacker, Client import threading event = threading.Event() class ClientHandler(ConnectionHandler, UMsgPacker): def _handle_decoded(self, msgs): if not isinstance(msgs, (tuple, list)): msgs = [msgs] from mu_repo.print_ import Print for msg in msgs: Print(msg) event.set() client = Client('127.0.0.1', port, ClientHandler) client.send(('stat', git, repos)) event.wait(5) return ExecuteStCommand(params, repos, git)
def start_server(): from mu_repo.system_mutex import create_system_mutex_for_current_dir system_mutex = create_system_mutex_for_current_dir() if system_mutex.get_mutex_aquired(): # Make sure it's not garbage collected so that we hold the mutex _MutexHolder.system_mutex = system_mutex #=========================================================================================== # Start the server now that we have the mutex #=========================================================================================== from mu_repo.umsgpack_s_conn import ConnectionHandler, UMsgPacker, Server class ServerHandler(ConnectionHandler, UMsgPacker): def _handle_decoded(self, decoded): try: # Some message was received from the client in the server. if decoded == 'echo': # Actual implementations may want to put that in a queue and have an additional # thread to check the queue and handle what was received and send the results back. self.send('echo back') return elif decoded == 'shutdown': # Actual implementations may want to put that in a queue and have an additional # thread to check the queue and handle what was received and send the results back. server_api.shutdown() return elif isinstance(decoded, (tuple, list)): if len(decoded) == 3 and decoded[0] == 'stat': self.send(server_api.stat(decoded[1], decoded[2])) return self.send('Error: %s did not match anything expected.' % (decoded,)) except: import traceback try: from StringIO import StringIO except ImportError: from io import StringIO stream = StringIO() traceback.print_exc(file=stream) self.send(stream.getvalue()) def send(self, obj): # Send a message to the client self.connection.sendall(self.pack_obj(obj)) # Start the server server = Server(ServerHandler) server_api = ServerAPI(server) assert server.after_bind_socket is not None def after_bind_socket(host, port): system_mutex.write(str(port)) if _DEBUG: print('debug: Started server at port: %s' % (port,)) server.after_bind_socket = after_bind_socket # Note: not blocking means it'll start in another thread server.serve_forever('127.0.0.1', 0, block=True) else: if _DEBUG: with open(system_mutex.filename, 'r') as stream: port = stream.read().strip() print('debug: Server previously started at port: %s' % (port,))