Fix poll_until exception type

This patch adds catching loopingcall.LoopingCallTimeOut and raising
PollTimeOut exceptions.

Without this change we didn't know which exception would be thrown in
case of task timeout in function poll_until. As in the rest of the
trove code we catch only PollTimeOut exception it was possible to miss
loopingcall.LoopingCallTimeOut.

Change-Id: Ic3f8bdf99e82ac5c438354a4f3af7c6856d14f34
This commit is contained in:
Kasper Hasior 2019-03-26 13:54:46 +01:00
parent b00bad9495
commit 0c8a5ee9f0
1 changed files with 11 additions and 7 deletions

View File

@ -18,7 +18,6 @@ import collections
import inspect
import os
import shutil
import time
import uuid
from eventlet.timeout import Timeout
@ -186,14 +185,11 @@ class MethodInspector(object):
def build_polling_task(retriever, condition=lambda value: value,
sleep_time=1, time_out=0):
start_time = time.time()
def poll_and_check():
obj = retriever()
if condition(obj):
raise loopingcall.LoopingCallDone(retvalue=obj)
if time_out > 0 and time.time() - start_time > time_out:
raise exception.PollTimeOut
return loopingcall.BackOffLoopingCall(
f=poll_and_check).start(initial_delay=False,
@ -201,6 +197,14 @@ def build_polling_task(retriever, condition=lambda value: value,
max_interval=30, timeout=time_out)
def wait_for_task(polling_task):
"""Waits for the task until it is finished"""
try:
return polling_task.wait()
except loopingcall.LoopingCallTimeOut:
raise exception.PollTimeOut
def poll_until(retriever, condition=lambda value: value,
sleep_time=1, time_out=0):
"""Retrieves object until it passes condition, then returns it.
@ -209,9 +213,9 @@ def poll_until(retriever, condition=lambda value: value,
amount of time is eclipsed.
"""
return build_polling_task(retriever, condition=condition,
sleep_time=sleep_time, time_out=time_out).wait()
task = build_polling_task(retriever, condition=condition,
sleep_time=sleep_time, time_out=time_out)
return wait_for_task(task)
# Copied from nova.api.openstack.common in the old code.