Skip to content

Commit

Permalink
Fix work queue timeout and stop the endless wait (#389)
Browse files Browse the repository at this point in the history
* Fix work queue timeout and stop the endless wait

* Cleaned up http.py, added exception handling

* black --line-length 120

* Removed profiling

* Re-add missing `update_event.wait()` call

---------

Co-authored-by: Tino <[email protected]>
  • Loading branch information
codymillscodes and hanzi authored Sep 8, 2024
1 parent cefb603 commit a2e9cdc
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions modules/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import time
from pathlib import Path
from threading import Event

import waitress
from apispec import APISpec
Expand All @@ -11,6 +12,7 @@
from flask_swagger_ui import get_swaggerui_blueprint
from jinja2 import Template

from modules.console import console
from modules.context import context
from modules.files import read_file
from modules.game import _event_flags
Expand Down Expand Up @@ -60,23 +62,25 @@ def _update_via_work_queue(
than or equal to that number of frames, this function
will do nothing.
"""

if state_cache_entry.age_in_frames < maximum_age_in_frames:
return

has_failed = False
update_event = Event()

def do_update():
nonlocal has_failed
try:
update_callback()
except:
has_failed = True

last_update_frame = state_cache_entry.frame
work_queue.put_nowait(do_update)
while not has_failed and state_cache_entry.frame == last_update_frame:
time.sleep(0.05)
except Exception:
console.print_exception()
finally:
update_event.set()

try:
work_queue.put_nowait(do_update)
update_event.wait(timeout=5)
except Exception:
console.print_exception()
return


def http_server() -> None:
Expand All @@ -99,7 +103,8 @@ def http_server() -> None:
description=f"{pokebot_name} API",
version=pokebot_version,
license=dict(
name="GNU General Public License v3.0", url="https://github.com/40Cakes/pokebot-gen3/blob/main/LICENSE"
name="GNU General Public License v3.0",
url="https://github.com/40Cakes/pokebot-gen3/blob/main/LICENSE",
),
),
servers=[
Expand Down Expand Up @@ -199,7 +204,6 @@ def http_get_party():
tags:
- pokemon
"""

cached_party = state_cache.party
_update_via_work_queue(cached_party, get_party)

Expand Down Expand Up @@ -583,11 +587,17 @@ def http_post_emulator():
context.bot_mode = new_settings["bot_mode"]
elif key == "video_enabled":
if not isinstance(new_settings["video_enabled"], bool):
return Response("Setting `video_enabled` did not contain a boolean value.", status=422)
return Response(
"Setting `video_enabled` did not contain a boolean value.",
status=422,
)
context.video = new_settings["video_enabled"]
elif key == "audio_enabled":
if not isinstance(new_settings["audio_enabled"], bool):
return Response("Setting `audio_enabled` did not contain a boolean value.", status=422)
return Response(
"Setting `audio_enabled` did not contain a boolean value.",
status=422,
)
context.audio = new_settings["audio_enabled"]
else:
return Response(f"Unrecognised setting: '{key}'.", status=422)
Expand Down Expand Up @@ -659,7 +669,10 @@ def update_inputs():
def http_get_events_stream():
subscribed_topics = request.args.getlist("topic")
if len(subscribed_topics) == 0:
return Response("You need to provide at least one `topic` parameter in the query.", status=422)
return Response(
"You need to provide at least one `topic` parameter in the query.",
status=422,
)

try:
queue, unsubscribe = add_subscriber(subscribed_topics)
Expand Down Expand Up @@ -765,7 +778,10 @@ def http_world_map():

@server.route("/swagger", methods=["GET"])
def http_get_swagger_json():
return Response(json.dumps(spec.to_dict(), indent=4), content_type="application/json; charset=utf-8")
return Response(
json.dumps(spec.to_dict(), indent=4),
content_type="application/json; charset=utf-8",
)

with server.test_request_context():
spec.path(view=http_get_player)
Expand Down

0 comments on commit a2e9cdc

Please sign in to comment.