-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
33 lines (23 loc) · 910 Bytes
/
Copy pathdeploy.py
File metadata and controls
33 lines (23 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import annotations
from pathlib import Path
DEFAULT_BACKEND_PORT = 5002
BACKEND_PORTS = (5002, 5003)
def read_active_backend_port(state_file: Path, default_port: int = DEFAULT_BACKEND_PORT) -> int:
if not state_file.exists():
return default_port
raw = state_file.read_text(encoding="utf-8").strip()
if not raw:
return default_port
try:
return int(raw)
except ValueError:
return default_port
def write_active_backend_port(state_file: Path, port: int) -> None:
state_file.parent.mkdir(parents=True, exist_ok=True)
tmp_file = state_file.with_suffix(state_file.suffix + ".tmp")
tmp_file.write_text(str(port), encoding="utf-8")
tmp_file.replace(state_file)
def pick_standby_port(active_port: int, ports: tuple[int, int] = BACKEND_PORTS) -> int:
if active_port == ports[0]:
return ports[1]
return ports[0]