Add flexible wait mechanism for events
As far as nova is not hard-realtime system it is we should not depend on particular sleep time waiting for some event. Change-Id: I4271acbefbd735bb943263d664c5d68853ade0bc
This commit is contained in:
parent
a341023085
commit
43d631c616
|
@ -0,0 +1,7 @@
|
|||
class TimeoutException(Exception):
|
||||
msg = "Timeout has been occured."
|
||||
|
||||
def __init__(self, message=None, **kwargs):
|
||||
if not message:
|
||||
message = self.msg
|
||||
super(TimeoutException, self).__init__(message, kwargs)
|
|
@ -15,7 +15,6 @@ limitations under the License.
|
|||
|
||||
Freezer Backup modes related functions
|
||||
"""
|
||||
import time
|
||||
|
||||
from oslo_log import log
|
||||
|
||||
|
@ -51,16 +50,32 @@ class BackupOs(object):
|
|||
instance = nova.servers.get(instance_id)
|
||||
glance = client_manager.get_glance()
|
||||
|
||||
if instance.__dict__['OS-EXT-STS:task_state']:
|
||||
time.sleep(5)
|
||||
def instance_finish_task():
|
||||
instance = nova.servers.get(instance_id)
|
||||
return not instance.__dict__['OS-EXT-STS:task_state']
|
||||
|
||||
utils.wait_for(
|
||||
instance_finish_task, 1, 10,
|
||||
message="Waiting for instance {0} to finish {1} to start the "
|
||||
"snapshot process".format(
|
||||
instance_id,
|
||||
instance.__dict__['OS-EXT-STS:task_state']
|
||||
)
|
||||
)
|
||||
instance = nova.servers.get(instance)
|
||||
|
||||
image_id = nova.servers.create_image(instance,
|
||||
"snapshot_of_%s" % instance_id)
|
||||
|
||||
image = glance.images.get(image_id)
|
||||
while image.status != 'active':
|
||||
time.sleep(5)
|
||||
|
||||
def image_active():
|
||||
image = glance.images.get(image_id)
|
||||
return image.status == 'active'
|
||||
|
||||
utils.wait_for(image_active, 1, 10,
|
||||
message="Waiting for instnace {0} snapshot to become "
|
||||
"active".format(instance_id))
|
||||
try:
|
||||
image = glance.images.get(image_id)
|
||||
except Exception as e:
|
||||
|
|
|
@ -17,8 +17,11 @@ import datetime
|
|||
import os
|
||||
import time
|
||||
|
||||
import mock
|
||||
|
||||
from mock import patch
|
||||
|
||||
from freezer.exceptions import utils as exception_utils
|
||||
from freezer.openstack import osclients
|
||||
from freezer.tests import commons
|
||||
from freezer.utils import utils
|
||||
|
@ -202,3 +205,13 @@ class TestDateTime(object):
|
|||
d2 = datetime.datetime(2015, 3, 7, 18, 18, 38, 508411)
|
||||
ts2 = utils.DateTime(d2)
|
||||
assert '0:30:53.791612' == '{}'.format(ts2 - self.datetime)
|
||||
|
||||
def test_wait_for_positive(self):
|
||||
condition = mock.MagicMock(side_effect=[False, False, True, True])
|
||||
utils.wait_for(condition, 0.1, 1)
|
||||
self.assertEqual(3, condition.called)
|
||||
|
||||
def test_wait_for_negative(self):
|
||||
condition = mock.MagicMock(side_effect=[False, False, False])
|
||||
self.assertRaises(exception_utils.TimeoutException,
|
||||
utils.wait_for(condition, 0.1, 0.2))
|
||||
|
|
|
@ -25,11 +25,11 @@ import sys
|
|||
import time
|
||||
|
||||
from distutils import spawn as distspawn
|
||||
from freezer.exceptions import utils
|
||||
from functools import wraps
|
||||
from oslo_log import log
|
||||
from six.moves import configparser
|
||||
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
|
@ -510,3 +510,14 @@ def set_max_process_priority():
|
|||
])
|
||||
except Exception as priority_error:
|
||||
LOG.warning('Priority: {0}'.format(priority_error))
|
||||
|
||||
|
||||
def wait_for(condition_func, wait_interval, timeout, message=None):
|
||||
while timeout > 0:
|
||||
if condition_func():
|
||||
return
|
||||
|
||||
time.sleep(wait_interval)
|
||||
timeout -= wait_interval
|
||||
|
||||
raise utils.TimeoutException(message)
|
||||
|
|
Loading…
Reference in New Issue