@@ -83,6 +83,10 @@ def gz_wrap(r: Resource) -> Resource:
8383
8484
8585class SynapseHomeServer (HomeServer ):
86+ """
87+ Homeserver class for the main Synapse process.
88+ """
89+
8690 DATASTORE_CLASS = DataStore
8791
8892 def _listener_http (
@@ -345,23 +349,17 @@ def load_or_generate_config(argv_options: List[str]) -> HomeServerConfig:
345349 return config
346350
347351
348- def setup (
352+ def create_homeserver (
349353 config : HomeServerConfig ,
350354 reactor : Optional [ISynapseReactor ] = None ,
351- freeze : bool = True ,
352355) -> SynapseHomeServer :
353356 """
354- Create and setup the main Synapse homeserver instance given a configuration .
357+ Create a homeserver instance for the Synapse main process .
355358
356359 Args:
357360 config: The configuration for the homeserver.
358361 reactor: Optionally provide a reactor to use. Can be useful in different
359362 scenarios that you want control over the reactor, such as tests.
360- freeze: whether to freeze the homeserver base objects in the garbage collector.
361- May improve garbage collection performance by marking objects with an effectively
362- static lifetime as frozen so they don't need to be considered for cleanup.
363- If you ever want to `shutdown` the homeserver, this needs to be
364- False otherwise the homeserver cannot be garbage collected after `shutdown`.
365363
366364 Returns:
367365 A homeserver instance.
@@ -380,24 +378,50 @@ def setup(
380378 synapse .metrics .MIN_TIME_BETWEEN_GCS = config .server .gc_seconds
381379
382380 hs = SynapseHomeServer (
383- config .server .server_name ,
381+ hostname = config .server .server_name ,
384382 config = config ,
385383 reactor = reactor ,
386384 )
387385
388- setup_logging ( hs , config , use_worker_options = False )
386+ return hs
389387
390- # Start the tracer
391- init_tracer (hs ) # noqa
392388
389+ def setup (
390+ hs : SynapseHomeServer ,
391+ * ,
392+ freeze : bool = True ,
393+ ) -> None :
394+ """
395+ Setup a Synapse homeserver instance given a configuration.
396+
397+ Args:
398+ hs: The homeserver to setup.
399+ freeze: whether to freeze the homeserver base objects in the garbage collector.
400+ May improve garbage collection performance by marking objects with an effectively
401+ static lifetime as frozen so they don't need to be considered for cleanup.
402+ If you ever want to `shutdown` the homeserver, this needs to be
403+ False otherwise the homeserver cannot be garbage collected after `shutdown`.
404+
405+ Returns:
406+ A homeserver instance.
407+ """
408+
409+ setup_logging (hs , hs .config , use_worker_options = False )
410+
411+ # Log after we've configured logging.
393412 logger .info ("Setting up server" )
394413
414+ # Start the tracer
415+ init_tracer (hs ) # noqa
416+
395417 try :
396418 hs .setup ()
397419 except Exception as e :
398420 handle_startup_exception (e )
399421
400- async def start () -> None :
422+ async def _start_when_reactor_running () -> None :
423+ # TODO: Feels like this should be moved somewhere else.
424+ #
401425 # Load the OIDC provider metadatas, if OIDC is enabled.
402426 if hs .config .oidc .oidc_enabled :
403427 oidc = hs .get_oidc_handler ()
@@ -406,21 +430,31 @@ async def start() -> None:
406430
407431 await _base .start (hs , freeze )
408432
433+ # TODO: This should be moved to `SynapseHomeServer.start_background_tasks` (not
434+ # `HomeServer.start_background_tasks`) (this way it matches the behavior of only
435+ # running on `main`)
409436 hs .get_datastores ().main .db_pool .updates .start_doing_background_updates ()
410437
411- register_start (hs , start )
438+ # Register a callback to be invoked once the reactor is running
439+ register_start (hs , _start_when_reactor_running )
412440
413- return hs
414441
442+ def start_reactor (
443+ config : HomeServerConfig ,
444+ ) -> None :
445+ """
446+ Start the reactor (Twisted event-loop).
415447
416- def run (hs : HomeServer ) -> None :
448+ Args:
449+ config: The configuration for the homeserver.
450+ """
417451 _base .start_reactor (
418452 "synapse-homeserver" ,
419- soft_file_limit = hs . config .server .soft_file_limit ,
420- gc_thresholds = hs . config .server .gc_thresholds ,
421- pid_file = hs . config .server .pid_file ,
422- daemonize = hs . config .server .daemonize ,
423- print_pidfile = hs . config .server .print_pidfile ,
453+ soft_file_limit = config .server .soft_file_limit ,
454+ gc_thresholds = config .server .gc_thresholds ,
455+ pid_file = config .server .pid_file ,
456+ daemonize = config .server .daemonize ,
457+ print_pidfile = config .server .print_pidfile ,
424458 logger = logger ,
425459 )
426460
@@ -431,13 +465,14 @@ def main() -> None:
431465 with LoggingContext (name = "main" , server_name = homeserver_config .server .server_name ):
432466 # check base requirements
433467 check_requirements ()
434- hs = setup (homeserver_config )
468+ hs = create_homeserver (homeserver_config )
469+ setup (hs )
435470
436471 # redirect stdio to the logs, if configured.
437472 if not hs .config .logging .no_redirect_stdio :
438473 redirect_stdio_to_logs ()
439474
440- run ( hs )
475+ start_reactor ( homeserver_config )
441476
442477
443478if __name__ == "__main__" :
0 commit comments