|
26 | 26 | from synapse.python_dependencies import check_requirements |
27 | 27 |
|
28 | 28 | from twisted.internet import reactor |
| 29 | +from twisted.application import service |
29 | 30 | from twisted.enterprise import adbapi |
30 | 31 | from twisted.web.resource import Resource |
31 | 32 | from twisted.web.static import File |
@@ -295,10 +296,19 @@ def change_resource_limit(soft_file_no): |
295 | 296 | logger.warn("Failed to set file limit: %s", e) |
296 | 297 |
|
297 | 298 |
|
298 | | -def setup(): |
| 299 | +def setup(config_options): |
| 300 | + """ |
| 301 | + Args: |
| 302 | + config_options_options: The options passed to Synapse. Usually |
| 303 | + `sys.argv[1:]`. |
| 304 | + should_run (bool): Whether to start the reactor. |
| 305 | +
|
| 306 | + Returns: |
| 307 | + HomeServer |
| 308 | + """ |
299 | 309 | config = HomeServerConfig.load_config( |
300 | 310 | "Synapse Homeserver", |
301 | | - sys.argv[1:], |
| 311 | + config_options, |
302 | 312 | generate_section="Homeserver" |
303 | 313 | ) |
304 | 314 |
|
@@ -370,34 +380,55 @@ def setup(): |
370 | 380 | hs.get_datastore().start_profiling() |
371 | 381 | hs.get_replication_layer().start_get_pdu_cache() |
372 | 382 |
|
373 | | - if config.daemonize: |
374 | | - print config.pid_file |
| 383 | + return hs |
| 384 | + |
| 385 | + |
| 386 | +class SynapseService(service.Service): |
| 387 | + """A twisted Service class that will start synapse. Used to run synapse |
| 388 | + via twistd and a .tac. |
| 389 | + """ |
| 390 | + def __init__(self, config): |
| 391 | + self.config = config |
| 392 | + |
| 393 | + def startService(self): |
| 394 | + hs = setup(self.config) |
| 395 | + change_resource_limit(hs.config.soft_file_limit) |
| 396 | + |
| 397 | + def stopService(self): |
| 398 | + return self._port.stopListening() |
| 399 | + |
| 400 | + |
| 401 | +def run(hs): |
| 402 | + |
| 403 | + def in_thread(): |
| 404 | + with LoggingContext("run"): |
| 405 | + change_resource_limit(hs.config.soft_file_limit) |
| 406 | + |
| 407 | + reactor.run() |
| 408 | + |
| 409 | + if hs.config.daemonize: |
| 410 | + |
| 411 | + print hs.config.pid_file |
375 | 412 |
|
376 | 413 | daemon = Daemonize( |
377 | 414 | app="synapse-homeserver", |
378 | | - pid=config.pid_file, |
379 | | - action=lambda: run(config), |
| 415 | + pid=hs.config.pid_file, |
| 416 | + action=lambda: in_thread(), |
380 | 417 | auto_close_fds=False, |
381 | 418 | verbose=True, |
382 | 419 | logger=logger, |
383 | 420 | ) |
384 | 421 |
|
385 | 422 | daemon.start() |
386 | 423 | else: |
387 | | - run(config) |
388 | | - |
389 | | - |
390 | | -def run(config): |
391 | | - with LoggingContext("run"): |
392 | | - change_resource_limit(config.soft_file_limit) |
393 | | - |
394 | | - reactor.run() |
| 424 | + in_thread(hs.config) |
395 | 425 |
|
396 | 426 |
|
397 | 427 | def main(): |
398 | 428 | with LoggingContext("main"): |
399 | 429 | check_requirements() |
400 | | - setup() |
| 430 | + hs = setup(sys.argv[1:]) |
| 431 | + run(hs) |
401 | 432 |
|
402 | 433 |
|
403 | 434 | if __name__ == '__main__': |
|
0 commit comments