Skip to content

Commit

Permalink
fix(cdk-ops): intuitive timeout
Browse files Browse the repository at this point in the history
- Fixes the bug that `libdatawarehouse.data_api.wait_for_results` did
  not time out. It now monitors the elapsed time rather than the loop
  count. It also takes a new parameter `cancel_at_timeout` that makes
  the function cancel the statement when it times out. It is `True` by
  default.

issue codemonger-io#30
  • Loading branch information
kikuomax committed Oct 14, 2022
1 parent 27d794b commit 00f8ea7
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cdk-ops/lambda/libdatawarehouse/src/libdatawarehouse/data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def wait_for_results(
client,
statement_id: str,
polling_interval: float = 0.05,
polling_timeout: int = round(300 / 0.05),
timeout: float = 300.0,
cancel_at_timeout: bool = True,
) -> Tuple[Optional[str], Dict]:
"""Waits for a given statement to finish.
Expand All @@ -23,15 +24,20 @@ def wait_for_results(
:param float polling_interval: interval in seconds between two consecutive
pollings.
:param int polling_timeout: timeout represented as the number of pollings.
:param float timeout: timeout in seconds.
:param bool cancel_at_timeout: whether cancels the statement when it times
out.
"""
polling_counter = 0
start_time = time.time()
while True:
res = client.describe_statement(Id=statement_id)
status = res['Status']
if status not in RUNNING_STATUSES:
return status, res
polling_counter += 1
if polling_counter >= polling_timeout:
elapsed = time.time() - start_time
if elapsed >= timeout:
if cancel_at_timeout:
client.cancel_statement(Id=statement_id)
return None, res
time.sleep(polling_interval)

0 comments on commit 00f8ea7

Please sign in to comment.