refactor open_obs_connection to implement exponential backoff and simplify connection logic
This commit is contained in:
@@ -17,12 +17,20 @@ def open_obs():
|
|||||||
subprocess.Popen(["obs", "--minimize-to-tray"])
|
subprocess.Popen(["obs", "--minimize-to-tray"])
|
||||||
|
|
||||||
def open_obs_connection(host: str, port: int, password: str) -> obs.ReqClient | None:
|
def open_obs_connection(host: str, port: int, password: str) -> obs.ReqClient | None:
|
||||||
try:
|
con = None
|
||||||
con = obs.ReqClient(host=host, port=port, password=password)
|
init_sleep = 1
|
||||||
return con
|
for _ in range(10):
|
||||||
except ConnectionRefusedError:
|
try:
|
||||||
print("Could not connect to OBS. Is it running and is the WebSocket server enabled?")
|
con = obs.ReqClient(host=host, port=port, password=password)
|
||||||
return None
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if con:
|
||||||
|
return con
|
||||||
|
|
||||||
|
time.sleep(init_sleep)
|
||||||
|
init_sleep *= 2
|
||||||
|
return None
|
||||||
|
|
||||||
def start_recording(con: obs.ReqClient) -> None:
|
def start_recording(con: obs.ReqClient) -> None:
|
||||||
con.start_record()
|
con.start_record()
|
||||||
@@ -49,10 +57,6 @@ def add_file_to_state(file_path: str) -> None:
|
|||||||
state = load_state()
|
state = load_state()
|
||||||
files = state.get("files", [])
|
files = state.get("files", [])
|
||||||
|
|
||||||
if files and len(files) > 0:
|
|
||||||
last_file = files[-1]
|
|
||||||
last_file["e_timestamp"] = datetime.datetime.now().timestamp()
|
|
||||||
|
|
||||||
files.append({
|
files.append({
|
||||||
"path": file_path,
|
"path": file_path,
|
||||||
"timestamp": datetime.datetime.now().timestamp(),
|
"timestamp": datetime.datetime.now().timestamp(),
|
||||||
@@ -76,16 +80,8 @@ def main() -> None:
|
|||||||
signal.signal(signal.SIGTERM, handle_shutdown)
|
signal.signal(signal.SIGTERM, handle_shutdown)
|
||||||
|
|
||||||
open_obs()
|
open_obs()
|
||||||
time.sleep(5)
|
|
||||||
|
|
||||||
config = load_config()
|
config = load_config()
|
||||||
|
con = open_obs_connection(config["obs"]["host"], config["obs"]["port"], config["obs"]["password"])
|
||||||
con = None
|
|
||||||
for _ in range(10):
|
|
||||||
con = open_obs_connection(config["obs"]["host"], config["obs"]["port"], config["obs"]["password"])
|
|
||||||
if con is not None:
|
|
||||||
break
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
recording_dir = con.get_record_directory().record_directory
|
recording_dir = con.get_record_directory().record_directory
|
||||||
start_recording(con)
|
start_recording(con)
|
||||||
|
|||||||
Reference in New Issue
Block a user