Skip to content

Commit

Permalink
implement websocket retries
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyinstarlight committed Jan 27, 2025
1 parent 70df0a5 commit 4da6172
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ disable=raw-checker-failed,
missing-function-docstring,
ungrouped-imports,
too-many-arguments,
too-many-positional-arguments,
too-many-locals,
too-many-branches,
too-many-statements,
Expand Down
49 changes: 38 additions & 11 deletions ftc_stream_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def upload_video(path, title, google_project_id, google_client_id, google_client
import urllib.error
import urllib.request

import websockets.client
import websockets


if sys.platform == 'win32':
Expand All @@ -261,6 +261,9 @@ def upload_video(path, title, google_project_id, google_client_id, google_client
comm = None # pylint: disable=invalid-name
stop = None # pylint: disable=invalid-name

retry_count = 0 # pylint: disable=invalid-name
retry_time = -1 # pylint: disable=invalid-name

output = None # pylint: disable=invalid-name
output_video_encoder = None # pylint: disable=invalid-name
output_audio_encoder = None # pylint: disable=invalid-name
Expand Down Expand Up @@ -729,29 +732,53 @@ def check_children():


def check_websocket():
global thread, post_time # pylint: disable=invalid-name
global thread, retry_count, retry_time, post_time # pylint: disable=invalid-name

if not obs.obs_data_get_bool(settings, 'switcher_enabled'):
return

# TODO: implement websocket retrying (e.g. wait 20 seconds and retry up to 5 times before giving up?)
if thread and not thread.is_alive():
# thread died and needs to be retried or cleaned up
print(f'ERROR: Connection to scorekeeper WS failed')
print()
if retry_time < 0:
# thread died and needs to be retried or cleaned up
print(f'ERROR: Connection to scorekeeper WS failed and will be retried')
print()

# disable switcher to prevent failure spam
stop.set()
thread = None
# start retry timer
retry_time = time.time()

# retry up to 5 times every 20 seconds
if retry_count < 5:
if time.time() >= retry_time + 20:
connect_scorekeeper_websocket()
retry_count += 1
retry_time = time.time()
else:
# disable switcher after too many failures
print(f'ERROR: Connection to scorekeeper WS failed too many times')
print()

# no return to let queue continue to be cleared since we are enabled
# clean up thread
stop.set()
thread = None

# reset retries
retry_count = 0
retry_time = -1

# no return on error to let queue continue to be cleared since we are enabled
else:
# reset retries if websocket connection successful
retry_count = 0
retry_time = -1

try:
while True:
if obs.obs_data_get_int(settings, 'match_wait_time') >= 0 and post_time >= 0 and time.time() >= post_time + obs.obs_data_get_int(settings, 'match_wait_time'):
# still in match post timer has been reached - set to match wait
scene = 'match_wait'
match_field = 0
match_name = ''
match_code = 0
else:
# check websocket for events
msg = comm.get_nowait()
Expand Down Expand Up @@ -831,7 +858,7 @@ def check_websocket():


async def run_websocket(uri):
async with websockets.client.connect(uri) as websocket:
async with websockets.connect(uri) as websocket:
# thread kill-switch check
while not stop.is_set():
try:
Expand Down

0 comments on commit 4da6172

Please sign in to comment.