summary refs log tree commit diff
path: root/synapse/app/homeserver.py
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/app/homeserver.py')
-rwxr-xr-xsynapse/app/homeserver.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index f96535a978..dff08c8bc5 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -26,6 +26,7 @@ from synapse.server import HomeServer
 from synapse.python_dependencies import check_requirements
 
 from twisted.internet import reactor
+from twisted.application import service
 from twisted.enterprise import adbapi
 from twisted.web.resource import Resource
 from twisted.web.static import File
@@ -295,10 +296,10 @@ def change_resource_limit(soft_file_no):
         logger.warn("Failed to set file limit: %s", e)
 
 
-def setup():
+def setup(config_options, should_run=True):
     config = HomeServerConfig.load_config(
         "Synapse Homeserver",
-        sys.argv[1:],
+        config_options,
         generate_section="Homeserver"
     )
 
@@ -370,6 +371,9 @@ def setup():
     hs.get_datastore().start_profiling()
     hs.get_replication_layer().start_get_pdu_cache()
 
+    if not should_run:
+        return
+
     if config.daemonize:
         print config.pid_file
 
@@ -387,6 +391,17 @@ def setup():
         run(config)
 
 
+class SynapseService(service.Service):
+    def __init__(self, config):
+        self.config = config
+
+    def startService(self):
+        setup(self.config, should_run=False)
+
+    def stopService(self):
+        return self._port.stopListening()
+
+
 def run(config):
     with LoggingContext("run"):
         change_resource_limit(config.soft_file_limit)
@@ -397,7 +412,7 @@ def run(config):
 def main():
     with LoggingContext("main"):
         check_requirements()
-        setup()
+        setup(sys.argv[1:])
 
 
 if __name__ == '__main__':