Hi
Anthony,
I think I see the issue, and it's not related
to the way you're constructing the app or client -- it's a problem with
your task polling loop (the inner, `while True` loop).
I also
have some follow-up notes on some things you can tidy up but which aren't
as important to correctness.
Your loop
reads:
The only way
this loop can exit is with an error. One trivial fix is to replace
`continue` with `break`.
However, I think we can make some other
improvements. For example, counting 300 1 second sleeps is not 5
minutes -- given that `get_task()` calls take time, it's probably a good
amount more! We can avoid that issue by measuring a time delta, instead of
counting steps. I also think it will be less error-prone to make the loop
condition express our actual stop criteria. Altogether, I'd express it like
so:
# poll in a "do-while" loop with a 5
minute time limit, until the task completes
start_time =
time.monotonic()
res =
SEARCH_CLIENT.get_task(task_id)
status =
res["state"]
while (time.monotonic() -
start_time) < 300 and status not in ("SUCCESS",
"FAILED"):
time.sleep(1)
res =
SEARCH_CLIENT.get_task(task_id)
status = res["state"]
if status ==
"FAILED":
raise ValueError(f"Ingest failed for
task '{task_id}'")
# if the
state was not failure (above) or success, something went
wrong
# so raise an
error
if res["state"] !=
"SUCCESS":
raise
ValueError(
f"Ingest
did not complete within time limit for task
'{task_id}'"
)
I would also
be concerned that something is not correct in your error handling in the
broader context where this code is called. Because the current loop doesn't
exit on success, I expect that it is waiting 5+ minutes and then raising an
exception. You may want to investigate to see where and how the exception
is being handled (or not).
There
are two other minor improvements I see, which I think will make your code
more efficient but aren't essential:
1. Are
you sure that you want to `prettyprint_json` and then `json.loads()` the
result? That looks a little unusual. If possible, I would avoid converting
the data to and from JSON.
2. Do you need to
create a new client and a new app object with each run of the loop? In most
scenarios, one app object and one client could be reused across loop
iterations. Both of these objects have components that they will reuse
across multiple requests, so reusing them makes them more
efficient.
I'm pretty sure that
fixing the loop will fix the main problem though, so let's start with
that!
Let us know if it still doesn't work
afterwards.
Cheers,
-Stephen