Skip to content

Commit

Permalink
Better error handling and logging for get operation status (#19)
Browse files Browse the repository at this point in the history
* Better error handling and logging for get operation status

---------

Co-authored-by: jgough <[email protected]>
  • Loading branch information
honourfish and jgough authored Jul 19, 2024
1 parent 6f7bbc9 commit 5bb325a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# code quality
autopep8~=2.0
black~=23.9
black~=24.4.2
pycodestyle~=2.10
pylint~=3.0
pyright~=1.1
Expand Down
49 changes: 39 additions & 10 deletions scitt/check_operation_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
import argparse
import logging
import sys

from time import sleep as time_sleep

Expand All @@ -10,7 +12,7 @@

# all timeouts and durations are in seconds
REQUEST_TIMEOUT = 30
POLL_TIMEOUT = 360
POLL_TIMEOUT = 60
POLL_INTERVAL = 10


Expand Down Expand Up @@ -41,24 +43,36 @@ def get_operation_status(operation_id: str, headers: dict) -> dict:
return response.json()


def poll_operation_status(operation_id: str, headers: dict) -> str:
def poll_operation_status(
operation_id: str, headers: dict, logger: logging.Logger
) -> str:
"""
polls for the operation status to be 'succeeded'.
"""

poll_attempts: int = int(POLL_TIMEOUT / POLL_INTERVAL)

logger.info("starting to poll for operation status 'succeeded'")

for _ in range(poll_attempts):
operation_status = get_operation_status(operation_id, headers)

# pylint: disable=fixme
# TODO: ensure get_operation_status handles error cases from the rest request
if "status" in operation_status and operation_status["status"] == "succeeded":
return operation_status["entryID"]
try:
operation_status = get_operation_status(operation_id, headers)

# pylint: disable=fixme
# TODO: ensure get_operation_status handles error cases from the rest request
if (
"status" in operation_status
and operation_status["status"] == "succeeded"
):
return operation_status["entryID"]

except requests.HTTPError as e:
logger.debug("failed getting operation status, error: %s", e)

time_sleep(POLL_INTERVAL)

raise TimeoutError("signed statement not registered within polling duration.")
raise TimeoutError("signed statement not registered within polling duration")


def main():
Expand Down Expand Up @@ -91,12 +105,27 @@ def main():
default=default_token_file_name,
)

# log level
parser.add_argument(
"--log-level",
type=str,
help="log level. for any individual poll errors use DEBUG, defaults to WARNING",
default="WARNING",
)

args = parser.parse_args()

logger = logging.getLogger("check operation status")
logging.basicConfig(level=logging.getLevelName(args.log_level))

headers = get_token_from_file(args.token_file_name)

entry_id = poll_operation_status(args.operation_id, headers)
print(entry_id)
try:
entry_id = poll_operation_status(args.operation_id, headers, logger)
print(entry_id)
except TimeoutError as e:
print(e, file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions unittests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests
"""

import unittest

# Hides Docstring
Expand Down

0 comments on commit 5bb325a

Please sign in to comment.