Resurrecting poll_until

This commit is contained in:
Nirmal Ranganathan 2012-06-04 14:46:27 -05:00
parent 5189546e5e
commit 82c72e9058
2 changed files with 24 additions and 0 deletions

View File

@ -130,3 +130,6 @@ class BadValue(ReddwarfError):
message = _("Value could not be converted: %(msg)s")
class PollTimeOut(ReddwarfError):
message = _("Polling request timed out.")

View File

@ -22,6 +22,7 @@ import logging
import re
import signal
import sys
import time
import urlparse
import uuid
@ -207,6 +208,26 @@ class LoopingCall(object):
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.
def get_id_from_href(href):
"""Return the id or uuid portion of a url.