Skip to content

Commit

Permalink
Merge pull request #86 from MiguelNdeCarvalho/feature/add-timeout-to-…
Browse files Browse the repository at this point in the history
…speedtest-cli-process

Fix Speedtest CLI bin location and add timeout for Speedtest CLI test
  • Loading branch information
MiguelNdeCarvalho authored Jul 23, 2021
2 parents 568d6f3 + d1fdc8a commit 574ef28
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN adduser -D speedtest
RUN pip install -r requirements.txt && \
export ARCHITECTURE=$(uname -m) && \
if [ "$ARCHITECTURE" == 'armv7l' ]; then export ARCHITECTURE=arm; fi && \
wget -O /tmp/speedtest.tgz "https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-${ARCHITECTURE}-linux.tgz" && \
wget -O /tmp/speedtest.tgz "https://install.speedtest.net/app/cli/ookla-speedtest-1.0.0-${ARCHITECTURE}-linux.tgz" && \
tar zxvf /tmp/speedtest.tgz -C /tmp && \
cp /tmp/speedtest /usr/local/bin && \
rm requirements.txt
Expand Down
9 changes: 8 additions & 1 deletion src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ def is_json(myjson):

def runTest():
serverID = os.environ.get('SPEEDTEST_SERVER')
timeout = int(os.environ.get('SPEEDTEST_TIMEOUT', 90))

cmd = ["speedtest", "--format=json-pretty", "--progress=no",
"--accept-license", "--accept-gdpr"]
if serverID:
cmd.append(f"--server-id={serverID}")
try:
output = subprocess.check_output(cmd)
output = subprocess.check_output(cmd, timeout=timeout)
except subprocess.CalledProcessError as e:
output = e.output
if not is_json(output):
Expand All @@ -54,6 +56,11 @@ def runTest():
'was not in JSON format')
print(output)
return (0, 0, 0, 0, 0, 0)
except subprocess.TimeoutExpired:
print('Speedtest CLI process took too long to complete ' +
'and was killed.')
return (0, 0, 0, 0, 0, 0)

if is_json(output):
data = json.loads(output)
if "error" in data:
Expand Down

0 comments on commit 574ef28

Please sign in to comment.