update to use time function

This commit is contained in:
sean-m-sullivan
2020-08-26 23:30:18 -05:00
parent 243c2cfe15
commit 0c18587851
4 changed files with 61 additions and 63 deletions

View File

@@ -7,6 +7,7 @@ from ansible.module_utils.six import PY2
from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.module_utils.six.moves.http_cookiejar import CookieJar
import time
import re
from json import loads, dumps
@@ -588,3 +589,26 @@ class TowerAPIModule(TowerModule):
return False
else:
return True
def wait_on_url(self, object_name=None, object_type=None, url=None, timeout=None, interval=None):
# Grab our start time to compare against for the timeout
start = time.time()
result = self.get_endpoint(url)
while not result['json']['finished']:
# If we are past our time out fail with a message
if timeout and timeout < time.time() - start:
self.json_output['msg'] = 'Monitoring of {0} "{1}" aborted due to timeout'.format(object_type, object_name)
self.fail_json(**self.json_output)
# Put the process to sleep for our interval
time.sleep(interval)
result = self.get_endpoint(url)
self.json_output['status'] = result['json']['status']
# If the job has failed, we want to raise a task failure for that so we get a non-zero response.
if result['json']['failed']:
self.json_output['msg'] = 'The {0} "{1}" failed'.format(object_type, object_name)
self.fail_json(**self.json_output)
return result