Skip to content

Commit 5be7679

Browse files
Split loading config vs homeserver setup (#18933)
This allows us to get access to `server_name` so we can use it when creating the `LoggingContext("main")` in the future (pre-requisite for #18868). This also allows us more flexibility to parse config however we want and setup a Synapse homeserver. Like what we do in [Synapse Pro for Small Hosts](https://github.com/element-hq/synapse-small-hosts). Split out from #18868
1 parent e7d98d3 commit 5be7679

17 files changed

+108
-48
lines changed

changelog.d/18933.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Split loading config from homeserver `setup`.

synapse/app/admin_cmd.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import os
2525
import sys
2626
import tempfile
27-
from typing import List, Mapping, Optional, Sequence
27+
from typing import List, Mapping, Optional, Sequence, Tuple
2828

2929
from twisted.internet import defer, task
3030

@@ -256,7 +256,7 @@ def finished(self) -> str:
256256
return self.base_directory
257257

258258

259-
def start(config_options: List[str]) -> None:
259+
def load_config(argv_options: List[str]) -> Tuple[HomeServerConfig, argparse.Namespace]:
260260
parser = argparse.ArgumentParser(description="Synapse Admin Command")
261261
HomeServerConfig.add_arguments_to_parser(parser)
262262

@@ -282,11 +282,15 @@ def start(config_options: List[str]) -> None:
282282
export_data_parser.set_defaults(func=export_data_command)
283283

284284
try:
285-
config, args = HomeServerConfig.load_config_with_parser(parser, config_options)
285+
config, args = HomeServerConfig.load_config_with_parser(parser, argv_options)
286286
except ConfigError as e:
287287
sys.stderr.write("\n" + str(e) + "\n")
288288
sys.exit(1)
289289

290+
return config, args
291+
292+
293+
def start(config: HomeServerConfig, args: argparse.Namespace) -> None:
290294
if config.worker.worker_app is not None:
291295
assert config.worker.worker_app == "synapse.app.admin_cmd"
292296

@@ -325,7 +329,7 @@ def start(config_options: List[str]) -> None:
325329
# command.
326330

327331
async def run() -> None:
328-
with LoggingContext("command"):
332+
with LoggingContext(name="command"):
329333
await _base.start(ss)
330334
await args.func(ss, args)
331335

@@ -337,5 +341,6 @@ async def run() -> None:
337341

338342

339343
if __name__ == "__main__":
340-
with LoggingContext("main"):
341-
start(sys.argv[1:])
344+
homeserver_config, args = load_config(sys.argv[1:])
345+
with LoggingContext(name="main"):
346+
start(homeserver_config, args)

synapse/app/appservice.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import sys
2323

24-
from synapse.app.generic_worker import start
24+
from synapse.app.generic_worker import load_config, start
2525
from synapse.util.logcontext import LoggingContext
2626

2727

2828
def main() -> None:
29-
with LoggingContext("main"):
30-
start(sys.argv[1:])
29+
homeserver_config = load_config(sys.argv[1:])
30+
with LoggingContext(name="main"):
31+
start(homeserver_config)
3132

3233

3334
if __name__ == "__main__":

synapse/app/client_reader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import sys
2323

24-
from synapse.app.generic_worker import start
24+
from synapse.app.generic_worker import load_config, start
2525
from synapse.util.logcontext import LoggingContext
2626

2727

2828
def main() -> None:
29-
with LoggingContext("main"):
30-
start(sys.argv[1:])
29+
homeserver_config = load_config(sys.argv[1:])
30+
with LoggingContext(name="main"):
31+
start(homeserver_config)
3132

3233

3334
if __name__ == "__main__":

synapse/app/event_creator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020

2121
import sys
2222

23-
from synapse.app.generic_worker import start
23+
from synapse.app.generic_worker import load_config, start
2424
from synapse.util.logcontext import LoggingContext
2525

2626

2727
def main() -> None:
28-
with LoggingContext("main"):
29-
start(sys.argv[1:])
28+
homeserver_config = load_config(sys.argv[1:])
29+
with LoggingContext(name="main"):
30+
start(homeserver_config)
3031

3132

3233
if __name__ == "__main__":

synapse/app/federation_reader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import sys
2323

24-
from synapse.app.generic_worker import start
24+
from synapse.app.generic_worker import load_config, start
2525
from synapse.util.logcontext import LoggingContext
2626

2727

2828
def main() -> None:
29-
with LoggingContext("main"):
30-
start(sys.argv[1:])
29+
homeserver_config = load_config(sys.argv[1:])
30+
with LoggingContext(name="main"):
31+
start(homeserver_config)
3132

3233

3334
if __name__ == "__main__":

synapse/app/federation_sender.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import sys
2323

24-
from synapse.app.generic_worker import start
24+
from synapse.app.generic_worker import load_config, start
2525
from synapse.util.logcontext import LoggingContext
2626

2727

2828
def main() -> None:
29-
with LoggingContext("main"):
30-
start(sys.argv[1:])
29+
homeserver_config = load_config(sys.argv[1:])
30+
with LoggingContext(name="main"):
31+
start(homeserver_config)
3132

3233

3334
if __name__ == "__main__":

synapse/app/frontend_proxy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import sys
2323

24-
from synapse.app.generic_worker import start
24+
from synapse.app.generic_worker import load_config, start
2525
from synapse.util.logcontext import LoggingContext
2626

2727

2828
def main() -> None:
29-
with LoggingContext("main"):
30-
start(sys.argv[1:])
29+
homeserver_config = load_config(sys.argv[1:])
30+
with LoggingContext(name="main"):
31+
start(homeserver_config)
3132

3233

3334
if __name__ == "__main__":

synapse/app/generic_worker.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,26 @@ def start_listening(self) -> None:
310310
self.get_replication_command_handler().start_replication(self)
311311

312312

313-
def start(config_options: List[str]) -> None:
313+
def load_config(argv_options: List[str]) -> HomeServerConfig:
314+
"""
315+
Parse the commandline and config files (does not generate config)
316+
317+
Args:
318+
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
319+
320+
Returns:
321+
Config object.
322+
"""
314323
try:
315-
config = HomeServerConfig.load_config("Synapse worker", config_options)
324+
config = HomeServerConfig.load_config("Synapse worker", argv_options)
316325
except ConfigError as e:
317326
sys.stderr.write("\n" + str(e) + "\n")
318327
sys.exit(1)
319328

329+
return config
330+
331+
332+
def start(config: HomeServerConfig) -> None:
320333
# For backwards compatibility let any of the old app names.
321334
assert config.worker.worker_app in (
322335
"synapse.app.appservice",
@@ -368,8 +381,9 @@ async def start() -> None:
368381

369382

370383
def main() -> None:
371-
with LoggingContext("main"):
372-
start(sys.argv[1:])
384+
homeserver_config = load_config(sys.argv[1:])
385+
with LoggingContext(name="main"):
386+
start(homeserver_config)
373387

374388

375389
if __name__ == "__main__":

synapse/app/homeserver.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,21 @@ def start_listening(self) -> None:
308308
logger.warning("Unrecognized listener type: %s", listener.type)
309309

310310

311-
def setup(config_options: List[str]) -> SynapseHomeServer:
311+
def load_or_generate_config(argv_options: List[str]) -> HomeServerConfig:
312312
"""
313+
Parse the commandline and config files
314+
315+
Supports generation of config files, so is used for the main homeserver app.
316+
313317
Args:
314-
config_options_options: The options passed to Synapse. Usually `sys.argv[1:]`.
318+
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
315319
316320
Returns:
317321
A homeserver instance.
318322
"""
319323
try:
320324
config = HomeServerConfig.load_or_generate_config(
321-
"Synapse Homeserver", config_options
325+
"Synapse Homeserver", argv_options
322326
)
323327
except ConfigError as e:
324328
sys.stderr.write("\n")
@@ -332,6 +336,20 @@ def setup(config_options: List[str]) -> SynapseHomeServer:
332336
# generating config files and shouldn't try to continue.
333337
sys.exit(0)
334338

339+
return config
340+
341+
342+
def setup(config: HomeServerConfig) -> SynapseHomeServer:
343+
"""
344+
Create and setup a Synapse homeserver instance given a configuration.
345+
346+
Args:
347+
config: The configuration for the homeserver.
348+
349+
Returns:
350+
A homeserver instance.
351+
"""
352+
335353
if config.worker.worker_app:
336354
raise ConfigError(
337355
"You have specified `worker_app` in the config but are attempting to start a non-worker "
@@ -405,10 +423,12 @@ def run(hs: HomeServer) -> None:
405423

406424

407425
def main() -> None:
426+
homeserver_config = load_or_generate_config(sys.argv[1:])
427+
408428
with LoggingContext("main"):
409429
# check base requirements
410430
check_requirements()
411-
hs = setup(sys.argv[1:])
431+
hs = setup(homeserver_config)
412432

413433
# redirect stdio to the logs, if configured.
414434
if not hs.config.logging.no_redirect_stdio:

0 commit comments

Comments
 (0)