From 72bfaf746d17505df01dfa68b23ee43eb9f54144 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 13 Mar 2019 17:33:54 +0000 Subject: [PATCH 1/3] Allow passing --daemonize to workers --- synapse/config/_base.py | 8 +++++++- synapse/config/workers.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/synapse/config/_base.py b/synapse/config/_base.py index c4d3087fa..5613f38e4 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -214,14 +214,20 @@ class Config(object): " Defaults to the directory containing the last config file", ) + obj = cls() + + obj.invoke_all("add_arguments", config_parser) + config_args = config_parser.parse_args(argv) config_files = find_config_files(search_paths=config_args.config_path) - obj = cls() obj.read_config_files( config_files, keys_directory=config_args.keys_directory, generate_keys=False ) + + obj.invoke_all("read_arguments", config_args) + return obj @classmethod diff --git a/synapse/config/workers.py b/synapse/config/workers.py index 80baf0ce0..8dc013d0e 100644 --- a/synapse/config/workers.py +++ b/synapse/config/workers.py @@ -57,3 +57,7 @@ class WorkerConfig(Config): bind_addresses.append(bind_address) elif not bind_addresses: bind_addresses.append('') + + def read_arguments(self, args): + if args.daemonize is not None: + self.worker_daemonize = args.daemonize From f5d57d48488721368168bb1cfa2aec2bb4420c27 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 13 Mar 2019 17:38:58 +0000 Subject: [PATCH 2/3] Newsfile --- changelog.d/4853.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4853.feature diff --git a/changelog.d/4853.feature b/changelog.d/4853.feature new file mode 100644 index 000000000..360f92e1d --- /dev/null +++ b/changelog.d/4853.feature @@ -0,0 +1 @@ +Allow passing --daemonize flags to workers in the same way as with master. From 9ad448c1e56dc548d8ee22494deabe802637b58a Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 14 Mar 2019 13:32:14 +0000 Subject: [PATCH 3/3] Correctly handle all command line options --- synapse/app/_base.py | 18 ++++++++++++------ synapse/app/homeserver.py | 16 +++++++--------- synapse/config/workers.py | 24 +++++++++++++++++++++++- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/synapse/app/_base.py b/synapse/app/_base.py index 32e8b8a3f..d4c6c4c8e 100644 --- a/synapse/app/_base.py +++ b/synapse/app/_base.py @@ -63,12 +63,13 @@ def start_worker_reactor(appname, config): start_reactor( appname, - config.soft_file_limit, - config.gc_thresholds, - config.worker_pid_file, - config.worker_daemonize, - config.worker_cpu_affinity, - logger, + soft_file_limit=config.soft_file_limit, + gc_thresholds=config.gc_thresholds, + pid_file=config.worker_pid_file, + daemonize=config.worker_daemonize, + cpu_affinity=config.worker_cpu_affinity, + print_pidfile=config.print_pidfile, + logger=logger, ) @@ -79,6 +80,7 @@ def start_reactor( pid_file, daemonize, cpu_affinity, + print_pidfile, logger, ): """ Run the reactor in the main process @@ -93,6 +95,7 @@ def start_reactor( pid_file (str): name of pid file to write to if daemonize is True daemonize (bool): true to run the reactor in a background process cpu_affinity (int|None): cpu affinity mask + print_pidfile (bool): whether to print the pid file, if daemonize is True logger (logging.Logger): logger instance to pass to Daemonize """ @@ -124,6 +127,9 @@ def start_reactor( reactor.run() if daemonize: + if print_pidfile: + print(pid_file) + daemon = Daemonize( app=appname, pid=pid_file, diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index e8b6cc311..ce89d48bf 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -636,17 +636,15 @@ def run(hs): # be quite busy the first few minutes clock.call_later(5 * 60, start_phone_stats_home) - if hs.config.daemonize and hs.config.print_pidfile: - print(hs.config.pid_file) - _base.start_reactor( "synapse-homeserver", - hs.config.soft_file_limit, - hs.config.gc_thresholds, - hs.config.pid_file, - hs.config.daemonize, - hs.config.cpu_affinity, - logger, + soft_file_limit=hs.config.soft_file_limit, + gc_thresholds=hs.config.gc_thresholds, + pid_file=hs.config.pid_file, + daemonize=hs.config.daemonize, + cpu_affinity=hs.config.cpu_affinity, + print_pidfile=hs.config.print_pidfile, + logger=logger, ) diff --git a/synapse/config/workers.py b/synapse/config/workers.py index 8dc013d0e..bfbd8b6c9 100644 --- a/synapse/config/workers.py +++ b/synapse/config/workers.py @@ -28,7 +28,7 @@ class WorkerConfig(Config): if self.worker_app == "synapse.app.homeserver": self.worker_app = None - self.worker_listeners = config.get("worker_listeners") + self.worker_listeners = config.get("worker_listeners", []) self.worker_daemonize = config.get("worker_daemonize") self.worker_pid_file = config.get("worker_pid_file") self.worker_log_file = config.get("worker_log_file") @@ -48,6 +48,17 @@ class WorkerConfig(Config): self.worker_main_http_uri = config.get("worker_main_http_uri", None) self.worker_cpu_affinity = config.get("worker_cpu_affinity") + # This option is really only here to support `--manhole` command line + # argument. + manhole = config.get("worker_manhole") + if manhole: + self.worker_listeners.append({ + "port": manhole, + "bind_addresses": ["127.0.0.1"], + "type": "manhole", + "tls": False, + }) + if self.worker_listeners: for listener in self.worker_listeners: bind_address = listener.pop("bind_address", None) @@ -59,5 +70,16 @@ class WorkerConfig(Config): bind_addresses.append('') def read_arguments(self, args): + # We support a bunch of command line arguments that override options in + # the config. A lot of these options have a worker_* prefix when running + # on workers so we also have to override them when command line options + # are specified. + if args.daemonize is not None: self.worker_daemonize = args.daemonize + if args.log_config is not None: + self.worker_log_config = args.log_config + if args.log_file is not None: + self.worker_log_file = args.log_file + if args.manhole is not None: + self.worker_manhole = args.worker_manhole