2020-06-11 00:54:34 +02:00
|
|
|
import json
|
|
|
|
import os
|
2022-09-01 01:11:27 +02:00
|
|
|
import re
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-11-02 23:51:05 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from zerver.models import Realm
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-11-07 02:13:37 +01:00
|
|
|
shard_map = {}
|
2022-09-01 01:11:27 +02:00
|
|
|
shard_regexes = []
|
2018-11-07 02:13:37 +01:00
|
|
|
if os.path.exists("/etc/zulip/sharding.json"):
|
|
|
|
with open("/etc/zulip/sharding.json") as f:
|
2022-09-01 01:11:27 +02:00
|
|
|
data = json.loads(f.read())
|
|
|
|
shard_map = data.get(
|
|
|
|
"shard_map",
|
|
|
|
data, # backwards compatibility
|
|
|
|
)
|
|
|
|
shard_regexes = [
|
|
|
|
(re.compile(regex, re.I), port) for regex, port in data.get("shard_regexes", [])
|
|
|
|
]
|
2018-11-02 23:51:05 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-02 23:51:05 +01:00
|
|
|
def get_tornado_port(realm: Realm) -> int:
|
2022-09-01 01:11:27 +02:00
|
|
|
if realm.host in shard_map:
|
|
|
|
return shard_map[realm.host]
|
|
|
|
|
|
|
|
for regex, port in shard_regexes:
|
|
|
|
if regex.match(realm.host):
|
|
|
|
return port
|
|
|
|
|
|
|
|
return settings.TORNADO_PORTS[0]
|
2018-11-02 23:51:05 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-02 23:51:05 +01:00
|
|
|
def get_tornado_uri(realm: Realm) -> str:
|
|
|
|
port = get_tornado_port(realm)
|
2020-06-13 08:59:37 +02:00
|
|
|
return f"http://127.0.0.1:{port}"
|
2018-11-03 00:06:13 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-03 00:06:13 +01:00
|
|
|
def notify_tornado_queue_name(port: int) -> str:
|
|
|
|
if settings.TORNADO_PROCESSES == 1:
|
|
|
|
return "notify_tornado"
|
2020-06-13 08:59:37 +02:00
|
|
|
return f"notify_tornado_port_{port}"
|