From c37eceeb9ef2514237468a7f5e3e6e07d293de16 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 10 Mar 2015 09:58:33 +0000 Subject: [PATCH] Split out the 'run' from 'setup' --- synapse/app/homeserver.py | 53 ++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index 26ef02e3de..e84b9ef5b3 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -296,7 +296,7 @@ def change_resource_limit(soft_file_no): logger.warn("Failed to set file limit: %s", e) -def setup(config_options, should_run=True): +def setup(config_options): """ Args: config_options_options: The options passed to Synapse. Usually @@ -380,25 +380,6 @@ def setup(config_options, should_run=True): hs.get_datastore().start_profiling() hs.get_replication_layer().start_get_pdu_cache() - if not should_run: - return hs - - if config.daemonize: - print config.pid_file - - daemon = Daemonize( - app="synapse-homeserver", - pid=config.pid_file, - action=lambda: run(config), - auto_close_fds=False, - verbose=True, - logger=logger, - ) - - daemon.start() - else: - run(config) - return hs @@ -410,24 +391,44 @@ class SynapseService(service.Service): self.config = config def startService(self): - hs = setup(self.config, should_run=False) + hs = setup(self.config) change_resource_limit(hs.config.soft_file_limit) def stopService(self): return self._port.stopListening() -def run(config): - with LoggingContext("run"): - change_resource_limit(config.soft_file_limit) +def run(hs): - reactor.run() + def in_thread(): + with LoggingContext("run"): + change_resource_limit(hs.config.soft_file_limit) + + reactor.run() + + if hs.config.daemonize: + + print hs.config.pid_file + + daemon = Daemonize( + app="synapse-homeserver", + pid=hs.config.pid_file, + action=lambda: in_thread(), + auto_close_fds=False, + verbose=True, + logger=logger, + ) + + daemon.start() + else: + in_thread(hs.config) def main(): with LoggingContext("main"): check_requirements() - setup(sys.argv[1:]) + hs = setup(sys.argv[1:]) + run(hs) if __name__ == '__main__':