2018-09-25 06:19:27 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2018-10-18 16:04:34 -04:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2018-09-25 06:19:27 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import collections
|
|
|
|
import errno
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import signal
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
from typing import Iterable, NoReturn, Optional, TextIO
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2019-08-28 10:55:58 -04:00
|
|
|
from synapse.config import find_config_files
|
|
|
|
|
2021-05-24 09:03:30 -04:00
|
|
|
MAIN_PROCESS = "synapse.app.homeserver"
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
GREEN = "\x1b[1;32m"
|
|
|
|
YELLOW = "\x1b[1;33m"
|
|
|
|
RED = "\x1b[1;31m"
|
|
|
|
NORMAL = "\x1b[m"
|
|
|
|
|
2022-02-28 06:42:13 -05:00
|
|
|
SYNCTL_CACHE_FACTOR_WARNING = """\
|
|
|
|
Setting 'synctl_cache_factor' in the config is deprecated. Instead, please do
|
|
|
|
one of the following:
|
|
|
|
- Either set the environment variable 'SYNAPSE_CACHE_FACTOR'
|
|
|
|
- or set 'caches.global_factor' in the homeserver config.
|
|
|
|
--------------------------------------------------------------------------------"""
|
|
|
|
|
2018-09-25 06:19:27 -04:00
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
def pid_running(pid: int) -> bool:
|
2018-09-25 06:19:27 -04:00
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError as err:
|
|
|
|
if err.errno == errno.EPERM:
|
2021-12-02 11:07:06 -05:00
|
|
|
pass # process exists
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# When running in a container, orphan processes may not get reaped and their
|
|
|
|
# PIDs may remain valid. Try to work around the issue.
|
|
|
|
try:
|
|
|
|
with open(f"/proc/{pid}/status") as status_file:
|
|
|
|
if "zombie" in status_file.read():
|
|
|
|
return False
|
|
|
|
except Exception:
|
|
|
|
# This isn't Linux or `/proc/` is unavailable.
|
|
|
|
# Assume that the process is still running.
|
|
|
|
pass
|
|
|
|
|
|
|
|
return True
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
def write(message: str, colour: str = NORMAL, stream: TextIO = sys.stdout) -> None:
|
2018-10-17 08:46:08 -04:00
|
|
|
# Lets check if we're writing to a TTY before colouring
|
|
|
|
should_colour = False
|
2018-10-17 12:06:49 -04:00
|
|
|
try:
|
2018-10-17 08:46:08 -04:00
|
|
|
should_colour = stream.isatty()
|
2018-10-17 12:06:49 -04:00
|
|
|
except AttributeError:
|
|
|
|
# Just in case `isatty` isn't defined on everything. The python
|
|
|
|
# docs are incredibly vague.
|
|
|
|
pass
|
2018-10-17 08:46:08 -04:00
|
|
|
|
2018-10-17 12:06:49 -04:00
|
|
|
if not should_colour:
|
2018-09-25 06:19:27 -04:00
|
|
|
stream.write(message + "\n")
|
|
|
|
else:
|
|
|
|
stream.write(colour + message + NORMAL + "\n")
|
|
|
|
|
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
def abort(message: str, colour: str = RED, stream: TextIO = sys.stderr) -> NoReturn:
|
2018-09-25 06:19:27 -04:00
|
|
|
write(message, colour, stream)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2021-05-24 09:03:30 -04:00
|
|
|
def start(pidfile: str, app: str, config_files: Iterable[str], daemonize: bool) -> bool:
|
|
|
|
"""Attempts to start a synapse main or worker process.
|
2019-09-18 17:27:59 -04:00
|
|
|
Args:
|
2021-05-24 09:03:30 -04:00
|
|
|
pidfile: the pidfile we expect the process to create
|
|
|
|
app: the python module to run
|
|
|
|
config_files: config files to pass to synapse
|
|
|
|
daemonize: if True, will include a --daemonize argument to synapse
|
2019-09-18 17:27:59 -04:00
|
|
|
|
|
|
|
Returns:
|
2021-05-24 09:03:30 -04:00
|
|
|
True if the process started successfully or was already running
|
2019-09-18 17:27:59 -04:00
|
|
|
False if there was an error starting the process
|
|
|
|
"""
|
|
|
|
|
2021-05-24 09:03:30 -04:00
|
|
|
if os.path.exists(pidfile) and pid_running(int(open(pidfile).read())):
|
|
|
|
print(app + " already running")
|
2019-09-18 17:27:59 -04:00
|
|
|
return True
|
2018-09-25 06:19:27 -04:00
|
|
|
|
2021-05-24 09:03:30 -04:00
|
|
|
args = [sys.executable, "-m", app]
|
|
|
|
for c in config_files:
|
|
|
|
args += ["-c", c]
|
|
|
|
if daemonize:
|
|
|
|
args.append("--daemonize")
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(args)
|
2021-05-24 09:03:30 -04:00
|
|
|
write("started %s(%s)" % (app, ",".join(config_files)), colour=GREEN)
|
2019-09-18 17:27:59 -04:00
|
|
|
return True
|
2018-09-25 06:19:27 -04:00
|
|
|
except subprocess.CalledProcessError as e:
|
2021-05-27 05:35:06 -04:00
|
|
|
err = "%s(%s) failed to start (exit code: %d). Check the Synapse logfile" % (
|
|
|
|
app,
|
|
|
|
",".join(config_files),
|
|
|
|
e.returncode,
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
2021-05-27 05:35:06 -04:00
|
|
|
if daemonize:
|
|
|
|
err += ", or run synctl with --no-daemonize"
|
|
|
|
err += "."
|
|
|
|
write(err, colour=RED, stream=sys.stderr)
|
2019-09-18 17:27:59 -04:00
|
|
|
return False
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
|
2021-11-30 06:51:12 -05:00
|
|
|
def stop(pidfile: str, app: str) -> Optional[int]:
|
2020-05-19 08:47:45 -04:00
|
|
|
"""Attempts to kill a synapse worker from the pidfile.
|
|
|
|
Args:
|
|
|
|
pidfile: path to file containing worker's pid
|
|
|
|
app: name of the worker's appservice
|
|
|
|
|
|
|
|
Returns:
|
2021-11-30 06:51:12 -05:00
|
|
|
process id, or None if the process was not running
|
2020-05-19 08:47:45 -04:00
|
|
|
"""
|
|
|
|
|
2018-09-25 06:19:27 -04:00
|
|
|
if os.path.exists(pidfile):
|
|
|
|
pid = int(open(pidfile).read())
|
|
|
|
try:
|
|
|
|
os.kill(pid, signal.SIGTERM)
|
|
|
|
write("stopped %s" % (app,), colour=GREEN)
|
2021-11-30 06:51:12 -05:00
|
|
|
return pid
|
2018-09-25 06:19:27 -04:00
|
|
|
except OSError as err:
|
|
|
|
if err.errno == errno.ESRCH:
|
|
|
|
write("%s not running" % (app,), colour=YELLOW)
|
|
|
|
elif err.errno == errno.EPERM:
|
|
|
|
abort("Cannot stop %s: Operation not permitted" % (app,))
|
|
|
|
else:
|
|
|
|
abort("Cannot stop %s: Unknown error" % (app,))
|
2020-05-19 08:47:45 -04:00
|
|
|
else:
|
|
|
|
write(
|
|
|
|
"No running worker of %s found (from %s)\nThe process might be managed by another controller (e.g. systemd)"
|
|
|
|
% (app, pidfile),
|
|
|
|
colour=YELLOW,
|
|
|
|
)
|
2021-11-30 06:51:12 -05:00
|
|
|
return None
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
Worker = collections.namedtuple(
|
|
|
|
"Worker", ["app", "configfile", "pidfile", "cache_factor", "cache_factors"]
|
|
|
|
)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
def main() -> None:
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"action",
|
|
|
|
choices=["start", "stop", "restart"],
|
|
|
|
help="whether to start, stop or restart the synapse",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"configfile",
|
|
|
|
nargs="?",
|
|
|
|
default="homeserver.yaml",
|
2019-08-28 10:55:58 -04:00
|
|
|
help="the homeserver config file. Defaults to homeserver.yaml. May also be"
|
|
|
|
" a directory with *.yaml files",
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-w", "--worker", metavar="WORKERCONFIG", help="start or stop a single worker"
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-10-19 20:16:55 -04:00
|
|
|
"-a",
|
|
|
|
"--all-processes",
|
2018-09-25 06:19:27 -04:00
|
|
|
metavar="WORKERCONFIGDIR",
|
|
|
|
help="start or stop all the workers in the given directory"
|
2018-10-19 20:16:55 -04:00
|
|
|
" and the main synapse process",
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
2019-06-10 08:01:56 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-daemonize",
|
|
|
|
action="store_false",
|
2019-07-02 05:14:08 -04:00
|
|
|
dest="daemonize",
|
2019-06-10 09:17:35 -04:00
|
|
|
help="Run synapse in the foreground for debugging. "
|
2019-07-02 05:14:08 -04:00
|
|
|
"Will work only if the daemonize option is not set in the config.",
|
2019-06-10 08:01:56 -04:00
|
|
|
)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
if options.worker and options.all_processes:
|
2018-10-19 20:16:55 -04:00
|
|
|
write('Cannot use "--worker" with "--all-processes"', stream=sys.stderr)
|
2018-09-25 06:19:27 -04:00
|
|
|
sys.exit(1)
|
2019-07-02 05:14:08 -04:00
|
|
|
if not options.daemonize and options.all_processes:
|
2019-06-10 08:01:56 -04:00
|
|
|
write('Cannot use "--no-daemonize" with "--all-processes"', stream=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
configfile = options.configfile
|
|
|
|
|
|
|
|
if not os.path.exists(configfile):
|
|
|
|
write(
|
2021-05-24 09:03:30 -04:00
|
|
|
f"Config file {configfile} does not exist.\n"
|
|
|
|
f"To generate a config file, run:\n"
|
|
|
|
f" {sys.executable} -m {MAIN_PROCESS}"
|
|
|
|
f" -c {configfile} --generate-config"
|
|
|
|
f" --server-name=<server name> --report-stats=<yes/no>\n",
|
2018-09-25 06:19:27 -04:00
|
|
|
stream=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-08-28 10:55:58 -04:00
|
|
|
config_files = find_config_files([configfile])
|
|
|
|
config = {}
|
|
|
|
for config_file in config_files:
|
|
|
|
with open(config_file) as file_stream:
|
|
|
|
yaml_config = yaml.safe_load(file_stream)
|
2020-07-03 08:19:03 -04:00
|
|
|
if yaml_config is not None:
|
|
|
|
config.update(yaml_config)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
pidfile = config["pid_file"]
|
|
|
|
cache_factor = config.get("synctl_cache_factor")
|
|
|
|
start_stop_synapse = True
|
|
|
|
|
|
|
|
if cache_factor:
|
2022-02-28 06:42:13 -05:00
|
|
|
write(SYNCTL_CACHE_FACTOR_WARNING)
|
2018-09-25 06:19:27 -04:00
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR"] = str(cache_factor)
|
|
|
|
|
|
|
|
cache_factors = config.get("synctl_cache_factors", {})
|
2020-06-16 08:51:47 -04:00
|
|
|
for cache_name, factor in cache_factors.items():
|
2018-09-25 06:19:27 -04:00
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR_" + cache_name.upper()] = str(factor)
|
|
|
|
|
|
|
|
worker_configfiles = []
|
|
|
|
if options.worker:
|
|
|
|
start_stop_synapse = False
|
|
|
|
worker_configfile = options.worker
|
|
|
|
if not os.path.exists(worker_configfile):
|
|
|
|
write(
|
2018-10-19 20:16:55 -04:00
|
|
|
"No worker config found at %r" % (worker_configfile,), stream=sys.stderr
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
worker_configfiles.append(worker_configfile)
|
|
|
|
|
|
|
|
if options.all_processes:
|
|
|
|
# To start the main synapse with -a you need to add a worker file
|
|
|
|
# with worker_app == "synapse.app.homeserver"
|
|
|
|
start_stop_synapse = False
|
|
|
|
worker_configdir = options.all_processes
|
|
|
|
if not os.path.isdir(worker_configdir):
|
|
|
|
write(
|
|
|
|
"No worker config directory found at %r" % (worker_configdir,),
|
|
|
|
stream=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
2018-10-19 20:16:55 -04:00
|
|
|
worker_configfiles.extend(
|
|
|
|
sorted(glob.glob(os.path.join(worker_configdir, "*.yaml")))
|
|
|
|
)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
workers = []
|
|
|
|
for worker_configfile in worker_configfiles:
|
|
|
|
with open(worker_configfile) as stream:
|
2019-03-22 06:20:17 -04:00
|
|
|
worker_config = yaml.safe_load(stream)
|
2018-09-25 06:19:27 -04:00
|
|
|
worker_app = worker_config["worker_app"]
|
|
|
|
if worker_app == "synapse.app.homeserver":
|
|
|
|
# We need to special case all of this to pick up options that may
|
|
|
|
# be set in the main config file or in this worker config file.
|
2018-10-19 20:16:55 -04:00
|
|
|
worker_pidfile = worker_config.get("pid_file") or pidfile
|
|
|
|
worker_cache_factor = (
|
|
|
|
worker_config.get("synctl_cache_factor") or cache_factor
|
2018-09-25 06:19:27 -04:00
|
|
|
)
|
2018-09-26 08:24:07 -04:00
|
|
|
worker_cache_factors = (
|
2018-10-19 20:16:55 -04:00
|
|
|
worker_config.get("synctl_cache_factors") or cache_factors
|
2018-09-26 08:24:07 -04:00
|
|
|
)
|
2018-09-25 06:19:27 -04:00
|
|
|
# The master process doesn't support using worker_* config.
|
|
|
|
for key in worker_config:
|
|
|
|
if key == "worker_app": # But we allow worker_app
|
|
|
|
continue
|
2018-10-19 20:16:55 -04:00
|
|
|
assert not key.startswith(
|
|
|
|
"worker_"
|
|
|
|
), "Main process cannot use worker_* config"
|
2018-09-25 06:19:27 -04:00
|
|
|
else:
|
|
|
|
worker_pidfile = worker_config["worker_pid_file"]
|
|
|
|
worker_cache_factor = worker_config.get("synctl_cache_factor")
|
2018-09-26 08:24:07 -04:00
|
|
|
worker_cache_factors = worker_config.get("synctl_cache_factors", {})
|
2018-10-19 20:16:55 -04:00
|
|
|
workers.append(
|
|
|
|
Worker(
|
|
|
|
worker_app,
|
|
|
|
worker_configfile,
|
|
|
|
worker_pidfile,
|
|
|
|
worker_cache_factor,
|
|
|
|
worker_cache_factors,
|
|
|
|
)
|
|
|
|
)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
action = options.action
|
|
|
|
|
|
|
|
if action == "stop" or action == "restart":
|
2021-11-30 06:51:12 -05:00
|
|
|
running_pids = []
|
2018-09-25 06:19:27 -04:00
|
|
|
for worker in workers:
|
2021-11-30 06:51:12 -05:00
|
|
|
pid = stop(worker.pidfile, worker.app)
|
|
|
|
if pid is not None:
|
|
|
|
running_pids.append(pid)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
if start_stop_synapse:
|
2021-11-30 06:51:12 -05:00
|
|
|
pid = stop(pidfile, MAIN_PROCESS)
|
|
|
|
if pid is not None:
|
|
|
|
running_pids.append(pid)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
if len(running_pids) > 0:
|
2021-11-30 06:51:12 -05:00
|
|
|
write("Waiting for processes to exit...")
|
2018-09-25 06:19:27 -04:00
|
|
|
for running_pid in running_pids:
|
|
|
|
while pid_running(running_pid):
|
|
|
|
time.sleep(0.2)
|
2021-11-30 06:51:12 -05:00
|
|
|
write("All processes exited")
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
if action == "start" or action == "restart":
|
2019-09-18 17:27:59 -04:00
|
|
|
error = False
|
2018-09-25 06:19:27 -04:00
|
|
|
if start_stop_synapse:
|
2021-05-24 09:03:30 -04:00
|
|
|
if not start(pidfile, MAIN_PROCESS, (configfile,), options.daemonize):
|
2019-09-18 17:27:59 -04:00
|
|
|
error = True
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
for worker in workers:
|
2018-09-26 08:24:07 -04:00
|
|
|
env = os.environ.copy()
|
|
|
|
|
2018-09-25 06:19:27 -04:00
|
|
|
if worker.cache_factor:
|
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR"] = str(worker.cache_factor)
|
|
|
|
|
2020-06-16 08:51:47 -04:00
|
|
|
for cache_name, factor in worker.cache_factors.items():
|
2018-09-26 08:24:07 -04:00
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR_" + cache_name.upper()] = str(factor)
|
|
|
|
|
2021-05-24 09:03:30 -04:00
|
|
|
if not start(
|
|
|
|
worker.pidfile,
|
|
|
|
worker.app,
|
|
|
|
(configfile, worker.configfile),
|
|
|
|
options.daemonize,
|
|
|
|
):
|
2019-09-18 17:27:59 -04:00
|
|
|
error = True
|
2018-09-25 06:19:27 -04:00
|
|
|
|
2018-09-26 08:24:07 -04:00
|
|
|
# Reset env back to the original
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(env)
|
2018-09-25 06:19:27 -04:00
|
|
|
|
2019-09-18 17:27:59 -04:00
|
|
|
if error:
|
|
|
|
exit(1)
|
|
|
|
|
2018-09-25 06:19:27 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|