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

Commit bf541ac

Browse files
committed
Add sharding_allowed to the WorkerTemplate rather than having a separate function for that
1 parent 7918811 commit bf541ac

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

docker/configure_workers_and_start.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class WorkerTemplate:
9090
shared_extra_conf: Callable[[str], Dict[str, Any]] = lambda _worker_name: {}
9191
worker_extra_conf: str = ""
9292

93+
# True if and only if multiple of this worker type are allowed.
94+
sharding_allowed: bool = True
95+
9396

9497
# Workers with exposed endpoints needs either "client", "federation", or "media" listener_resources
9598
# Watching /_matrix/client needs a "client" listener
@@ -218,6 +221,7 @@ class WorkerTemplate:
218221
# This worker cannot be sharded. Therefore, there should only ever be one
219222
# background worker. This is enforced for the safety of your database.
220223
shared_extra_conf=lambda worker_name: {"run_background_tasks_on": worker_name},
224+
sharding_allowed=False,
221225
),
222226
"event_creator": WorkerTemplate(
223227
listener_resources={"client"},
@@ -243,13 +247,15 @@ class WorkerTemplate:
243247
shared_extra_conf=lambda worker_name: {
244248
"stream_writers": {"account_data": [worker_name]}
245249
},
250+
sharding_allowed=False,
246251
),
247252
"presence": WorkerTemplate(
248253
listener_resources={"client", "replication"},
249254
endpoint_patterns={"^/_matrix/client/(api/v1|r0|v3|unstable)/presence/"},
250255
shared_extra_conf=lambda worker_name: {
251256
"stream_writers": {"presence": [worker_name]}
252257
},
258+
sharding_allowed=False,
253259
),
254260
"receipts": WorkerTemplate(
255261
listener_resources={"client", "replication"},
@@ -260,20 +266,23 @@ class WorkerTemplate:
260266
shared_extra_conf=lambda worker_name: {
261267
"stream_writers": {"receipts": [worker_name]}
262268
},
269+
sharding_allowed=False,
263270
),
264271
"to_device": WorkerTemplate(
265272
listener_resources={"client", "replication"},
266273
endpoint_patterns={"^/_matrix/client/(r0|v3|unstable)/sendToDevice/"},
267274
shared_extra_conf=lambda worker_name: {
268275
"stream_writers": {"to_device": [worker_name]}
269276
},
277+
sharding_allowed=False,
270278
),
271279
"typing": WorkerTemplate(
272280
listener_resources={"client", "replication"},
273281
endpoint_patterns={"^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/typing"},
274282
shared_extra_conf=lambda worker_name: {
275283
"stream_writers": {"typing": [worker_name]}
276284
},
285+
sharding_allowed=False,
277286
),
278287
}
279288

@@ -496,23 +505,6 @@ def apply_requested_multiplier_for_worker(worker_types: List[str]) -> List[str]:
496505
return new_worker_types
497506

498507

499-
def is_sharding_allowed_for_worker_type(worker_type: str) -> bool:
500-
"""Helper to check to make sure worker types that cannot have multiples do not.
501-
502-
Args:
503-
worker_type: The type of worker to check against.
504-
Returns: True if allowed, False if not
505-
"""
506-
return worker_type not in [
507-
"background_worker",
508-
"account_data",
509-
"presence",
510-
"receipts",
511-
"typing",
512-
"to_device",
513-
]
514-
515-
516508
def split_and_strip_string(
517509
given_string: str, split_char: str, max_split: SupportsIndex = -1
518510
) -> List[str]:
@@ -638,7 +630,7 @@ def parse_worker_types(
638630
)
639631

640632
if worker_type in worker_type_shard_counter:
641-
if not is_sharding_allowed_for_worker_type(worker_type):
633+
if not WORKERS_CONFIG[worker_type].sharding_allowed:
642634
error(
643635
f"There can be only a single worker with {worker_type} "
644636
"type. Please recount and remove."

0 commit comments

Comments
 (0)