Skip to content

Commit

Permalink
added better handling to the RequestConsumer for network failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Dec 5, 2023
1 parent 10d4252 commit 6b65ea9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion straders_sdk/request_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import sleep
import requests
from queue import PriorityQueue
import logging

# this consumer should be a singleton.
# it will have a priority queue of special request objects
Expand All @@ -25,6 +26,7 @@ def __init__(self, auto_start=True) -> None:
return
self.queue = PriorityQueue()
self.stop_flag = False
self.logger = logging.getLogger("RequestConsumer")
self._consumer_thread = Thread(
target=self._consume_until_stopped, daemon=auto_start
)
Expand Down Expand Up @@ -56,7 +58,14 @@ def _consume_until_stopped(self):
package.response = self._session.send(package.request)

except Exception as e:
print(e)
package.priority = 0
self.queue.put((0, package))
self.logger.warning(
"Request failed, retrying in 30 seconds - reason %s", e
)
sleep(30)
continue

if package.response.status_code != 429:
package.event.set()
print(
Expand Down
2 changes: 2 additions & 0 deletions straders_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def sleep_until_ready(ship: "Ship"):

def waypoint_slicer(waypoint_symbol: str) -> str:
"returns the system symbol from a waypoint symbol"
if "-" not in waypoint_symbol:
return waypoint_symbol
if not waypoint_symbol:
return None
pieces = waypoint_symbol.split("-")
Expand Down

0 comments on commit 6b65ea9

Please sign in to comment.