1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

VMware: Add timeout in wait_for_task (#39596)

* VMware: Add timeout in wait_for_task
* Added exponential backoff algorithm
* code review comments

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-06-20 11:23:58 +05:30 committed by GitHub
parent dce708e63e
commit b5c18ea2bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,7 @@ import atexit
import os import os
import ssl import ssl
import time import time
from random import randint
try: try:
# requests is required for exception handling of the ConnectionError # requests is required for exception handling of the ConnectionError
@ -23,26 +24,44 @@ except ImportError:
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from ansible.module_utils.six import integer_types, iteritems, string_types from ansible.module_utils.six import integer_types, iteritems, string_types
from ansible.module_utils.basic import env_fallback from ansible.module_utils.basic import env_fallback
from ansible.module_utils import six
class TaskError(Exception): class TaskError(Exception):
pass pass
def wait_for_task(task): def wait_for_task(task, max_backoff=64, timeout=3600):
"""Wait for given task using exponential back-off algorithm.
Args:
task: VMware task object
max_backoff: Maximum amount of sleep time in seconds
timeout: Timeout for the given task in seconds
Returns: Tuple with True and result for successful task
Raises: TaskError on failure
"""
failure_counter = 0
start_time = time.time()
while True: while True:
if time.time() - start_time >= timeout:
raise TaskError("Timeout")
if task.info.state == vim.TaskInfo.State.success: if task.info.state == vim.TaskInfo.State.success:
return True, task.info.result return True, task.info.result
if task.info.state == vim.TaskInfo.State.error: if task.info.state == vim.TaskInfo.State.error:
error_msg = task.info.error
try: try:
raise TaskError(task.info.error) error_msg = error_msg.msg
except AttributeError: except AttributeError:
raise TaskError("An unknown error has occurred") pass
if task.info.state == vim.TaskInfo.State.running: finally:
time.sleep(15) six.raise_from(TaskError(error_msg), task.info.error)
if task.info.state == vim.TaskInfo.State.queued: if task.info.state in [vim.TaskInfo.State.running, vim.TaskInfo.State.queued]:
time.sleep(15) sleep_time = min(2 ** failure_counter + randint(1, 1000), max_backoff)
time.sleep(sleep_time)
failure_counter += 1
def wait_for_vm_ip(content, vm, timeout=300): def wait_for_vm_ip(content, vm, timeout=300):