Skip to content

Commit

Permalink
fix: check if json_result is empty
Browse files Browse the repository at this point in the history
refactor: correct variable name

refactor: change logger types for some messages
  • Loading branch information
dbrennand committed Aug 25, 2024
1 parent f301af4 commit 1e1be01
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions speeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
exit(1)
else:
# Create list from comma separated string of server IDs
SPEEDER_SPEEDTEST_SERVER_ID = SPEEDER_SPEEDTEST_SERVER_ID.split(",")
SPEEDER_SPEEDTEST_SERVER_IDs = SPEEDER_SPEEDTEST_SERVER_ID.split(",")

# Connect to InfluxDB
logger.debug(
Expand All @@ -67,7 +67,7 @@
) as client:
# Run the speedtest using the librespeed/speedtest-cli on an interval
while True:
for server_id in SPEEDER_SPEEDTEST_SERVER_ID:
for server_id in SPEEDER_SPEEDTEST_SERVER_IDs:
logger.debug(f"Running speedtest for server ID: {server_id}.")
result = subprocess.run(
[
Expand All @@ -84,15 +84,18 @@
# Check if the speedtest failed
if result.returncode != 0:
# CLI errors go to stdout
logger.debug(
logger.error(
f"Speedtest for server ID: {server_id} failed with exit code: {result.returncode}.\nError: {result.stdout}"
)
else:
logger.debug(f"Speedtest for server ID: {server_id} succeeded.")
try:
json_result = json.loads(result.stdout)
# Check if the JSON result is empty
if not json_result:
raise json.decoder.JSONDecodeError("JSON result is empty.", json_result, 0)
except json.decoder.JSONDecodeError as err:
logger.debug(
logger.error(
f"Failed to parse JSON results for server ID: {server_id}.\nError: {err}"
)
continue
Expand Down

0 comments on commit 1e1be01

Please sign in to comment.