2.6.5.2 Python

Track Job Status

To track job status in Python, we can repeatedly query the API using the job URI returned in each response. To keep our script polite, we use the time library to sleep between requests. Since this is functionality that we require for a variety of tasks it is better to define it as a function:

...
def wait_for_job(joburi,s):
        while True:
            time.sleep(1)
            r=s.get(joburi)
            job=r.json()
            if job['summaryDone']:
                print '{name}: {runState}'.format(name=job['name'], runState=job['jobRunState'])
                if job['jobRunState'].upper() == 'FAILURE':
                    raise Exception('Job failed: {error}'.format(error=job['error']))
                elif job['jobRunState'].upper() == 'SUCCESS':
                    if 'resultId' in job:
                        return job['resultId']
                    break
                else:
                    break     

A completed job that consists of an action where an object is created in Oracle VM Manager also includes the ID of the object that has been created. We check the summaryDone property of the job to determine whether the job is complete, as this provides a more complete view of the status of any spawned child jobs as opposed to the done property. See Section 5.1.4, “Working with Jobs” for more information on this. For these types of activities, it is useful to return this value after the job is complete, so that it is easier to perform subsequent tasks related to the same object. Only return this value if the job was successful and no error was returned.