Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit cae4121

Browse files
authored
Make systemd-with-workers doc official (#7234)
Simplify and update this documentation, and make it part of the core dist.
1 parent c11d24d commit cae4121

10 files changed

Lines changed: 134 additions & 194 deletions

File tree

changelog.d/7234.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update the contributed documentation on managing synapse workers with systemd, and bring it into the core distribution.
Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,2 @@
1-
# Setup Synapse with Workers and Systemd
2-
3-
This is a setup for managing synapse with systemd including support for
4-
managing workers. It provides a `matrix-synapse`, as well as a
5-
`matrix-synapse-worker@` service for any workers you require. Additionally to
6-
group the required services it sets up a `matrix.target`. You can use this to
7-
automatically start any bot- or bridge-services. More on this in
8-
[Bots and Bridges](#bots-and-bridges).
9-
10-
See the folder [system](system) for any service and target files.
11-
12-
The folder [workers](workers) contains an example configuration for the
13-
`federation_reader` worker. Pay special attention to the name of the
14-
configuration file. In order to work with the `matrix-synapse-worker@.service`
15-
service, it needs to have the exact same name as the worker app.
16-
17-
This setup expects neither the homeserver nor any workers to fork. Forking is
18-
handled by systemd.
19-
20-
## Setup
21-
22-
1. Adjust your matrix configs. Make sure that the worker config files have the
23-
exact same name as the worker app. Compare `matrix-synapse-worker@.service` for
24-
why. You can find an example worker config in the [workers](workers) folder. See
25-
below for relevant settings in the `homeserver.yaml`.
26-
2. Copy the `*.service` and `*.target` files in [system](system) to
27-
`/etc/systemd/system`.
28-
3. `systemctl enable matrix-synapse.service` this adds the homeserver
29-
app to the `matrix.target`
30-
4. *Optional.* `systemctl enable
31-
matrix-synapse-worker@federation_reader.service` this adds the federation_reader
32-
app to the `matrix-synapse.service`
33-
5. *Optional.* Repeat step 4 for any additional workers you require.
34-
6. *Optional.* Add any bots or bridges by enabling them.
35-
7. Start all matrix related services via `systemctl start matrix.target`
36-
8. *Optional.* Enable autostart of all matrix related services on system boot
37-
via `systemctl enable matrix.target`
38-
39-
## Usage
40-
41-
After you have setup you can use the following commands to manage your synapse
42-
installation:
43-
44-
```
45-
# Start matrix-synapse, all workers and any enabled bots or bridges.
46-
systemctl start matrix.target
47-
48-
# Restart matrix-synapse and all workers (not necessarily restarting bots
49-
# or bridges, see "Bots and Bridges")
50-
systemctl restart matrix-synapse.service
51-
52-
# Stop matrix-synapse and all workers (not necessarily restarting bots
53-
# or bridges, see "Bots and Bridges")
54-
systemctl stop matrix-synapse.service
55-
56-
# Restart a specific worker (i. e. federation_reader), the homeserver is
57-
# unaffected by this.
58-
systemctl restart matrix-synapse-worker@federation_reader.service
59-
60-
# Add a new worker (assuming all configs are setup already)
61-
systemctl enable matrix-synapse-worker@federation_writer.service
62-
systemctl restart matrix-synapse.service
63-
```
64-
65-
## The Configs
66-
67-
Make sure the `worker_app` is set in the `homeserver.yaml` and it does not fork.
68-
69-
```
70-
worker_app: synapse.app.homeserver
71-
daemonize: false
72-
```
73-
74-
None of the workers should fork, as forking is handled by systemd. Hence make
75-
sure this is present in all worker config files.
76-
77-
```
78-
worker_daemonize: false
79-
```
80-
81-
The config files of all workers are expected to be located in
82-
`/etc/matrix-synapse/workers`. If you want to use a different location you have
83-
to edit the provided `*.service` files accordingly.
84-
85-
## Bots and Bridges
86-
87-
Most bots and bridges do not care if the homeserver goes down or is restarted.
88-
Depending on the implementation this may crash them though. So look up the docs
89-
or ask the community of the specific bridge or bot you want to run to make sure
90-
you choose the correct setup.
91-
92-
Whichever configuration you choose, after the setup the following will enable
93-
automatically starting (and potentially restarting) your bot/bridge with the
94-
`matrix.target`.
95-
96-
```
97-
systemctl enable <yourBotOrBridgeName>.service
98-
```
99-
100-
**Note** that from an inactive synapse the bots/bridges will only be started with
101-
synapse if you start the `matrix.target`, not if you start the
102-
`matrix-synapse.service`. This is on purpose. Think of `matrix-synapse.service`
103-
as *just* synapse, but `matrix.target` being anything matrix related, including
104-
synapse and any and all enabled bots and bridges.
105-
106-
### Start with synapse but ignore synapse going down
107-
108-
If the bridge can handle shutdowns of the homeserver you'll want to install the
109-
service in the `matrix.target` and optionally add a
110-
`After=matrix-synapse.service` dependency to have the bot/bridge start after
111-
synapse on starting everything.
112-
113-
In this case the service file should look like this.
114-
115-
```
116-
[Unit]
117-
# ...
118-
# Optional, this will only ensure that if you start everything, synapse will
119-
# be started before the bot/bridge will be started.
120-
After=matrix-synapse.service
121-
122-
[Service]
123-
# ...
124-
125-
[Install]
126-
WantedBy=matrix.target
127-
```
128-
129-
### Stop/restart when synapse stops/restarts
130-
131-
If the bridge can't handle shutdowns of the homeserver you'll still want to
132-
install the service in the `matrix.target` but also have to specify the
133-
`After=matrix-synapse.service` *and* `BindsTo=matrix-synapse.service`
134-
dependencies to have the bot/bridge stop/restart with synapse.
135-
136-
In this case the service file should look like this.
137-
138-
```
139-
[Unit]
140-
# ...
141-
# Mandatory
142-
After=matrix-synapse.service
143-
BindsTo=matrix-synapse.service
144-
145-
[Service]
146-
# ...
147-
148-
[Install]
149-
WantedBy=matrix.target
150-
```
1+
The documentation for using systemd to manage synapse workers is now part of
2+
the main synapse distribution. See [docs/systemd-with-workers](../../docs/systemd-with-workers).

contrib/systemd-with-workers/system/matrix-synapse-worker@.service

Lines changed: 0 additions & 19 deletions
This file was deleted.

contrib/systemd-with-workers/system/matrix.target

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Setting up Synapse with Workers and Systemd
2+
3+
This is a setup for managing synapse with systemd, including support for
4+
managing workers. It provides a `matrix-synapse` service for the master, as
5+
well as a `matrix-synapse-worker@` service template for any workers you
6+
require. Additionally, to group the required services, it sets up a
7+
`matrix-synapse.target`.
8+
9+
See the folder [system](system) for the systemd unit files.
10+
11+
The folder [workers](workers) contains an example configuration for the
12+
`federation_reader` worker.
13+
14+
## Synapse configuration files
15+
16+
See [workers.md](../workers.md) for information on how to set up the
17+
configuration files and reverse-proxy correctly. You can find an example worker
18+
config in the [workers](workers) folder.
19+
20+
Systemd manages daemonization itself, so ensure that none of the configuration
21+
files set either `daemonize` or `worker_daemonize`.
22+
23+
The config files of all workers are expected to be located in
24+
`/etc/matrix-synapse/workers`. If you want to use a different location, edit
25+
the provided `*.service` files accordingly.
26+
27+
There is no need for a separate configuration file for the master process.
28+
29+
## Set up
30+
31+
1. Adjust synapse configuration files as above.
32+
1. Copy the `*.service` and `*.target` files in [system](system) to
33+
`/etc/systemd/system`.
34+
1. Run `systemctl deamon-reload` to tell systemd to load the new unit files.
35+
1. Run `systemctl enable matrix-synapse.service`. This will configure the
36+
synapse master process to be started as part of the `matrix-synapse.target`
37+
target.
38+
1. For each worker process to be enabled, run `systemctl enable
39+
matrix-synapse-worker@<worker_name>.service`. For each `<worker_name>`, there
40+
should be a corresponding configuration file
41+
`/etc/matrix-synapse/workers/<worker_name>.yaml`.
42+
1. Start all the synapse processes with `systemctl start matrix-synapse.target`.
43+
1. Tell systemd to start synapse on boot with `systemctl enable matrix-synapse.target`/
44+
45+
## Usage
46+
47+
Once the services are correctly set up, you can use the following commands
48+
to manage your synapse installation:
49+
50+
```sh
51+
# Restart Synapse master and all workers
52+
systemctl restart matrix-synapse.target
53+
54+
# Stop Synapse and all workers
55+
systemctl stop matrix-synapse.target
56+
57+
# Restart the master alone
58+
systemctl start matrix-synapse.service
59+
60+
# Restart a specific worker (eg. federation_reader); the master is
61+
# unaffected by this.
62+
systemctl restart matrix-synapse-worker@federation_reader.service
63+
64+
# Add a new worker (assuming all configs are set up already)
65+
systemctl enable matrix-synapse-worker@federation_writer.service
66+
systemctl restart matrix-synapse.target
67+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[Unit]
2+
Description=Synapse %i
3+
4+
# This service should be restarted when the synapse target is restarted.
5+
PartOf=matrix-synapse.target
6+
7+
[Service]
8+
Type=notify
9+
NotifyAccess=main
10+
User=matrix-synapse
11+
WorkingDirectory=/var/lib/matrix-synapse
12+
EnvironmentFile=/etc/default/matrix-synapse
13+
ExecStart=/opt/venvs/matrix-synapse/bin/python -m synapse.app.generic_worker --config-path=/etc/matrix-synapse/homeserver.yaml --config-path=/etc/matrix-synapse/conf.d/ --config-path=/etc/matrix-synapse/workers/%i.yaml
14+
ExecReload=/bin/kill -HUP $MAINPID
15+
Restart=always
16+
RestartSec=3
17+
SyslogIdentifier=matrix-synapse-%i
18+
19+
[Install]
20+
WantedBy=matrix-synapse.target

contrib/systemd-with-workers/system/matrix-synapse.service renamed to docs/systemd-with-workers/system/matrix-synapse.service

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[Unit]
2-
Description=Synapse Matrix Homeserver
2+
Description=Synapse master
3+
4+
# This service should be restarted when the synapse target is restarted.
5+
PartOf=matrix-synapse.target
36

47
[Service]
58
Type=notify
@@ -15,4 +18,4 @@ RestartSec=3
1518
SyslogIdentifier=matrix-synapse
1619

1720
[Install]
18-
WantedBy=matrix.target
21+
WantedBy=matrix-synapse.target
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
Description=Synapse parent target
3+
After=network.target
4+
5+
[Install]
6+
WantedBy=multi-user.target

contrib/systemd-with-workers/workers/federation_reader.yaml renamed to docs/systemd-with-workers/workers/federation_reader.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ worker_listeners:
1010
resources:
1111
- names: [federation]
1212

13-
worker_daemonize: false
1413
worker_log_config: /etc/matrix-synapse/federation-reader-log.yaml

docs/workers.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,20 @@ synapse process.)
5252

5353
You then create a set of configs for the various worker processes. These
5454
should be worker configuration files, and should be stored in a dedicated
55-
subdirectory, to allow synctl to manipulate them. An additional configuration
56-
for the master synapse process will need to be created because the process will
57-
not be started automatically. That configuration should look like this:
58-
59-
worker_app: synapse.app.homeserver
60-
daemonize: true
55+
subdirectory, to allow synctl to manipulate them.
6156

6257
Each worker configuration file inherits the configuration of the main homeserver
6358
configuration file. You can then override configuration specific to that worker,
6459
e.g. the HTTP listener that it provides (if any); logging configuration; etc.
6560
You should minimise the number of overrides though to maintain a usable config.
6661

67-
You must specify the type of worker application (`worker_app`). The currently
68-
available worker applications are listed below. You must also specify the
69-
replication endpoints that it's talking to on the main synapse process.
70-
`worker_replication_host` should specify the host of the main synapse,
71-
`worker_replication_port` should point to the TCP replication listener port and
72-
`worker_replication_http_port` should point to the HTTP replication port.
62+
In the config file for each worker, you must specify the type of worker
63+
application (`worker_app`). The currently available worker applications are
64+
listed below. You must also specify the replication endpoints that it's talking
65+
to on the main synapse process. `worker_replication_host` should specify the
66+
host of the main synapse, `worker_replication_port` should point to the TCP
67+
replication listener port and `worker_replication_http_port` should point to
68+
the HTTP replication port.
7369

7470
Currently, the `event_creator` and `federation_reader` workers require specifying
7571
`worker_replication_http_port`.
@@ -90,8 +86,6 @@ For instance:
9086
- names:
9187
- client
9288

93-
worker_daemonize: True
94-
worker_pid_file: /home/matrix/synapse/synchrotron.pid
9589
worker_log_config: /home/matrix/synapse/config/synchrotron_log_config.yaml
9690

9791
...is a full configuration for a synchrotron worker instance, which will expose a
@@ -101,7 +95,31 @@ by the main synapse.
10195
Obviously you should configure your reverse-proxy to route the relevant
10296
endpoints to the worker (`localhost:8083` in the above example).
10397

104-
Finally, to actually run your worker-based synapse, you must pass synctl the -a
98+
Finally, you need to start your worker processes. This can be done with either
99+
`synctl` or your distribution's preferred service manager such as `systemd`. We
100+
recommend the use of `systemd` where available: for information on setting up
101+
`systemd` to start synapse workers, see
102+
[systemd-with-workers](systemd-with-workers). To use `synctl`, see below.
103+
104+
### Using synctl
105+
106+
If you want to use `synctl` to manage your synapse processes, you will need to
107+
create an an additional configuration file for the master synapse process. That
108+
configuration should look like this:
109+
110+
```yaml
111+
worker_app: synapse.app.homeserver
112+
```
113+
114+
Additionally, each worker app must be configured with the name of a "pid file",
115+
to which it will write its process ID when it starts. For example, for a
116+
synchrotron, you might write:
117+
118+
```yaml
119+
worker_pid_file: /home/matrix/synapse/synchrotron.pid
120+
```
121+
122+
Finally, to actually run your worker-based synapse, you must pass synctl the `-a`
105123
commandline option to tell it to operate on all the worker configurations found
106124
in the given directory, e.g.:
107125

0 commit comments

Comments
 (0)