Resurrecting poll_until
This commit is contained in:
parent
5189546e5e
commit
82c72e9058
@ -130,3 +130,6 @@ class BadValue(ReddwarfError):
|
|||||||
|
|
||||||
message = _("Value could not be converted: %(msg)s")
|
message = _("Value could not be converted: %(msg)s")
|
||||||
|
|
||||||
|
|
||||||
|
class PollTimeOut(ReddwarfError):
|
||||||
|
message = _("Polling request timed out.")
|
||||||
|
@ -22,6 +22,7 @@ import logging
|
|||||||
import re
|
import re
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import urlparse
|
import urlparse
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
@ -207,6 +208,26 @@ class LoopingCall(object):
|
|||||||
return self.done.wait()
|
return self.done.wait()
|
||||||
|
|
||||||
|
|
||||||
|
def poll_until(retriever, condition=lambda value: value,
|
||||||
|
sleep_time=1, time_out=None):
|
||||||
|
"""Retrieves object until it passes condition, then returns it.
|
||||||
|
|
||||||
|
If time_out_limit is passed in, PollTimeOut will be raised once that
|
||||||
|
amount of time is eclipsed.
|
||||||
|
|
||||||
|
"""
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
def poll_and_check():
|
||||||
|
obj = retriever()
|
||||||
|
if condition(obj):
|
||||||
|
raise LoopingCallDone(retvalue=obj)
|
||||||
|
if time_out is not None and time.time() > start_time + time_out:
|
||||||
|
raise exception.PollTimeOut
|
||||||
|
lc = LoopingCall(f=poll_and_check).start(sleep_time, True)
|
||||||
|
return lc.wait()
|
||||||
|
|
||||||
|
|
||||||
# Copied from nova.api.openstack.common in the old code.
|
# Copied from nova.api.openstack.common in the old code.
|
||||||
def get_id_from_href(href):
|
def get_id_from_href(href):
|
||||||
"""Return the id or uuid portion of a url.
|
"""Return the id or uuid portion of a url.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user