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

Commit db1d3a6

Browse files
committed
Use a lambda for the worker name rather than search and replace later
1 parent 16afa43 commit db1d3a6

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

docker/configure_workers_and_start.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from pathlib import Path
6060
from typing import (
6161
Any,
62+
Callable,
6263
Dict,
6364
List,
6465
Mapping,
@@ -80,10 +81,6 @@
8081
MAIN_PROCESS_UNIX_SOCKET_PUBLIC_PATH = "/run/main_public.sock"
8182
MAIN_PROCESS_UNIX_SOCKET_PRIVATE_PATH = "/run/main_private.sock"
8283

83-
# A simple name used as a placeholder in the WORKERS_CONFIG below. This will be replaced
84-
# during processing with the name of the worker.
85-
WORKER_PLACEHOLDER_NAME = "placeholder_name"
86-
8784

8885
# Workers with exposed endpoints needs either "client", "federation", or "media" listener_resources
8986
# Watching /_matrix/client needs a "client" listener
@@ -95,24 +92,25 @@
9592
class WorkerTemplate:
9693
listener_resources: List[str] = field(default_factory=list)
9794
endpoint_patterns: List[str] = field(default_factory=list)
98-
shared_extra_conf: Dict[str, Any] = field(default_factory=dict)
95+
# (worker_name) -> {}
96+
shared_extra_conf: Callable[[str], Dict[str, Any]] = lambda _worker_name: {}
9997
worker_extra_conf: str = ""
10098

10199

102100
WORKERS_CONFIG: Dict[str, WorkerTemplate] = {
103101
"pusher": WorkerTemplate(
104102
listener_resources=[],
105103
endpoint_patterns=[],
106-
shared_extra_conf={},
104+
shared_extra_conf=lambda _worker_name: {},
107105
worker_extra_conf="",
108106
),
109107
"user_dir": WorkerTemplate(
110108
listener_resources=["client"],
111109
endpoint_patterns=[
112110
"^/_matrix/client/(api/v1|r0|v3|unstable)/user_directory/search$"
113111
],
114-
shared_extra_conf={
115-
"update_user_directory_from_worker": WORKER_PLACEHOLDER_NAME
112+
shared_extra_conf=lambda worker_name: {
113+
"update_user_directory_from_worker": worker_name
116114
},
117115
worker_extra_conf="",
118116
),
@@ -127,22 +125,24 @@ class WorkerTemplate:
127125
"^/_synapse/admin/v1/quarantine_media/.*$",
128126
],
129127
# The first configured media worker will run the media background jobs
130-
shared_extra_conf={
128+
shared_extra_conf=lambda worker_name: {
131129
"enable_media_repo": False,
132-
"media_instance_running_background_jobs": WORKER_PLACEHOLDER_NAME,
130+
"media_instance_running_background_jobs": worker_name,
133131
},
134132
worker_extra_conf="enable_media_repo: true",
135133
),
136134
"appservice": WorkerTemplate(
137135
listener_resources=[],
138136
endpoint_patterns=[],
139-
shared_extra_conf={"notify_appservices_from_worker": WORKER_PLACEHOLDER_NAME},
137+
shared_extra_conf=lambda worker_name: {
138+
"notify_appservices_from_worker": worker_name
139+
},
140140
worker_extra_conf="",
141141
),
142142
"federation_sender": WorkerTemplate(
143143
listener_resources=[],
144144
endpoint_patterns=[],
145-
shared_extra_conf={},
145+
shared_extra_conf=lambda _worker_name: {},
146146
worker_extra_conf="",
147147
),
148148
"synchrotron": WorkerTemplate(
@@ -153,7 +153,7 @@ class WorkerTemplate:
153153
"^/_matrix/client/(api/v1|r0|v3)/initialSync$",
154154
"^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$",
155155
],
156-
shared_extra_conf={},
156+
shared_extra_conf=lambda _worker_name: {},
157157
worker_extra_conf="",
158158
),
159159
"client_reader": WorkerTemplate(
@@ -187,7 +187,7 @@ class WorkerTemplate:
187187
"^/_matrix/client/(r0|v3|unstable)/capabilities$",
188188
"^/_matrix/client/(r0|v3|unstable)/notifications$",
189189
],
190-
shared_extra_conf={},
190+
shared_extra_conf=lambda _worker_name: {},
191191
worker_extra_conf="",
192192
),
193193
"federation_reader": WorkerTemplate(
@@ -213,27 +213,27 @@ class WorkerTemplate:
213213
"^/_matrix/federation/(v1|v2)/get_groups_publicised$",
214214
"^/_matrix/key/v2/query",
215215
],
216-
shared_extra_conf={},
216+
shared_extra_conf=lambda _worker_name: {},
217217
worker_extra_conf="",
218218
),
219219
"federation_inbound": WorkerTemplate(
220220
listener_resources=["federation"],
221221
endpoint_patterns=["/_matrix/federation/(v1|v2)/send/"],
222-
shared_extra_conf={},
222+
shared_extra_conf=lambda _worker_name: {},
223223
worker_extra_conf="",
224224
),
225225
"event_persister": WorkerTemplate(
226226
listener_resources=["replication"],
227227
endpoint_patterns=[],
228-
shared_extra_conf={},
228+
shared_extra_conf=lambda _worker_name: {},
229229
worker_extra_conf="",
230230
),
231231
"background_worker": WorkerTemplate(
232232
listener_resources=[],
233233
endpoint_patterns=[],
234234
# This worker cannot be sharded. Therefore, there should only ever be one
235235
# background worker. This is enforced for the safety of your database.
236-
shared_extra_conf={"run_background_tasks_on": WORKER_PLACEHOLDER_NAME},
236+
shared_extra_conf=lambda worker_name: {"run_background_tasks_on": worker_name},
237237
worker_extra_conf="",
238238
),
239239
"event_creator": WorkerTemplate(
@@ -246,13 +246,13 @@ class WorkerTemplate:
246246
"^/_matrix/client/(api/v1|r0|v3|unstable)/knock/",
247247
"^/_matrix/client/(api/v1|r0|v3|unstable)/profile/",
248248
],
249-
shared_extra_conf={},
249+
shared_extra_conf=lambda _worker_name: {},
250250
worker_extra_conf="",
251251
),
252252
"frontend_proxy": WorkerTemplate(
253253
listener_resources=["client", "replication"],
254254
endpoint_patterns=["^/_matrix/client/(api/v1|r0|v3|unstable)/keys/upload"],
255-
shared_extra_conf={},
255+
shared_extra_conf=lambda _worker_name: {},
256256
worker_extra_conf="",
257257
),
258258
"account_data": WorkerTemplate(
@@ -261,13 +261,13 @@ class WorkerTemplate:
261261
"^/_matrix/client/(r0|v3|unstable)/.*/tags",
262262
"^/_matrix/client/(r0|v3|unstable)/.*/account_data",
263263
],
264-
shared_extra_conf={},
264+
shared_extra_conf=lambda _worker_name: {},
265265
worker_extra_conf="",
266266
),
267267
"presence": WorkerTemplate(
268268
listener_resources=["client", "replication"],
269269
endpoint_patterns=["^/_matrix/client/(api/v1|r0|v3|unstable)/presence/"],
270-
shared_extra_conf={},
270+
shared_extra_conf=lambda _worker_name: {},
271271
worker_extra_conf="",
272272
),
273273
"receipts": WorkerTemplate(
@@ -276,19 +276,19 @@ class WorkerTemplate:
276276
"^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt",
277277
"^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers",
278278
],
279-
shared_extra_conf={},
279+
shared_extra_conf=lambda _worker_name: {},
280280
worker_extra_conf="",
281281
),
282282
"to_device": WorkerTemplate(
283283
listener_resources=["client", "replication"],
284284
endpoint_patterns=["^/_matrix/client/(r0|v3|unstable)/sendToDevice/"],
285-
shared_extra_conf={},
285+
shared_extra_conf=lambda _worker_name: {},
286286
worker_extra_conf="",
287287
),
288288
"typing": WorkerTemplate(
289289
listener_resources=["client", "replication"],
290290
endpoint_patterns=["^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/typing"],
291-
shared_extra_conf={},
291+
shared_extra_conf=lambda _worker_name: {},
292292
worker_extra_conf="",
293293
),
294294
}
@@ -459,9 +459,9 @@ def merge_worker_template_configs(
459459
)
460460

461461
# merge dictionaries; the worker name will be replaced later
462-
new_template.shared_extra_conf = {
463-
**new_template.shared_extra_conf,
464-
**to_be_merged_template.shared_extra_conf,
462+
new_template.shared_extra_conf = lambda worker_name: {
463+
**new_template.shared_extra_conf(worker_name),
464+
**to_be_merged_template.shared_extra_conf(worker_name),
465465
}
466466

467467
# There is only one worker type that has a 'worker_extra_conf' and it is
@@ -485,10 +485,7 @@ def insert_worker_name_for_worker_config(
485485
Returns: Copy of the dict with newly inserted worker name
486486
"""
487487
dict_to_edit = dataclasses.asdict(existing_template)
488-
for k, v in dict_to_edit["shared_extra_conf"].items():
489-
# Only proceed if it's the placeholder name string
490-
if v == WORKER_PLACEHOLDER_NAME:
491-
dict_to_edit["shared_extra_conf"][k] = worker_name
488+
dict_to_edit["shared_extra_conf"] = existing_template.shared_extra_conf(worker_name)
492489
return dict_to_edit
493490

494491

0 commit comments

Comments
 (0)